ICM20649 Simple test

Ensure your ICM20649 device works with one of these simple tests.

examples/icm20x_icm20649_simpletest.py
 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
 9icm = adafruit_icm20x.ICM20649(i2c)
10
11while True:
12    print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2" % (icm.acceleration))
13    print("Gyro X:%.2f, Y: %.2f, Z: %.2f rads/s" % (icm.gyro))
14    print("")
15    time.sleep(0.5)

ICM20649 Full test

Test using all the ICM20649 sensor capabilities

examples/examples/icm20x_icm20649_full_test.py
 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
19ism = ICM20649(i2c)
20
21ism.accelerometer_range = AccelRange.RANGE_30G
22print("Accelerometer range set to: %d g" % AccelRange.string[ism.accelerometer_range])
23
24ism.gyro_range = GyroRange.RANGE_500_DPS
25print("Gyro range set to: %d DPS" % GyroRange.string[ism.gyro_range])
26
27ax_max = ay_max = az_max = 0
28gx_max = gy_max = gz_max = 0
29
30ism.gyro_data_rate = 125
31ism.accelerometer_data_rate = 4095
32st = time.monotonic()
33while time.monotonic() - st < 0.250:
34
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.

examples/icm20x_icm20948_simpletest.py
 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
 9icm = adafruit_icm20x.ICM20948(i2c)
10
11while True:
12    print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2" % (icm.acceleration))
13    print("Gyro X:%.2f, Y: %.2f, Z: %.2f rads/s" % (icm.gyro))
14    print("Magnetometer X:%.2f, Y: %.2f, Z: %.2f uT" % (icm.magnetic))
15    print("")
16    time.sleep(0.5)

ICM20948 Acceleration data rate test

Example showing ICM20948 sensor cycling between two acceleration data rates

examples/icm20x_icm20948_accel_data_rate_test.py
 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
10icm = ICM20948(i2c)
11
12# Cycle between two data rates
13# Best viewed in the Mu serial plotter where you can see how
14# the data rate affects the resolution of the data
15while True:
16    icm.accelerometer_data_rate_divisor = 0  # minimum
17    print("Data Rate:", icm.accelerometer_data_rate)
18    time.sleep(2)
19    for i in range(cycles):
20        print(icm.acceleration)
21
22    icm.accelerometer_data_rate_divisor = 4095  # maximum
23    print("Data Rate:", icm.accelerometer_data_rate)
24    time.sleep(2)
25    for i in range(cycles):
26        print(icm.acceleration)

ICM20948 Gyro data rate test

Example showing ICM20948 sensor cycling between two gyro data rates

examples/icm20x_icm20948_gyro_data_rate_test.py
 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
10icm = ICM20948(i2c)
11
12# Cycle between two data rates
13# Best viewed in the Mu serial plotter where you can see how
14# the data rate affects the resolution of the data
15while True:
16    icm.gyro_data_rate_divisor = 0  # minimum
17    print("Data Rate:", icm.gyro_data_rate)
18    time.sleep(2)
19    for i in range(cycles):
20        print(icm.gyro)
21
22    icm.gyro_data_rate_divisor = 255  # maximum
23    print("Data Rate:", icm.gyro_data_rate)
24    time.sleep(2)
25    for i in range(cycles):
26        print(icm.gyro)

ICM20948 Magnetic data rate test

Example showing ICM20948 sensor cycling between two magnetic data rates

examples/icm20x_icm20948_mag_data_rate_test.py
 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
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.magnetometer_data_rate = MagDataRate.RATE_100HZ
18    for i in range(cycles):
19        print(icm.magnetic)
20    time.sleep(0.3)
21    icm.magnetometer_data_rate = MagDataRate.RATE_10HZ
22    for i in range(cycles):
23        print(icm.magnetic)
24    time.sleep(0.3)