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
 5
 6import board
 7
 8import adafruit_icm20x
 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
12icm = adafruit_icm20x.ICM20649(i2c, address=0x69)
13
14while True:
15    print("Acceleration: X:{:.2f}, Y: {:.2f}, Z: {:.2f} m/s^2".format(*icm.acceleration))
16    print("Gyro X:{:.2f}, Y: {:.2f}, Z: {:.2f} rads/s".format(*icm.gyro))
17    print("")
18    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
 5
 6import board
 7
 8from adafruit_icm20x import ICM20649, AccelRange, GyroRange
 9
10
11def printNewMax(value, current_max, axis):
12    if value > current_max:
13        current_max = value
14        print(axis, "Max:", current_max)
15    return current_max
16
17
18# pylint:disable=no-member
19i2c = board.I2C()  # uses board.SCL and board.SDA
20# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
21
22ism = ICM20649(i2c, address=0x69)
23
24ism.accelerometer_range = AccelRange.RANGE_30G
25print(f"Accelerometer range set to: {AccelRange.string[ism.accelerometer_range]:d} g")
26
27ism.gyro_range = GyroRange.RANGE_500_DPS
28print(f"Gyro range set to: {GyroRange.string[ism.gyro_range]:d} DPS")
29
30ax_max = ay_max = az_max = 0
31gx_max = gy_max = gz_max = 0
32
33ism.gyro_data_rate = 1100
34ism.accelerometer_data_rate = 1125
35st = time.monotonic()
36while time.monotonic() - st < 0.250:
37    print(
38        "Accel X:%.2f Y:%.2f Z:%.2f ms^2 Gyro X:%.2f Y:%.2f Z:%.2f degrees/s"
39        % (ism.acceleration + ism.gyro)
40    )
41
42#     acceleration = ism.acceleration
43#     # ax_max = printNewMax(acceleration[0], ax_max, "AX")
44#     # ay_max = printNewMax(acceleration[1], ay_max, "AY")
45#     # az_max = printNewMax(acceleration[2], az_max, "AZ")
46
47#     gyro = ism.gyro
48#     # gx_max = printNewMax(gyro[0], gx_max, "GX")
49#     # gy_max = printNewMax(gyro[1], gy_max, "GY")
50#     # gz_max = printNewMax(gyro[2], gz_max, "GZ")

ICM20649 data rate test

Test using all the ICM20649 sensor capabilities

examples/examples/icm20x_icm20649_data_rate_test.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5
 6import board
 7
 8from adafruit_icm20x import ICM20649, AccelRange, GyroRange
 9
10i2c = board.I2C()  # uses board.SCL and board.SDA
11
12ism = ICM20649(i2c, address=0x69)
13
14ism.accelerometer_range = AccelRange.RANGE_4G
15print(f"Accelerometer range set to: {AccelRange.string[ism.accelerometer_range]:d} g")
16
17ism.gyro_range = GyroRange.RANGE_500_DPS
18print(f"Gyro range set to: {GyroRange.string[ism.gyro_range]:d} DPS")
19
20ism.gyro_data_rate = 1100  # 1100 max
21ism.accelerometer_data_rate = 1125  # 1125 max
22
23print(f"Gyro rate: {ism.gyro_data_rate:f}")
24print(f"Accel rate: {ism.accelerometer_data_rate:f}")
25
26ism.gravity = 9.8
27
28previousTime = time.monotonic_ns()
29while True:
30    if ism.data_ready:
31        currentTime = time.monotonic_ns()
32        print("\033[2J")
33        print(
34            "Accel X:{:5.2f} Y:{:5.2f} Z:{:5.2f} ms^2 "
35            "Gyro X:{:8.3f} Y:{:8.3f} Z:{:8.3f} degrees/s Sample Rate: {:8.1f} Hz".format(
36                *ism.acceleration, *ism.gyro, (1 / (currentTime - previousTime))
37            )
38        )
39        previousTime = currentTime

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
 5
 6import board
 7
 8import adafruit_icm20x
 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
12icm = adafruit_icm20x.ICM20948(i2c)
13
14while True:
15    print("Acceleration: X:{:.2f}, Y: {:.2f}, Z: {:.2f} m/s^2".format(*icm.acceleration))
16    print("Gyro X:{:.2f}, Y: {:.2f}, Z: {:.2f} rads/s".format(*icm.gyro))
17    print("Magnetometer X:{:.2f}, Y: {:.2f}, Z: {:.2f} uT".format(*icm.magnetic))
18    print("")
19    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
 5
 6import board
 7
 8from adafruit_icm20x import ICM20948
 9
10cycles = 200
11i2c = board.I2C()  # uses board.SCL and board.SDA
12# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
13icm = ICM20948(i2c)
14
15# Cycle between two data rates
16# Best viewed in the Mu serial plotter where you can see how
17# the data rate affects the resolution of the data
18while True:
19    icm.accelerometer_data_rate_divisor = 0  # minimum
20    print("Data Rate:", icm.accelerometer_data_rate)
21    time.sleep(2)
22    for i in range(cycles):
23        print(icm.acceleration)
24
25    icm.accelerometer_data_rate_divisor = 4095  # maximum
26    print("Data Rate:", icm.accelerometer_data_rate)
27    time.sleep(2)
28    for i in range(cycles):
29        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
 5
 6import board
 7
 8from adafruit_icm20x import ICM20948
 9
10cycles = 200
11i2c = board.I2C()  # uses board.SCL and board.SDA
12# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
13icm = ICM20948(i2c)
14
15# Cycle between two data rates
16# Best viewed in the Mu serial plotter where you can see how
17# the data rate affects the resolution of the data
18while True:
19    icm.gyro_data_rate_divisor = 0  # minimum
20    print("Data Rate:", icm.gyro_data_rate)
21    time.sleep(2)
22    for i in range(cycles):
23        print(icm.gyro)
24
25    icm.gyro_data_rate_divisor = 255  # maximum
26    print("Data Rate:", icm.gyro_data_rate)
27    time.sleep(2)
28    for i in range(cycles):
29        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
 6
 7import board
 8
 9from adafruit_icm20x import ICM20948, MagDataRate
10
11cycles = 200
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
14icm = ICM20948(i2c)
15
16# Cycle between two data rates
17# Best viewed in the Mu serial plotter where you can see how
18# the data rate affects the resolution of the data
19while True:
20    icm.magnetometer_data_rate = MagDataRate.RATE_100HZ
21    for i in range(cycles):
22        print(icm.magnetic)
23    time.sleep(0.3)
24    icm.magnetometer_data_rate = MagDataRate.RATE_10HZ
25    for i in range(cycles):
26        print(icm.magnetic)
27    time.sleep(0.3)

DisplayIO Simpletest

This is a simple test for boards with built-in display.

examples/icm20x_icm20948_displayio_simpletest.py
 1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
 2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
 3#
 4# SPDX-License-Identifier: MIT
 5
 6import time
 7
 8import board
 9from adafruit_display_text.bitmap_label import Label
10from displayio import Group
11from terminalio import FONT
12
13import adafruit_icm20x
14
15# Simple demo of using the built-in display.
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C()  # uses board.SCL and board.SDA
20icm = adafruit_icm20x.ICM20948(i2c)
21
22# Create Label(s) to show the readings. If you have a very small
23# display you may need to change to scale=1.
24display_output_label = Label(FONT, text="", scale=2)
25
26# place the label(s) in the middle of the screen with anchored positioning
27display_output_label.anchor_point = (0, 0)
28display_output_label.anchored_position = (
29    4,
30    board.DISPLAY.height // 2 - 60,
31)
32
33# add the label(s) to the main_group
34main_group.append(display_output_label)
35
36# set the main_group as the root_group of the built-in DISPLAY
37board.DISPLAY.root_group = main_group
38
39# begin main loop
40while True:
41    # update the text of the label(s) to show the sensor readings
42    acc_x, acc_y, acc_z = icm.acceleration
43    display_output_label.text = (
44        f"Acceleration\n"
45        + f"x: {acc_x:.1f} m/s^2\n"
46        + f"y: {acc_y:.1f} m/s^2\n"
47        + f"z: {acc_z:.1f} m/s^2"
48    )
49    # wait for a bit
50    time.sleep(0.5)