Simple tests¶
Ensure your device works with these simple tests.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4""" Display accelerometer data once per second """
5
6import time
7import board
8import adafruit_lsm303_accel
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
12sensor = adafruit_lsm303_accel.LSM303_Accel(i2c)
13
14while True:
15 acc_x, acc_y, acc_z = sensor.acceleration
16
17 print(
18 "Acceleration (m/s^2): ({0:10.3f}, {1:10.3f}, {2:10.3f})".format(
19 acc_x, acc_y, acc_z
20 )
21 )
22 print("")
23 time.sleep(1.0)
Fast Acceleration Example¶
Example to demonstrate fast acceleration data acquisition
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4""" Read data from the accelerometer and print it out, ASAP! """
5
6import board
7import adafruit_lsm303_accel
8
9i2c = board.I2C() # uses board.SCL and board.SDA
10# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
11sensor = adafruit_lsm303_accel.LSM303_Accel(i2c)
12
13while True:
14 accel_x, accel_y, accel_z = sensor.acceleration
15 print("{0:10.3f} {1:10.3f} {2:10.3f}".format(accel_x, accel_y, accel_z))
Inclinometer Example¶
Demonstrate inclinometer example
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4""" Display inclination data five times per second """
5
6import time
7from math import atan2, degrees
8import board
9import adafruit_lsm303_accel
10
11
12i2c = board.I2C() # uses board.SCL and board.SDA
13# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
14sensor = adafruit_lsm303_accel.LSM303_Accel(i2c)
15
16
17def vector_2_degrees(x, y):
18 angle = degrees(atan2(y, x))
19 if angle < 0:
20 angle += 360
21 return angle
22
23
24def get_inclination(_sensor):
25 x, y, z = _sensor.acceleration
26 return vector_2_degrees(x, z), vector_2_degrees(y, z)
27
28
29while True:
30 angle_xz, angle_yz = get_inclination(sensor)
31 print("XZ angle = {:6.2f}deg YZ angle = {:6.2f}deg".format(angle_xz, angle_yz))
32 time.sleep(0.2)
Tap Detection Example¶
Tap detection example
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import board
5import adafruit_lsm303_accel
6
7i2c = board.I2C() # uses board.SCL and board.SDA
8# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
9accel = adafruit_lsm303_accel.LSM303_Accel(i2c)
10accel.range = adafruit_lsm303_accel.Range.RANGE_8G
11accel.set_tap(1, 30)
12
13while True:
14 if accel.tapped:
15 print("Tapped!\n")