Python and CircuitPython

Using the ADXL343 with CircuitPython is easy. The Adafruit_CircuitPython_ADXL34x repo on Github always contains the latest public code, and allows you to quickly and easily get started with the Adafruit ADXL343 breakout board.

CircuitPython Wiring

The diagram below shows how you can wire up your CircuitPython board (in Feather form below) to the ADXL343:

 

  • Connect SCL on the Feather to SCL on the ADXL343
  • Connect SDA on the Feather to SDA in the ADXL343
  • Connect GND on the Feather to GND on the ADXL343
  • Connect 3.3V on the Feather to VIN on the ADXL343

Library Installation

You'll need to install the Adafruit CircuitPython ADXL34x library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our CircuitPython starter guide has a great page on how to install the library bundle.

For non-express boards like the Trinket M0 or Gemma M0, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_adxl34x.mpy
  • adafruit_bus_device

Before continuing make sure your board's lib folder or root filesystem has the adafruit_adxl34x.mpy, and adafruit_bus_device files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

If this works for you, scroll down to the Example Code section to get a minimal example of talking to the ADXL343 up and running in no time!

Python Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Make the following connections between the Pi and the ADXL343:

  • Connect SCL on the RPi to SCL on the ADXL343
  • Connect SDA on the Rpi to SDA in the ADXL343
  • Connect GND on the Rpi to GND on the ADXL343
  • Connect 3.3V on the Rpi to VIN on the ADXL343

Python Installation of the ADXL34x Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-adxl34x

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

If you open the Serial tab in mu-editor with the sample code running, you should get output resembling the following if everything was setup correctly:

Example Code

The ADXL34x codebase contains several examples, but the easiest place to start is to open up or copy and paste the adxl34x_simpletest.py code, shown below for convenience sake:

import time
import board
import busio
import adafruit_adxl34x
i2c = busio.I2C(board.SCL, board.SDA)
accelerometer = adafruit_adxl34x.ADXL345(i2c)
while True:
    print("%f %f %f"%accelerometer.acceleration)
    time.sleep(1)

If you open this in mu-editor and save it as code.py, opening the Serial window, you should see something resembling the following output:

This guide was first published on Feb 24, 2019. It was last updated on Feb 24, 2019. This page (Python and CircuitPython) was last updated on May 23, 2019.