LSM6DSOX Simple test
Ensure your LSM6DSOX device works with this simple test.
examples/lsm6ds_lsm6dsox_simpletest.py
1# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4import time
5
6import board
7
8from adafruit_lsm6ds.lsm6dsox import LSM6DSOX
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 = LSM6DSOX(i2c)
13
14while True:
15 accel_x, accel_y, accel_z = sensor.acceleration
16 print(f"Acceleration: X:{accel_x:.2f}, Y: {accel_y:.2f}, Z: {accel_z:.2f} m/s^2")
17 gyro_x, gyro_y, gyro_z = sensor.gyro
18 print(f"Gyro X:{gyro_x:.2f}, Y: {gyro_y:.2f}, Z: {gyro_z:.2f} radians/s")
19 print("")
20 time.sleep(0.5)
LSM6DSO32 Simple test
Ensure your LSM6DSO32 device works with this simple test.
examples/lsm6ds_lsm6dso32_simpletest.py
1# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4import time
5
6import board
7
8from adafruit_lsm6ds.lsm6dso32 import LSM6DSO32
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 = LSM6DSO32(i2c)
13
14while True:
15 accel_x, accel_y, accel_z = sensor.acceleration
16 print(f"Acceleration: X:{accel_x:.2f}, Y: {accel_y:.2f}, Z: {accel_z:.2f} m/s^2")
17 gyro_x, gyro_y, gyro_z = sensor.gyro
18 print(f"Gyro X:{gyro_x:.2f}, Y: {gyro_y:.2f}, Z: {gyro_z:.2f} radians/s")
19 print("")
20 time.sleep(0.5)
LSM6DS Simple test
Ensure your LSM6DS device works with this simple test.
examples/lsm6ds_lsm6ds33_simpletest.py
1# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4import time
5
6import board
7
8from adafruit_lsm6ds.lsm6ds33 import LSM6DS33
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 = LSM6DS33(i2c)
13
14while True:
15 accel_x, accel_y, accel_z = sensor.acceleration
16 print(f"Acceleration: X:{accel_x:.2f}, Y: {accel_y:.2f}, Z: {accel_z:.2f} m/s^2")
17 gyro_x, gyro_y, gyro_z = sensor.gyro
18 print(f"Gyro X:{gyro_x:.2f}, Y: {gyro_y:.2f}, Z: {gyro_z:.2f} radians/s")
19 print("")
20 time.sleep(0.5)
ISM330DHCX Simple test
Ensure your ISM330DHCX device works with this simple test.
examples/lsm6ds_ism330dhcx_simpletest.py
1# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4import time
5
6import board
7
8from adafruit_lsm6ds.ism330dhcx import ISM330DHCX
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 = ISM330DHCX(i2c)
13
14while True:
15 accel_x, accel_y, accel_z = sensor.acceleration
16 print(f"Acceleration: X:{accel_x:.2f}, Y: {accel_y:.2f}, Z: {accel_z:.2f} m/s^2")
17 gyro_x, gyro_y, gyro_z = sensor.gyro
18 print(f"Gyro X:{gyro_x:.2f}, Y: {gyro_y:.2f}, Z: {gyro_z:.2f} radians/s")
19 print("")
20 time.sleep(0.5)
LSM6DS Full test
LSM6DS Full tests
examples/lsm6ds_full_test.py
1# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4import time
5
6import board
7
8# pylint:disable=no-member
9from adafruit_lsm6ds import AccelRange, GyroRange, Rate
10from adafruit_lsm6ds.lsm6dsox import LSM6DSOX as LSM6DS
11
12# from adafruit_lsm6ds.lsm6ds33 import LSM6DS33 as LSM6DS
13# from adafruit_lsm6ds.lsm6dso32 import LSM6DSO32 as LSM6DS
14# from adafruit_lsm6ds.ism330dhcx import ISM330DHCX as LSM6DS
15
16i2c = board.I2C() # uses board.SCL and board.SDA
17# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
18sensor = LSM6DS(i2c)
19
20sensor.accelerometer_range = AccelRange.RANGE_8G
21print(f"Accelerometer range set to: {AccelRange.string[sensor.accelerometer_range]:d} G")
22
23sensor.gyro_range = GyroRange.RANGE_2000_DPS
24print(f"Gyro range set to: {GyroRange.string[sensor.gyro_range]:d} DPS")
25
26sensor.accelerometer_data_rate = Rate.RATE_1_66K_HZ
27# sensor.accelerometer_data_rate = Rate.RATE_12_5_HZ
28print(f"Accelerometer rate set to: {Rate.string[sensor.accelerometer_data_rate]:d} HZ")
29
30sensor.gyro_data_rate = Rate.RATE_1_66K_HZ
31print(f"Gyro rate set to: {Rate.string[sensor.gyro_data_rate]:d} HZ")
32
33while True:
34 print(
35 "Accel X:%.2f Y:%.2f Z:%.2f ms^2 Gyro X:%.2f Y:%.2f Z:%.2f radians/s"
36 % (sensor.acceleration + sensor.gyro)
37 )
38 time.sleep(0.05)
Pedometer Example
Example showing how to use the device as a pedometer
examples/lsm6ds_pedometer.py
1# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4"""This example shows off how to use the step counter built
5into the ST LSM6DS series IMUs. The steps are calculated in
6the chip so you don't have to do any calculations!"""
7
8import time
9
10import board
11
12from adafruit_lsm6ds import AccelRange, Rate
13
14# pylint:disable=no-member
15# Import the correct class for the sensor that you have.
16# Change the import as needed. Not all are listed here.
17# Note that the LSM6DS3TRC, which is used on the Adafruit Feather Sense,
18# uses a different I2C register to enable the pedometer,
19# so the device classes are not interchangeable.
20#
21# from adafruit_lsm6ds.lsm6ds33 import LSM6DS33 as LSM
22from adafruit_lsm6ds.lsm6ds3trc import LSM6DS3TRC as LSM
23
24i2c = board.I2C() # uses board.SCL and board.SDA
25# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
26sensor = LSM(i2c)
27
28# enable accelerometer sensor @ 2G and 26 Hz
29sensor.accelerometer_range = AccelRange.RANGE_2G
30sensor.accelerometer_data_rate = Rate.RATE_26_HZ
31# no gyro used for step detection
32sensor.gyro_data_rate = Rate.RATE_SHUTDOWN
33
34# enable the pedometer
35sensor.pedometer_enable = True
36
37while True:
38 print("Steps: ", sensor.pedometer_steps)
39 time.sleep(1)
Rate test
Example showing a Rate test
examples/lsm6ds_rate_test.py
1# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4import board
5
6# pylint:disable=no-member,unused-import
7from adafruit_lsm6ds import Rate
8from adafruit_lsm6ds.lsm6dsox import LSM6DSOX as LSM6DS
9
10# from adafruit_lsm6ds.lsm6ds33 import LSM6DS33 as LSM6DS
11# from adafruit_lsm6ds.lsm6dso32 import LSM6DSO32 as LSM6DS
12# from adafruit_lsm6ds.ism330dhcx import ISM330DHCX as LSM6DS
13
14i2c = board.I2C() # uses board.SCL and board.SDA
15# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
16sensor = LSM6DS(i2c)
17
18while True:
19 sensor.accelerometer_data_rate = Rate.RATE_12_5_HZ
20 sensor.gyro_data_rate = Rate.RATE_12_5_HZ
21 for i in range(100):
22 print("(%.2f, %.2f, %.2f, %.2f, %.2f, %.2f" % (sensor.acceleration + sensor.gyro))
23 print()
24
25 sensor.accelerometer_data_rate = Rate.RATE_52_HZ
26 sensor.gyro_data_rate = Rate.RATE_52_HZ
27 for i in range(100):
28 print("(%.2f, %.2f, %.2f, %.2f, %.2f, %.2f" % (sensor.acceleration + sensor.gyro))
29 print()
30
31 sensor.accelerometer_data_rate = Rate.RATE_416_HZ
32 sensor.gyro_data_rate = Rate.RATE_416_HZ
33 for i in range(100):
34 print("(%.2f, %.2f, %.2f, %.2f, %.2f, %.2f" % (sensor.acceleration + sensor.gyro))
35 print()