Chain IMU

This library is the driver for Chain IMU.

Support the following products:

Chain IMU

UiFlow2 Example

IMU sensor display

Open the m5core_chain_imu_basic_example.m5f2 project in UiFlow2.

Connect Chain IMU as the first device on M5Stack Basic UART2 (TX=21, RX=22). The screen has a centered “Chain IMU” title, three X/Y/Z columns for acceleration (m/s2) and angular velocity (rad/s), and a temperature reading (degrees Celsius) at the bottom. Readings refresh every 100 ms. Acceleration and angular velocity are rounded to two decimal places, and temperature to one decimal place.

UiFlow2 Code Block:

m5core_chain_imu_basic_example.png

MicroPython Example

IMU sensor display

Connect Chain IMU as the first device on M5Stack Basic UART2 (TX=21, RX=22). The screen has a centered “Chain IMU” title, three X/Y/Z columns for acceleration (m/s2) and angular velocity (rad/s), and a temperature reading (degrees Celsius) at the bottom. Readings refresh every 100 ms. Acceleration and angular velocity are rounded to two decimal places, and temperature to one decimal place.

MicroPython Code Block:

  1# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
  2#
  3# SPDX-License-Identifier: MIT
  4
  5import os, sys, io
  6import M5
  7from M5 import *
  8from chain import ChainBus
  9from chain import IMUChain
 10import time
 11
 12
 13title = None
 14accel_caption = None
 15accel_unit = None
 16x_caption = None
 17accel_x = None
 18y_caption = None
 19accel_y = None
 20z_caption = None
 21accel_z = None
 22gyro_caption = None
 23gyro_unit = None
 24gyro_x = None
 25gyro_y = None
 26gyro_z = None
 27temperature = None
 28bus2 = None
 29chain_imu_0 = None
 30accel = None
 31gyro = None
 32last_time = None
 33
 34
 35def setup():
 36    global \
 37        title, \
 38        accel_caption, \
 39        accel_unit, \
 40        x_caption, \
 41        accel_x, \
 42        y_caption, \
 43        accel_y, \
 44        z_caption, \
 45        accel_z, \
 46        gyro_caption, \
 47        gyro_unit, \
 48        gyro_x, \
 49        gyro_y, \
 50        gyro_z, \
 51        temperature, \
 52        bus2, \
 53        chain_imu_0, \
 54        accel, \
 55        gyro, \
 56        last_time
 57
 58    M5.begin()
 59    Widgets.setRotation(1)
 60    Widgets.fillScreen(0x10171D)
 61    title = Widgets.Label("Chain IMU", 112, 8, 1.0, 0x4CD7D0, 0x10171D, Widgets.FONTS.Montserrat18)
 62    accel_caption = Widgets.Label(
 63        "Acceleration", 12, 43, 1.0, 0x8FA1AD, 0x10171D, Widgets.FONTS.Montserrat14
 64    )
 65    accel_unit = Widgets.Label(
 66        "m/s2", 200, 43, 1.0, 0x63A8FF, 0x10171D, Widgets.FONTS.Montserrat14
 67    )
 68    x_caption = Widgets.Label("X", 12, 65, 1.0, 0xFFB454, 0x10171D, Widgets.FONTS.Montserrat14)
 69    accel_x = Widgets.Label("--.--", 12, 86, 1.0, 0xF2F6F8, 0x10171D, Widgets.FONTS.Montserrat24)
 70    y_caption = Widgets.Label("Y", 113, 65, 1.0, 0x63D08C, 0x10171D, Widgets.FONTS.Montserrat14)
 71    accel_y = Widgets.Label("--.--", 113, 86, 1.0, 0xF2F6F8, 0x10171D, Widgets.FONTS.Montserrat24)
 72    z_caption = Widgets.Label("Z", 214, 65, 1.0, 0x63A8FF, 0x10171D, Widgets.FONTS.Montserrat14)
 73    accel_z = Widgets.Label("--.--", 214, 86, 1.0, 0xF2F6F8, 0x10171D, Widgets.FONTS.Montserrat24)
 74    gyro_caption = Widgets.Label(
 75        "Angular velocity", 12, 131, 1.0, 0x8FA1AD, 0x10171D, Widgets.FONTS.Montserrat14
 76    )
 77    gyro_unit = Widgets.Label(
 78        "rad/s", 200, 131, 1.0, 0x4CD7D0, 0x10171D, Widgets.FONTS.Montserrat14
 79    )
 80    gyro_x = Widgets.Label("--.--", 12, 157, 1.0, 0x4CD7D0, 0x10171D, Widgets.FONTS.Montserrat24)
 81    gyro_y = Widgets.Label("--.--", 113, 157, 1.0, 0x4CD7D0, 0x10171D, Widgets.FONTS.Montserrat24)
 82    gyro_z = Widgets.Label("--.--", 214, 157, 1.0, 0x4CD7D0, 0x10171D, Widgets.FONTS.Montserrat24)
 83    temperature = Widgets.Label(
 84        "Temperature  --.- C", 12, 212, 1.0, 0xFFB454, 0x10171D, Widgets.FONTS.Montserrat14
 85    )
 86
 87    bus2 = ChainBus(2, tx=21, rx=22)
 88    chain_imu_0 = IMUChain(bus2, 1)
 89    title.setCursor(
 90        x=(int((320 - (M5.Display.textWidth("Chain IMU", Widgets.FONTS.Montserrat18))) / 2)), y=8
 91    )
 92    last_time = 0
 93
 94
 95def loop():
 96    global \
 97        title, \
 98        accel_caption, \
 99        accel_unit, \
100        x_caption, \
101        accel_x, \
102        y_caption, \
103        accel_y, \
104        z_caption, \
105        accel_z, \
106        gyro_caption, \
107        gyro_unit, \
108        gyro_x, \
109        gyro_y, \
110        gyro_z, \
111        temperature, \
112        bus2, \
113        chain_imu_0, \
114        accel, \
115        gyro, \
116        last_time
117    M5.update()
118    if (time.ticks_diff((time.ticks_ms()), last_time)) >= 100:
119        accel = chain_imu_0.read_accel()
120        gyro = chain_imu_0.read_gyro()
121        accel_x.setText(str(round(accel[0], 2)))
122        gyro_x.setText(str(round(gyro[0], 2)))
123        accel_y.setText(str(round(accel[1], 2)))
124        gyro_y.setText(str(round(gyro[1], 2)))
125        accel_z.setText(str(round(accel[2], 2)))
126        gyro_z.setText(str(round(gyro[2], 2)))
127        temperature.setText(
128            str(
129                (
130                    str((str("Temperature  ") + str((round(chain_imu_0.read_temperature(), 1)))))
131                    + str(" C")
132                )
133            )
134        )
135        last_time = time.ticks_ms()
136    time.sleep_ms(10)
137
138
139if __name__ == "__main__":
140    try:
141        setup()
142        while True:
143            loop()
144    except (Exception, KeyboardInterrupt) as e:
145        try:
146            bus2.deinit()
147            from utility import print_error_msg
148
149            print_error_msg(e)
150        except ImportError:
151            print("please update to latest firmware")

API

IMUChain

class chain.imu.IMUChain(bus, device_id)

Bases: KeyChain

Driver for Chain IMU.

Create a IMUChain on an existing ChainBus. Registration performs no device configuration. The caller owns the bus and must close it after all devices are detached.

Parameters:
  • bus (ChainBus) – An open shared ChainBus instance.

  • device_id (int) – Enumerated Chain position, 1..254.

Returns:

A registered IMUChain object.

Return type:

IMUChain

UiFlow2 Code Block:

init.png

MicroPython Code Block:

device = IMUChain(bus, 1)
ACCEL_RANGE_2G = 2
ACCEL_RANGE_4G = 4
ACCEL_RANGE_8G = 8
ACCEL_RANGE_16G = 16
GYRO_RANGE_125DPS = 125
GYRO_RANGE_250DPS = 250
GYRO_RANGE_500DPS = 500
GYRO_RANGE_1000DPS = 1000
GYRO_RANGE_2000DPS = 2000
ODR_POWER_DOWN = 0
ODR_1_6HZ = 1.6
ODR_12_5HZ = 12.5
ODR_26HZ = 26
ODR_52HZ = 52
ODR_104HZ = 104
ODR_208HZ = 208
ODR_416HZ = 416
ODR_833HZ = 833
ODR_1660HZ = 1660
ODR_3330HZ = 3330
ODR_6660HZ = 6660
ACCEL_RANGES = (2, 4, 8, 16)
GYRO_RANGES = (125, 250, 500, 1000, 2000)
ACCEL_ODR_VALUES = (0, 1.6, 12.5, 26, 52, 104, 208, 416, 833, 1660, 3330, 6660)
GYRO_ODR_VALUES = (0, 12.5, 26, 52, 104, 208, 416, 833, 1660, 3330, 6660)
ALARM_NONE = 0
ALARM_ACCEL = 1
ALARM_GYRO = 2
ALARM_TEMPERATURE = 4
ALARM_ALL = 7
EVENT_ACCEL_CLEAR = 16
EVENT_ACCEL_TRIGGER = 17
EVENT_GYRO_CLEAR = 32
EVENT_GYRO_TRIGGER = 33
EVENT_TEMPERATURE_HIGH_CLEAR = 48
EVENT_TEMPERATURE_HIGH = 49
EVENT_TEMPERATURE_LOW = 50
EVENT_TEMPERATURE_LOW_CLEAR = 51
read_accel()

Read three-axis acceleration in m/s^2, using standard gravity 9.80665 m/s^2.

Returns:

Tuple (x, y, z) of floats in m/s^2, resolution 0.00980665 m/s^2.

Return type:

tuple

UiFlow2 Code Block:

read_accel.png

MicroPython Code Block:

x, y, z = device.read_accel()
read_gyro()

Read the latest IMU sample. Firmware refreshes the sensor but can return the previous sample if that refresh fails.

Returns:

Tuple (x, y, z) of floats in rad/s, resolution pi/180 rad/s (about 0.0174533 rad/s).

Return type:

tuple

UiFlow2 Code Block:

read_gyro.png

MicroPython Code Block:

xyz = device.read_gyro()
read_accel_raw()

Read the latest IMU sample. Firmware refreshes the sensor but can return the previous sample if that refresh fails.

Returns:

Tuple (x, y, z) of signed 16-bit raw sensor counts.

Return type:

tuple

UiFlow2 Code Block:

read_accel_raw.png

MicroPython Code Block:

xyz = device.read_accel_raw()
read_gyro_raw()

Read the latest IMU sample. Firmware refreshes the sensor but can return the previous sample if that refresh fails.

Returns:

Tuple (x, y, z) of signed 16-bit raw sensor counts.

Return type:

tuple

UiFlow2 Code Block:

read_gyro_raw.png

MicroPython Code Block:

xyz = device.read_gyro_raw()
read_temperature()

Read IMU temperature. Firmware clamps temperatures below zero to 0 degrees Celsius; negative temperatures cannot be recovered through this command.

Returns:

Temperature in degrees Celsius, 0..655.35, resolution 0.01 degrees.

Return type:

float

UiFlow2 Code Block:

read_temperature.png

MicroPython Code Block:

temperature = device.read_temperature()
set_accel_range(accel_scale)

Set acceleration full-scale range. The setting is volatile.

Parameters:

accel_scale (int or float) – An ACCEL_RANGE_* constant in ACCEL_RANGES, in g (2, 4, 8 or 16). Samples from read_accel() remain in m/s^2.

Returns:

No return value.

Return type:

None

UiFlow2 Code Block:

set_accel_range.png

MicroPython Code Block:

device.set_accel_range(IMUChain.ACCEL_RANGE_2G)
get_accel_range()

Get acceleration full-scale range.

Returns:

An ACCEL_RANGE_* constant in ACCEL_RANGES, in g (2, 4, 8 or 16). Samples from read_accel() remain in m/s^2.

Return type:

int or float

UiFlow2 Code Block:

get_accel_range.png

MicroPython Code Block:

value = device.get_accel_range()
set_gyro_range(gyro_scale)

Set the gyroscope full-scale range. The setting is volatile.

Parameters:

gyro_scale (int) – A GYRO_RANGE_* constant in GYRO_RANGES, in degrees/s (125, 250, 500, 1000 or 2000). Samples from read_gyro() remain in rad/s.

Returns:

No return value.

Return type:

None

UiFlow2 Code Block:

set_gyro_range.png

MicroPython Code Block:

device.set_gyro_range(IMUChain.GYRO_RANGE_500DPS)
get_gyro_range()

Get the gyroscope full-scale range.

Returns:

Full-scale magnitude in degrees per second.

Return type:

int

UiFlow2 Code Block:

get_gyro_range.png

MicroPython Code Block:

value = device.get_gyro_range()
set_accel_odr(accel_odr)

Set acceleration sampling rate. The setting is volatile.

Parameters:

accel_odr (int or float) – An ODR_* constant in ACCEL_ODR_VALUES, in Hz; ODR_POWER_DOWN (0) disables this channel.

Returns:

No return value.

Return type:

None

UiFlow2 Code Block:

set_accel_odr.png

MicroPython Code Block:

device.set_accel_odr(IMUChain.ODR_104HZ)
get_accel_odr()

Get acceleration sampling rate.

Returns:

An ODR_* constant in ACCEL_ODR_VALUES, in Hz; ODR_POWER_DOWN (0) disables this channel.

Return type:

int or float

UiFlow2 Code Block:

get_accel_odr.png

MicroPython Code Block:

value = device.get_accel_odr()
set_gyro_odr(gyro_odr)

Set gyroscope sampling rate. The setting is volatile.

Parameters:

gyro_odr (int or float) – An ODR_* constant in GYRO_ODR_VALUES, in Hz; ODR_POWER_DOWN (0) disables this channel.

Returns:

No return value.

Return type:

None

UiFlow2 Code Block:

set_gyro_odr.png

MicroPython Code Block:

device.set_gyro_odr(IMUChain.ODR_104HZ)
get_gyro_odr()

Get gyroscope sampling rate.

Returns:

An ODR_* constant in GYRO_ODR_VALUES, in Hz; ODR_POWER_DOWN (0) disables this channel.

Return type:

int or float

UiFlow2 Code Block:

get_gyro_odr.png

MicroPython Code Block:

value = device.get_gyro_odr()
set_alarm_mode(mode)

Select the enabled alarm sources. Zero disables all alarms. The setting is volatile. Bits: 1 acceleration, 2 gyroscope, 4 temperature.

Parameters:

mode (int) – Alarm bitmask, 0..7; combine ALARM_* bits with |, use ALARM_NONE to disable or ALARM_ALL to enable all sources.

Returns:

No return value.

Return type:

None

UiFlow2 Code Block:

set_alarm_mode.png

MicroPython Code Block:

device.set_alarm_mode(IMUChain.ALARM_ALL)
get_alarm_mode()

Get the enabled alarm sources.

Returns:

Alarm bitmask, 0..7; combine ALARM_* bits with |, use ALARM_NONE to disable or ALARM_ALL to enable all sources.

Return type:

int

UiFlow2 Code Block:

get_alarm_mode.png

MicroPython Code Block:

mode = device.get_alarm_mode()
set_motion_alarm_thresh(accel_mg, gyro_dps)

Set motion delta thresholds. Firmware compares each axis against its moving reference and debounces transitions over five samples. The setting is volatile.

Parameters:
  • accel_mg (int) – Acceleration delta in mg, 0..255.

  • gyro_dps (int) – Angular velocity delta in degrees per second, 0..255.

Returns:

No return value.

Return type:

None

UiFlow2 Code Block:

set_motion_alarm_thresh.png

MicroPython Code Block:

device.set_motion_alarm_thresh(100, 20)
get_motion_alarm_thresh()

Get motion delta thresholds.

Returns:

Tuple (accel_mg, gyro_dps), each 0..255.

Return type:

tuple

UiFlow2 Code Block:

get_motion_alarm_thresh.png

MicroPython Code Block:

accel_mg, gyro_dps = device.get_motion_alarm_thresh()
set_temperature_alarm_thresh(low, high)

Set the temperature alarm window. The setting is volatile.

Parameters:
  • low (int) – Temperature in degrees Celsius. Range: 0..255; low must be less than high.

  • high (int) – Temperature in degrees Celsius. Range: 0..255; low must be less than high.

Returns:

No return value.

Return type:

None

UiFlow2 Code Block:

set_temperature_alarm_thresh.png

MicroPython Code Block:

device.set_temperature_alarm_thresh(10, 40)
get_temperature_alarm_thresh()

Get the temperature alarm window.

Returns:

Tuple (low, high). Temperature in degrees Celsius.

Return type:

tuple

UiFlow2 Code Block:

get_temperature_alarm_thresh.png

MicroPython Code Block:

low, high = device.get_temperature_alarm_thresh()
deinit()

Detach the device and release all event callbacks; the shared bus stays open.

Returns:

No return value.

Return type:

None

UiFlow2 Code Block:

deinit.png

set_event_callback(event=None, callback=None)

Register a specific IMU alarm or recovery callback.

Use EVENT_ACCEL_TRIGGER/CLEAR, EVENT_GYRO_TRIGGER/CLEAR, or EVENT_TEMPERATURE_HIGH/LOW/HIGH_CLEAR/LOW_CLEAR. Each handler receives its selected event constant. None removes only that event handler. Registration does not change alarm reporting. Enable alarms afterwards. State tracking starts at zero when the first source is registered. Recovery requires a prior alarm report, except explicit low recovery. Legacy source selectors and the single-callback form receive raw status. set_event_callback(None) removes all handlers.

Parameters:
  • event (int or callable or None) – An EVENT_* constant, legacy callback or None. Default: None.

  • callback (callable or None) – Handler receiving the selected event constant. Default: None.

Returns:

No return value.

Return type:

None

UiFlow2 Code Block:

event_callback.png

MicroPython Code Block:

def on_accel(event):
    print(event)

device.set_event_callback(IMUChain.EVENT_ACCEL_TRIGGER, on_accel)