简单测试¶
确保您的设备能够通过这个简单的测试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
import board
import adafruit_adxl34x
i2c = board.I2C() # uses board.SCL and 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)
|
运动检测¶
使用加速度计检测运动。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
import board
import adafruit_adxl34x
i2c = board.I2C() # uses board.SCL and 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)
|
自由落体检测¶
使用加速度计来检测什么时候掉了东西。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
import board
import adafruit_adxl34x
i2c = board.I2C() # uses board.SCL and board.SDA
# For ADXL343
accelerometer = adafruit_adxl34x.ADXL343(i2c)
# For ADXL345
# accelerometer = adafruit_adxl34x.ADXL345(i2c)
accelerometer.enable_freefall_detection()
# alternatively you can specify attributes when you enable freefall detection for more control:
# accelerometer.enable_freefall_detection(threshold=10,time=25)
while True:
print("%f %f %f" % accelerometer.acceleration)
print("Dropped: %s" % accelerometer.events["freefall"])
time.sleep(0.5)
|
敲击检测¶
加速度计还可以配置为检测敲击。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
import board
import adafruit_adxl34x
i2c = board.I2C() # uses board.SCL and board.SDA
# For ADXL343
accelerometer = adafruit_adxl34x.ADXL343(i2c)
# For ADXL345
# accelerometer = adafruit_adxl34x.ADXL345(i2c)
accelerometer.enable_tap_detection()
# you can also configure the tap detection parameters when you enable tap detection:
# accelerometer.enable_tap_detection(tap_count=2,threshold=20, duration=50)
while True:
print("%f %f %f" % accelerometer.acceleration)
print("Tapped: %s" % accelerometer.events["tap"])
time.sleep(0.5)
|