
Arduino Quick Start
Chain Captain onboard BMI270 6-axis IMU related APIs and sample programs.
3.3.10-aM5ChainCaptain0.3.00.3.0#include <M5Unified.h>
M5Canvas canvas(&M5.Display);
static uint32_t lastRefresh = 0;
static bool ensureImuReady()
{
if (M5.Imu.isEnabled()) return true;
// Retry the probe while M5PM1 and the shared I2C bus wake up.
for (int retry = 0; retry < 5; ++retry) {
if (M5.Imu.begin(&M5.In_I2C, M5.getBoard())) return true;
delay(200);
}
return false;
}
static void drawReading()
{
// BMI270: read accelerometer and gyroscope data.
auto data = M5.Imu.getImuData();
canvas.fillSprite(TFT_BLACK);
canvas.setTextDatum(middle_center);
canvas.setFont(&fonts::FreeSansBold12pt7b);
canvas.setTextColor(TFT_WHITE, TFT_BLACK);
canvas.drawString("BMI270", 120, 22);
canvas.drawRect(8, 44, 224, 120, TFT_DARKGREY);
canvas.fillCircle(120, 104, 5, TFT_DARKGREY);
// CoreP4X LSM6DS3TR-C and ChainCaptain BMI270 share the same fixed axis remap.
// M5Unified applies it in getImuData(), so use the corrected axes directly.
const int x = constrain(static_cast<int>(120 + data.accel.x * 80.0f), 18, 222);
const int y = constrain(static_cast<int>(104 - data.accel.y * 45.0f), 54, 154);
canvas.fillCircle(x, y, 8, TFT_GREEN);
canvas.setFont(&fonts::FreeSans9pt7b);
canvas.setTextDatum(top_left);
canvas.setTextColor(TFT_WHITE, TFT_BLACK);
canvas.setCursor(8, 172);
canvas.printf("ACC %+.2f %+.2f %+.2f\n", data.accel.x, data.accel.y, data.accel.z);
canvas.printf("GYRO %+.2f %+.2f %+.2f", data.gyro.x, data.gyro.y, data.gyro.z);
canvas.pushSprite(0, 0);
}
void setup()
{
delay(500);
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
cfg.fallback_board = m5::board_t::board_M5ChainCaptain;
M5.begin(cfg);
if (M5.Display.getBoard() != m5::board_t::board_M5ChainCaptain) {
M5.Display.init();
}
if (M5.Display.getBoard() != m5::board_t::board_M5ChainCaptain) {
Serial.println("Display detection: no");
return;
}
M5.Display.setRotation(0);
M5.Display.setBrightness(128);
canvas.createSprite(M5.Display.width(), M5.Display.height());
const bool imuReady = ensureImuReady();
Serial.printf("IMU enabled: %s\n", imuReady ? "yes" : "no");
if (!imuReady) {
canvas.fillSprite(TFT_BLACK);
canvas.setTextDatum(middle_center);
canvas.setTextColor(TFT_RED, TFT_BLACK);
canvas.drawString("IMU not found", 120, 120);
canvas.pushSprite(0, 0);
return;
}
drawReading();
}
void loop()
{
if (!M5.Imu.isEnabled()) {
delay(1000);
return;
}
const uint32_t now = millis();
if (now - lastRefresh >= 100) {
lastRefresh = now;
if (M5.Imu.update()) {
drawReading();
}
}
delay(10);
}After uploading, the green dot in the center of the screen moves according to the acceleration direction, and the accelerometer and gyroscope data from the BMI270 are displayed below in real time.
The Chain Captain IMU section uses the IMU_Class from the M5Unified library. For more IMU related APIs, refer to the following document: