English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

Chain IMU Tutorial

1. Preparation

2. Example Program

Use the Chain Bridge connector to connect Chain Captain and Chain IMU. Pay attention to the connector orientation: the triangular arrow should point outward from the main controller, as shown below:

Data Reading

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
#include "M5Chain.h"
#include "M5Unified.h"

#define TXD_PIN GPIO_NUM_18
#define RXD_PIN GPIO_NUM_17

Chain M5Chain;
M5Canvas canvas(&M5.Display);
uint8_t imu_id = 0;
uint8_t op_status = 0;

// Find the first IMU node on the Chain bus.
bool findIMU()
{
    uint16_t count = 0;
    if (M5Chain.getDeviceNum(&count) != CHAIN_OK) {
        Serial.println("Failed to get Chain device count");
        return false;
    }
    Serial.printf("Chain device count: %u\r\n", count);
    if (count == 0) {
        Serial.println("No Chain devices found");
        return false;
    }

    device_info_t infos[8];
    device_list_t list = {count < 8 ? count : 8, infos};
    if (!M5Chain.getDeviceList(&list)) {
        Serial.println("Failed to get Chain device list");
        return false;
    }

    for (uint16_t i = 0; i < list.count; i++) {
        if (list.devices[i].device_type == CHAIN_IMU_TYPE_CODE) {
            imu_id = list.devices[i].id;
            Serial.printf("IMU found, id=%u\r\n", imu_id);
            return true;
        }
    }
    Serial.println("IMU not found");
    return false;
}

void showMessage(const char *text)
{
    canvas.fillSprite(TFT_BLACK);
    canvas.setTextDatum(middle_center);
    canvas.setTextColor(TFT_WHITE, TFT_BLACK);
    canvas.setFont(&fonts::FreeSansBold12pt7b);
    canvas.drawString(text, 120, 120);
    canvas.pushSprite(0, 0);
}

void setup()
{
    delay(520);
    // Initialize Chain Captain; Chain 5V is enabled by default.
    M5.begin();
    M5.Display.setBrightness(255);
    canvas.createSprite(M5.Display.width(), M5.Display.height());
    Serial.begin(115200);
    Serial.println("Chain IMU data demo start");
    Serial.printf("Chain UART: RX=GPIO%d, TX=GPIO%d\r\n", RXD_PIN, TXD_PIN);

    // Start the Chain bus on UART1.
    M5Chain.begin(&Serial1, 115200, RXD_PIN, TXD_PIN);

    if (!findIMU()) {
        Serial.println("IMU init failed: device not found");
        showMessage("IMU not found");
        while (1) {
            delay(100);
        }
    }

    // Configure accelerometer and gyroscope ranges and output rates.
    bool config_ok =
        M5Chain.setIMUAccelRange(imu_id, CHAIN_IMU_ACCEL_RANGE_4G, &op_status) == CHAIN_OK &&
        op_status == 1 &&
        M5Chain.setIMUGyroRange(imu_id, CHAIN_IMU_GYRO_RANGE_500DPS, &op_status) == CHAIN_OK &&
        op_status == 1 &&
        M5Chain.setIMUAccelODR(imu_id, CHAIN_IMU_ACCEL_ODR_104HZ, &op_status) == CHAIN_OK &&
        op_status == 1 &&
        M5Chain.setIMUGyroODR(imu_id, CHAIN_IMU_GYRO_ODR_104HZ, &op_status) == CHAIN_OK &&
        op_status == 1;

    if (!config_ok) {
        Serial.printf("IMU config failed, operation status=%u\r\n", op_status);
        showMessage("Config failed");
        while (1) {
            delay(100);
        }
    }

    Serial.println("IMU config: range=4g/500dps, ODR=104Hz");
    Serial.println("IMU ready");
    showMessage("IMU ready");
}

void loop()
{
    int16_t ax = 0, ay = 0, az = 0;
    int16_t gx = 0, gy = 0, gz = 0;
    uint16_t temp = 0;

    if (M5Chain.getIMUAccelData(imu_id, &ax, &ay, &az) == CHAIN_OK &&
        M5Chain.getIMUGyroData(imu_id, &gx, &gy, &gz) == CHAIN_OK &&
        M5Chain.getIMUTemperature(imu_id, &temp) == CHAIN_OK) {
        Serial.printf("IMU data: A=(%d,%d,%d) mg, G=(%d,%d,%d) dps, T=%.2f C\r\n",
                      ax, ay, az, gx, gy, gz, temp / 100.0f);

        // Draw a compact dashboard with a clear hierarchy.
        canvas.fillSprite(TFT_BLACK);
        canvas.setTextDatum(middle_center);
        canvas.setFont(&fonts::FreeSansBold12pt7b);
        canvas.setTextColor(TFT_CYAN, TFT_BLACK);
        canvas.drawString("Chain IMU", 120, 24);
        canvas.setFont(&fonts::FreeSans9pt7b);
        canvas.setTextColor(TFT_LIGHTGREY, TFT_BLACK);
        canvas.drawString("ACCEL (mg)", 120, 54);
        canvas.setFont(&fonts::FreeSansBold12pt7b);
        canvas.setTextColor(TFT_WHITE, TFT_BLACK);
        canvas.drawString(String(ax) + " / " + String(ay) + " / " + String(az), 120, 82);
        canvas.setFont(&fonts::FreeSans9pt7b);
        canvas.setTextColor(TFT_LIGHTGREY, TFT_BLACK);
        canvas.drawString("GYRO (dps)", 120, 112);
        canvas.setFont(&fonts::FreeSansBold12pt7b);
        canvas.setTextColor(TFT_WHITE, TFT_BLACK);
        canvas.drawString(String(gx) + " / " + String(gy) + " / " + String(gz), 120, 140);
        canvas.setFont(&fonts::FreeSans9pt7b);
        canvas.setTextColor(TFT_LIGHTGREY, TFT_BLACK);
        canvas.drawString("TEMP", 120, 174);
        canvas.setFont(&fonts::FreeSansBold12pt7b);
        canvas.setTextColor(TFT_GREEN, TFT_BLACK);
        canvas.drawString(String(temp / 100.0f, 2) + " C", 120, 202);
        canvas.pushSprite(0, 0);
    } else {
        Serial.println("IMU data read failed");
        showMessage("Read failed");
    }

    delay(50);
}

The screen displays three-axis acceleration (mg), three-axis angular velocity (dps), and sensor temperature, while the serial monitor outputs device information, configuration results, and real-time data.

Threshold Events

The following example enables acceleration, angular velocity, and temperature events simultaneously and sets their thresholds to 40 mg, 40 dps, and 0~30°C, respectively.

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
#include "M5Chain.h"
#include "M5Unified.h"

#define TXD_PIN GPIO_NUM_18
#define RXD_PIN GPIO_NUM_17

Chain M5Chain;
M5Canvas canvas(&M5.Display);
uint8_t imu_id = 0;
uint8_t op_status = 0;
uint8_t last_status = 0;
bool event_pending = false;
uint32_t last_draw_ms = 0;
const uint32_t event_hold_ms = 250;  // Minimum time between event screen updates.
const char *const temp_text[] = {"TEMP HIGH RELEASE", "TEMP HIGH ACTIVE",
                                 "TEMP LOW ACTIVE", "TEMP LOW RELEASE"};

// Find the first IMU node on the Chain bus.
bool findIMU()
{
    uint16_t count = 0;
    if (M5Chain.getDeviceNum(&count) != CHAIN_OK) {
        Serial.println("Failed to get Chain device count");
        return false;
    }
    Serial.printf("Chain device count: %u\r\n", count);
    if (count == 0) {
        Serial.println("No Chain devices found");
        return false;
    }

    device_info_t infos[8];
    device_list_t list = {count < 8 ? count : 8, infos};
    if (!M5Chain.getDeviceList(&list)) {
        Serial.println("Failed to get Chain device list");
        return false;
    }

    for (uint16_t i = 0; i < list.count; i++) {
        if (list.devices[i].device_type == CHAIN_IMU_TYPE_CODE) {
            imu_id = list.devices[i].id;
            Serial.printf("IMU found, id=%u\r\n", imu_id);
            return true;
        }
    }
    Serial.println("IMU not found");
    return false;
}

void showMessage(const char *text)
{
    canvas.fillSprite(TFT_BLACK);
    canvas.setTextDatum(middle_center);
    canvas.setTextColor(TFT_WHITE, TFT_BLACK);
    canvas.setFont(&fonts::FreeSansBold12pt7b);
    canvas.drawString(text, 120, 120);
    canvas.pushSprite(0, 0);
}

void drawEvent(uint8_t status)
{
    bool accel = status & CHAIN_IMU_EVENT_STATUS_ACCEL_MASK;
    bool gyro  = status & CHAIN_IMU_EVENT_STATUS_GYRO_MASK;
    uint8_t temp = (status & CHAIN_IMU_EVENT_STATUS_TEMPERATURE_MASK) >>
                   CHAIN_IMU_EVENT_STATUS_TEMPERATURE_SHIFT;

    canvas.fillSprite(TFT_BLACK);
    canvas.setTextDatum(middle_center);
    canvas.setFont(&fonts::FreeSansBold12pt7b);
    canvas.setTextColor(TFT_CYAN, TFT_BLACK);
    canvas.drawString("Chain IMU Event", 120, 32);
    canvas.setTextColor(accel ? TFT_BLUE : TFT_GREEN, TFT_BLACK);
    canvas.drawString(accel ? "ACC ACTIVE" : "ACC RELEASE", 120, 88);
    canvas.setTextColor(gyro ? TFT_BLUE : TFT_GREEN, TFT_BLACK);
    canvas.drawString(gyro ? "GYRO ACTIVE" : "GYRO RELEASE", 120, 132);
    canvas.setFont(&fonts::FreeSansBold9pt7b);
    canvas.setTextColor((temp == 1 || temp == 2) ? TFT_RED : TFT_GREEN, TFT_BLACK);
    canvas.drawString(temp_text[temp], 120, 176);
    canvas.pushSprite(0, 0);
}

void setup()
{
    delay(520);
    M5.begin();
    M5.Display.setBrightness(255);
    canvas.createSprite(M5.Display.width(), M5.Display.height());
    Serial.begin(115200);
    Serial.println("Chain IMU event demo start");
    Serial.printf("Chain UART: RX=GPIO%d, TX=GPIO%d\r\n", RXD_PIN, TXD_PIN);

    // Start the Chain bus on UART1.
    M5Chain.begin(&Serial1, 115200, RXD_PIN, TXD_PIN);

    if (!findIMU()) {
        Serial.println("IMU event init failed: device not found");
        showMessage("IMU not found");
        while (1) {
            delay(100);
        }
    }

    // Enable acceleration, gyro, and temperature event reporting.
    uint8_t mode = CHAIN_IMU_EVENT_MODE_ACCEL | CHAIN_IMU_EVENT_MODE_GYRO |
                   CHAIN_IMU_EVENT_MODE_TEMPERATURE;
    bool config_ok =
        M5Chain.setIMUEventTriggerMode(imu_id, mode, &op_status) == CHAIN_OK && op_status == 1 &&
        M5Chain.setIMU6AxisWarningThreshold(imu_id, 40, 40, &op_status) == CHAIN_OK &&
        op_status == 1 &&
        M5Chain.setIMUTemperatureWarningThreshold(imu_id, 0, 30, &op_status) == CHAIN_OK &&
        op_status == 1;

    if (!config_ok) {
        Serial.printf("IMU event config failed, operation status=%u\r\n", op_status);
        showMessage("Config failed");
        while (1) {
            delay(100);
        }
    }

    Serial.println("IMU event config: accel=40 mg, gyro=40 dps, temp=0-30 C");
    Serial.println("IMU event ready");
    showMessage("Event ready");
}

void loop()
{
    uint8_t status = 0;

    // Keep the serial log complete while limiting screen redraws.
    while (M5Chain.getIMUEventTrigger(imu_id, &status)) {
        last_status = status;
        event_pending = true;
        bool accel = status & CHAIN_IMU_EVENT_STATUS_ACCEL_MASK;
        bool gyro  = status & CHAIN_IMU_EVENT_STATUS_GYRO_MASK;
        uint8_t temp = (status & CHAIN_IMU_EVENT_STATUS_TEMPERATURE_MASK) >>
                       CHAIN_IMU_EVENT_STATUS_TEMPERATURE_SHIFT;
        Serial.printf("IMU event: %s, %s, %s\r\n",
                      accel ? "ACC ACTIVE" : "ACC RELEASE",
                      gyro ? "GYRO ACTIVE" : "GYRO RELEASE", temp_text[temp]);
    }

    uint32_t now = millis();
    if (event_pending && (last_draw_ms == 0 || now - last_draw_ms >= event_hold_ms)) {
        drawEvent(last_status);
        last_draw_ms = now;
        event_pending = false;
    }

    delay(20);
}

Shake the device, and the screen displays the latest event as readable status text:

  • ACC ACTIVE / ACC RELEASE
  • GYRO ACTIVE / GYRO RELEASE
  • TEMP HIGH ACTIVE / TEMP HIGH RELEASE
  • TEMP LOW ACTIVE / TEMP LOW RELEASE
Page Tools
PDF
On This Page