ICM20649 Simple test¶
Ensure your ICM20649 device works with one of these simple tests.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6import adafruit_icm20x
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
10icm = adafruit_icm20x.ICM20649(i2c)
11
12while True:
13 print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2" % (icm.acceleration))
14 print("Gyro X:%.2f, Y: %.2f, Z: %.2f rads/s" % (icm.gyro))
15 print("")
16 time.sleep(0.5)
ICM20649 Full test¶
Test using all the ICM20649 sensor capabilities
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6from adafruit_icm20x import ICM20649, AccelRange, GyroRange
7
8
9def printNewMax(value, current_max, axis):
10 if value > current_max:
11 current_max = value
12 print(axis, "Max:", current_max)
13 return current_max
14
15
16# pylint:disable=no-member
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
20ism = ICM20649(i2c)
21
22ism.accelerometer_range = AccelRange.RANGE_30G
23print("Accelerometer range set to: %d g" % AccelRange.string[ism.accelerometer_range])
24
25ism.gyro_range = GyroRange.RANGE_500_DPS
26print("Gyro range set to: %d DPS" % GyroRange.string[ism.gyro_range])
27
28ax_max = ay_max = az_max = 0
29gx_max = gy_max = gz_max = 0
30
31ism.gyro_data_rate = 125
32ism.accelerometer_data_rate = 4095
33st = time.monotonic()
34while time.monotonic() - st < 0.250:
35 print(
36 "Accel X:%.2f Y:%.2f Z:%.2f ms^2 Gyro X:%.2f Y:%.2f Z:%.2f degrees/s"
37 % (ism.acceleration + ism.gyro)
38 )
39
40# acceleration = ism.acceleration
41# # ax_max = printNewMax(acceleration[0], ax_max, "AX")
42# # ay_max = printNewMax(acceleration[1], ay_max, "AY")
43# # az_max = printNewMax(acceleration[2], az_max, "AZ")
44
45# gyro = ism.gyro
46# # gx_max = printNewMax(gyro[0], gx_max, "GX")
47# # gy_max = printNewMax(gyro[1], gy_max, "GY")
48# # gz_max = printNewMax(gyro[2], gz_max, "GZ")
ICM20948 Simple test¶
Ensure your ICM20948 device works with one of these simple tests.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6import adafruit_icm20x
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
10icm = adafruit_icm20x.ICM20948(i2c)
11
12while True:
13 print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2" % (icm.acceleration))
14 print("Gyro X:%.2f, Y: %.2f, Z: %.2f rads/s" % (icm.gyro))
15 print("Magnetometer X:%.2f, Y: %.2f, Z: %.2f uT" % (icm.magnetic))
16 print("")
17 time.sleep(0.5)
ICM20948 Acceleration data rate test¶
Example showing ICM20948 sensor cycling between two acceleration data rates
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6from adafruit_icm20x import ICM20948
7
8cycles = 200
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
11icm = ICM20948(i2c)
12
13# Cycle between two data rates
14# Best viewed in the Mu serial plotter where you can see how
15# the data rate affects the resolution of the data
16while True:
17 icm.accelerometer_data_rate_divisor = 0 # minimum
18 print("Data Rate:", icm.accelerometer_data_rate)
19 time.sleep(2)
20 for i in range(cycles):
21 print(icm.acceleration)
22
23 icm.accelerometer_data_rate_divisor = 4095 # maximum
24 print("Data Rate:", icm.accelerometer_data_rate)
25 time.sleep(2)
26 for i in range(cycles):
27 print(icm.acceleration)
ICM20948 Gyro data rate test¶
Example showing ICM20948 sensor cycling between two gyro data rates
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6from adafruit_icm20x import ICM20948
7
8cycles = 200
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
11icm = ICM20948(i2c)
12
13# Cycle between two data rates
14# Best viewed in the Mu serial plotter where you can see how
15# the data rate affects the resolution of the data
16while True:
17 icm.gyro_data_rate_divisor = 0 # minimum
18 print("Data Rate:", icm.gyro_data_rate)
19 time.sleep(2)
20 for i in range(cycles):
21 print(icm.gyro)
22
23 icm.gyro_data_rate_divisor = 255 # maximum
24 print("Data Rate:", icm.gyro_data_rate)
25 time.sleep(2)
26 for i in range(cycles):
27 print(icm.gyro)
ICM20948 Magnetic data rate test¶
Example showing ICM20948 sensor cycling between two magnetic data rates
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# pylint: disable=no-member
5import time
6import board
7from adafruit_icm20x import MagDataRate, ICM20948
8
9cycles = 200
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
12icm = ICM20948(i2c)
13
14# Cycle between two data rates
15# Best viewed in the Mu serial plotter where you can see how
16# the data rate affects the resolution of the data
17while True:
18 icm.magnetometer_data_rate = MagDataRate.RATE_100HZ
19 for i in range(cycles):
20 print(icm.magnetic)
21 time.sleep(0.3)
22 icm.magnetometer_data_rate = MagDataRate.RATE_10HZ
23 for i in range(cycles):
24 print(icm.magnetic)
25 time.sleep(0.3)