Simple test
Ensure your device works with this simple test.
examples/adxl34x_simpletest.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5
6import board
7
8import adafruit_adxl34x
9
10i2c = board.I2C() # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12
13# For ADXL343
14accelerometer = adafruit_adxl34x.ADXL343(i2c)
15# For ADXL345
16# accelerometer = adafruit_adxl34x.ADXL345(i2c)
17
18while True:
19 print("{} {} {}".format(*accelerometer.acceleration))
20 time.sleep(0.2)
Motion detection
Use the accelerometer to detect motion.
examples/adxl34x_motion_detection_test.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5
6import board
7
8import adafruit_adxl34x
9
10i2c = board.I2C() # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12
13# For ADXL343
14accelerometer = adafruit_adxl34x.ADXL343(i2c)
15# For ADXL345
16# accelerometer = adafruit_adxl34x.ADXL345(i2c)
17
18accelerometer.enable_motion_detection()
19# alternatively you can specify the threshold when you enable motion detection for more control:
20# accelerometer.enable_motion_detection(threshold=10)
21
22while True:
23 print("{} {} {}".format(*accelerometer.acceleration))
24
25 print("Motion detected: {}".format(accelerometer.events["motion"]))
26 time.sleep(0.5)
Freefall detection
Use the accelerometer to detect when something is dropped.
examples/adxl34x_freefall_detection_test.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5
6import board
7
8import adafruit_adxl34x
9
10i2c = board.I2C() # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12
13# For ADXL343
14accelerometer = adafruit_adxl34x.ADXL343(i2c)
15# For ADXL345
16# accelerometer = adafruit_adxl34x.ADXL345(i2c)
17
18accelerometer.enable_freefall_detection()
19# alternatively you can specify attributes when you enable freefall detection for more control:
20# accelerometer.enable_freefall_detection(threshold=10,time=25)
21
22while True:
23 print("{} {} {}".format(*accelerometer.acceleration))
24
25 print("Dropped: {}".format(accelerometer.events["freefall"]))
26 time.sleep(0.5)
Tap detection
The accelerometer can also be configured to detect taps.
examples/adxl34x_tap_detection_test.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5
6import board
7
8import adafruit_adxl34x
9
10i2c = board.I2C() # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12
13# For ADXL343
14accelerometer = adafruit_adxl34x.ADXL343(i2c)
15# For ADXL345
16# accelerometer = adafruit_adxl34x.ADXL345(i2c)
17
18accelerometer.enable_tap_detection()
19# you can also configure the tap detection parameters when you enable tap detection:
20# accelerometer.enable_tap_detection(tap_count=2,threshold=20, duration=50)
21
22while True:
23 print("{} {} {}".format(*accelerometer.acceleration))
24
25 print("Tapped: {}".format(accelerometer.events["tap"]))
26 time.sleep(0.5)
DisplayIO Simpletest
This is a simple test for boards with built-in display.
examples/adxl34x_displayio_simpletest.py
1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
3#
4# SPDX-License-Identifier: MIT
5import time
6
7import board
8from adafruit_display_text.bitmap_label import Label
9from displayio import Group
10from terminalio import FONT
11
12import adafruit_adxl34x
13
14# create a main_group to hold anything we want to show on the display.
15main_group = Group()
16
17i2c = board.I2C() # uses board.SCL and board.SDA
18# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
19
20# For ADXL343
21accelerometer = adafruit_adxl34x.ADXL343(i2c)
22# For ADXL345
23# accelerometer = adafruit_adxl34x.ADXL345(i2c)
24
25# Create a Label to show the readings. If you have a very small
26# display you may need to change to scale=1.
27display_output_label = Label(FONT, text="", scale=1)
28
29# place the label in the middle of the screen with anchored positioning
30display_output_label.anchor_point = (0, 0)
31display_output_label.anchored_position = (4, board.DISPLAY.height // 2)
32
33# add the label to the main_group
34main_group.append(display_output_label)
35
36# set the main_group as the root_group of the built-in DISPLAY
37board.DISPLAY.root_group = main_group
38
39# begin main loop
40while True:
41 acc_x, acc_y, acc_z = accelerometer.acceleration
42 # Update the label.text property to change the text on the display
43 display_output_label.text = f"x:{acc_x:.1f} m/s^2 y:{acc_y:.1f} m/s^2 z:{acc_z:.1f} m/s^2"
44 # wait for a bit
45 time.sleep(1)