Home Circuitpython Adafruit Feather M0 and ADXL345 accelerometer circuitpython example

Adafruit Feather M0 and ADXL345 accelerometer circuitpython example

by shedboy71

In this example we connect a ADXL345 accelerometer to an Adafruit Feather M0 running Circuitpython

First lets look at some information about the sensor from the manufacturer

Sensor Information

The ADXL345 is a small, thin, low power, 3-axis accelerometer with high resolution (13-bit) measurement at up to ±16g. Digital output data is formatted as 16-bit twos complement and is accessible through either a SPI (3- or 4-wire) or I2C digital interface.

The ADXL345 is well suited for mobile device applications. It measures the static acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration resulting from motion or shock. Its high resolution (4 mg/LSB) enables measurement of inclination changes less than 1.0°.

Several special sensing functions are provided. Activity and inactivity sensing detect the presence or lack of motion and if the acceleration on any axis exceeds a user-set level. Tap sensing detects single and double taps.

Free-fall sensing detects if the device is falling. These functions can be mapped to one of two interrupt output pins. An integrated, patent pending 32-level first in, first out (FIFO) buffer can be used to store data to minimize host processor intervention.

Low power modes enable intelligent motion-based power management with threshold sensing and active acceleration measurement at extremely low power dissipation.

 

This was my sensor of choice that I used

Parts Required

 

Name Link
Adafruit Feather M0 Express Amazon link

Ebay link

ADXL345 Amazon.com linkAmazon link

Ebay link

Connecting cables Aliexpress product link

Amazon.com link

Ebay link

 

Schematic/Connection

 

feather and ADXL345 layout

Code Example

I used Mu for development

The following is based on a library , I copied the adafruit_adxl34x library for this device to the lib folder on my Feather M0 Express – https://circuitpython.org/libraries

This is the basic example which comes with the library

[codesyntax lang=”python”]

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)

[/codesyntax]

Output

Here is what I saw in Mu REPL window

16.788971 -5.138680 14.239244
8.119900 16.122116 1.372930
5.727077 -0.353039 5.217134
-1.216024 8.904434 -13.493942
20.044777 9.257469 20.044777
8.080673 -3.687298 -4.511055
11.846421 20.044777 18.279587

Code 2 : Motion Detection example

And another example

[codesyntax lang=”python”]

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)

accelerometer.enable_motion_detection()
# alternatively you can specify the threshold when you enable motion detection for more control:
# accelerometer.enable_motion_detection(threshold=10)

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

    print("Motion detected: %s" % accelerometer.events["motion"])
    time.sleep(0.5)

[/codesyntax]

Links

https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf

You may also like

Leave a Comment