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
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
|Adafruit Feather M0 Express||Adafruit (PID 3403) Feather M0 Express – Designed for CircuitPython – ATSAMD21 Cortex M0|
|ADXL345||GY-291 ADXL345 Triple Axis IIC SPI Sensor Module|
|Connecting cables||Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire|
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
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)
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
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)