Python and CircuitPython

It's easy to use the ADXL343 or the ADXL345 with Python and CircuitPython, and the Adafruit CircuitPython ADXL34x module. This module allows you to easily write Python code that reads the acceleration, taps, motion and more from the breakout.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

The pinouts on the ADXL343 and the ADXL345 are slightly different, but the chips are essentially identical. This page includes different wiring diagrams for each. Other than initialising the proper chip, the code will be the same for both!

CircuitPython Microcontroller Wiring

First, wire up the breakout exactly as shown in the previous pages. Here is an example of wiring the ADXL343 to a Feather M0:

 

  • 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

Here's an example of wiring the ADXL345 to a Feather M0:

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

Python Computer 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

The following shows a Raspberry Pi connected to 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

The following shows a Raspberry Pi connected to the ADXL345:

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

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.

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!

CircuitPython & Python Usage

To demonstrate the usage of the breakout we'll initialize it and read the acceleration and more from the board's Python REPL.

Run the following code to import the necessary modules and create the I2C object:

Download: file
import time
import board
import busio
import adafruit_adxl34x

i2c = busio.I2C(board.SCL, board.SDA)

If you're using the ADXL343, run the following to initialise the I2C connection with the breakout:

Download: file
accelerometer = adafruit_adxl34x.ADXL343(i2c)

If you're using the ADXL345, run the following to initialise the I2C connection with the breakout:

Download: file
accelerometer = adafruit_adxl34x.ADXL345(i2c)

Now you're ready to read values from and enable features of the breakout using any of the following:

  • acceleration - The acceleration values on the x, y and z axes
  • enable_motion_detection - Enables motion detection. Allows for setting threshold. Threshold defaults to 18.
  • enable_tap_detection - Enables tap detection. Allows for single or double-tap detection.
  • enable_freefall_detection - Enables freefall detection. Allows for setting threshold and time. Threshold defaults to 10, time defaults to 25.
  • events - Used to read the events when motion detection, tap detection and freefall detection are enables. Requires specifying which event you are trying to read.

To print the acceleration values:

Download: file
while True:
    print(accelerometer.acceleration)
    time.sleep(0.2)

That's all there is to reading acceleration values from the ADXL343 and ADXL345 using CircuitPython!

Full Example Code

import time
import board
import busio
import adafruit_adxl34x

i2c = busio.I2C(board.SCL, board.SDA)

# For ADXL343
accelerometer = adafruit_adxl34x.ADXL343(i2c)
# For ADXL345
# accelerometer = adafruit_adxl34x.ADXL345(i2c)

while True:
    print("%f %f %f" % accelerometer.acceleration)
    time.sleep(0.2)

Motion, Tap and Freefall

There are examples for enabling and using motion, tap and freefall available on GitHub:

Save any of the files as code.py on your CircuitPython board, or run them from the Python REPL on your Linux computer, to try them out.

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 Oct 18, 2019.