
Arduino Quick Start
APIs and example programs related to the IMU attitude sensor of CoreP4X.
3.3.10-aM5CoreP4X0.3.10.3.1CoreP4X has an onboard LSM6DS3TR-C 6-axis attitude sensor, including a 3-axis accelerometer and a 3-axis gyroscope.
#include <M5Unified.h>
M5Canvas canvas(&M5.Display);
static void drawImu(const m5::imu_data_t& data)
{
constexpr int boxX = 40;
constexpr int boxY = 126;
constexpr int boxW = 400;
constexpr int boxH = 210;
constexpr int centerX = boxX + boxW / 2;
constexpr int centerY = boxY + boxH / 2;
constexpr float ballScale = 70.0f;
constexpr float ballXSign = 1.0f;
constexpr float ballYSign = -1.0f;
canvas.fillSprite(TFT_BLACK);
canvas.setTextDatum(middle_center);
canvas.setTextColor(TFT_CYAN, TFT_BLACK);
canvas.setFont(&fonts::FreeSansBold24pt7b);
canvas.drawString("CoreP4X IMU", 240, 54);
canvas.setTextColor(TFT_WHITE, TFT_BLACK);
canvas.setFont(&fonts::FreeSans12pt7b);
canvas.drawString("LSM6DS3TR-C", 240, 94);
canvas.drawRect(boxX, boxY, boxW, boxH, TFT_DARKGREY);
canvas.drawFastVLine(centerX, boxY, boxH, 0x2124);
canvas.drawFastHLine(boxX, centerY, boxW, 0x2124);
canvas.fillCircle(centerX, centerY, 5, TFT_DARKGREY);
// Screen X follows accel.x; screen Y is inverted because Y grows downward.
const int ballX = constrain(
centerX + static_cast<int>(data.accel.x * ballScale * ballXSign),
boxX + 24, boxX + boxW - 24);
const int ballY = constrain(
centerY + static_cast<int>(data.accel.y * ballScale * ballYSign),
boxY + 24, boxY + boxH - 24);
canvas.fillCircle(ballX, ballY, 16, TFT_GREEN);
char accLine[64];
char gyroLine[64];
snprintf(accLine, sizeof(accLine), "ACC %+5.2f %+5.2f %+5.2f",
data.accel.x, data.accel.y, data.accel.z);
snprintf(gyroLine, sizeof(gyroLine), "GYRO %+5.2f %+5.2f %+5.2f",
data.gyro.x, data.gyro.y, data.gyro.z);
canvas.setTextDatum(middle_center);
canvas.setFont(&fonts::FreeSansBold18pt7b);
canvas.setTextColor(TFT_WHITE, TFT_BLACK);
canvas.drawString(accLine, 240, 384);
canvas.drawString(gyroLine, 240, 428);
canvas.pushSprite(0, 0);
}
static bool ensureImuReady()
{
if (M5.Imu.isEnabled()) return true;
// Retry after the PMIC and shared I2C bus wake up.
for (int retry = 0; retry < 5; ++retry) {
M5.Imu.begin(&M5.In_I2C, M5.getBoard());
if (M5.Imu.isEnabled()) return true;
delay(200);
}
return false;
}
void setup()
{
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
M5.begin(cfg);
M5.Display.setRotation(0);
M5.Display.setBrightness(160);
canvas.createSprite(M5.Display.width(), M5.Display.height());
if (!ensureImuReady()) {
canvas.fillSprite(TFT_BLACK);
canvas.setTextDatum(middle_center);
canvas.setTextColor(TFT_RED, TFT_BLACK);
canvas.setFont(&fonts::FreeSansBold18pt7b);
canvas.drawString("IMU not found", 240, 240);
canvas.pushSprite(0, 0);
Serial.println("IMU enabled: no");
return;
}
Serial.println("IMU enabled: yes");
drawImu(M5.Imu.getImuData());
}
void loop()
{
if (!M5.Imu.isEnabled()) {
delay(500);
return;
}
if (M5.Imu.update()) {
const auto data = M5.Imu.getImuData();
drawImu(data);
Serial.printf("A: %.3f %.3f %.3f | G: %.3f %.3f %.3f\n",
data.accel.x, data.accel.y, data.accel.z,
data.gyro.x, data.gyro.y, data.gyro.z);
}
delay(100);
}After uploading, tilt the CoreP4X to move the green ball in the center; the lower part of the screen shows the 3-axis acceleration and 3-axis angular velocity in real time.
The IMU section of CoreP4X uses the IMU_Class in the M5Unified library. For more APIs, refer to: