Simple test
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6import adafruit_adxl34x
7
8i2c = board.I2C() # uses board.SCL and board.SDA
9# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
10
11# For ADXL343
12accelerometer = adafruit_adxl34x.ADXL343(i2c)
13# For ADXL345
14# accelerometer = adafruit_adxl34x.ADXL345(i2c)
15
16while True:
17 print("%f %f %f" % accelerometer.acceleration)
18 time.sleep(0.2)
Motion detection
Use the accelerometer to detect motion.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6import adafruit_adxl34x
7
8i2c = board.I2C() # uses board.SCL and board.SDA
9# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
10
11# For ADXL343
12accelerometer = adafruit_adxl34x.ADXL343(i2c)
13# For ADXL345
14# accelerometer = adafruit_adxl34x.ADXL345(i2c)
15
16accelerometer.enable_motion_detection()
17# alternatively you can specify the threshold when you enable motion detection for more control:
18# accelerometer.enable_motion_detection(threshold=10)
19
20while True:
21 print("%f %f %f" % accelerometer.acceleration)
22
23 print("Motion detected: %s" % accelerometer.events["motion"])
24 time.sleep(0.5)
Freefall detection
Use the accelerometer to detect when something is dropped.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6import adafruit_adxl34x
7
8i2c = board.I2C() # uses board.SCL and board.SDA
9# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
10
11# For ADXL343
12accelerometer = adafruit_adxl34x.ADXL343(i2c)
13# For ADXL345
14# accelerometer = adafruit_adxl34x.ADXL345(i2c)
15
16accelerometer.enable_freefall_detection()
17# alternatively you can specify attributes when you enable freefall detection for more control:
18# accelerometer.enable_freefall_detection(threshold=10,time=25)
19
20while True:
21 print("%f %f %f" % accelerometer.acceleration)
22
23 print("Dropped: %s" % accelerometer.events["freefall"])
24 time.sleep(0.5)
Tap detection
The accelerometer can also be configured to detect taps.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6import adafruit_adxl34x
7
8i2c = board.I2C() # uses board.SCL and board.SDA
9# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
10
11# For ADXL343
12accelerometer = adafruit_adxl34x.ADXL343(i2c)
13# For ADXL345
14# accelerometer = adafruit_adxl34x.ADXL345(i2c)
15
16accelerometer.enable_tap_detection()
17# you can also configure the tap detection parameters when you enable tap detection:
18# accelerometer.enable_tap_detection(tap_count=2,threshold=20, duration=50)
19
20while True:
21 print("%f %f %f" % accelerometer.acceleration)
22
23 print("Tapped: %s" % accelerometer.events["tap"])
24 time.sleep(0.5)