🛠 Lire l'accélération avec l'IMU MPU6886

M5StickC Plus2 (M5Stack) — ESP32-PICO-V3-02 "stick" wearable kit (SKU K016-P2) · debutant · Schéma : IMG-m5stack-stick-c-plus-2-debutant-3-m5stick-c2-imu-acceleration.svg — M5StickC Plus2 inclinée avec vecteurs X, Y, Z d'accélération et bus I2C interne (SDA=G21, SCL=G22) reliant l'IMU MPU6886 et l'horloge RTC BM8563.

Objectives

Read the 3 acceleration axes of the MPU6886 (6-axis IMU: accelerometer + gyroscope) connected to the internal I2C bus SDA=G21 / SCL=G22, the same bus as the BM8563 real-time clock (standard I2C addresses: 0x68 for the MPU6886, 0x51 for the BM8563). Display ax, ay, az in g on the screen and on the serial monitor.

Hardware

Complete code

// IMU MPU6886 sur I2C interne SDA=G21 SCL=G22 (adresse 0x68)
#include <M5Unified.h>

void setup {
  auto cfg = M5.config;
  M5.begin(cfg);
  Serial.begin(115200);
  M5.Display.setRotation(1);
  M5.Display.setTextSize(2);
  M5.Display.setTextColor(TFT_CYAN, TFT_BLACK);
  if (!M5.Imu.isEnabled) {
    M5.Display.println("IMU absente!");   // MPU6886 introuvable sur 0x68
  }
}

void loop {
  M5.update;
  float ax = 0, ay = 0, az = 0;
  M5.Imu.getAccel(&ax, &ay, &az);        // en g (1 g = 9.81 m/s2)
  M5.Display.setCursor(0, 10);
  M5.Display.printf("ax %+5.2f g\n", ax);
  M5.Display.printf("ay %+5.2f g\n", ay);
  M5.Display.printf("az %+5.2f g\n", az);
  Serial.printf("%f %f %f\n", ax, ay, az);
  vTaskDelay(100 / portTICK_PERIOD_MS);
}

Expected verification

When placed flat, the screen displays approximately az = +1.00 g and ax/ay close to 0; when tilted on its edge, the corresponding axis moves to ±1 g. The serial monitor outputs 10 lines per second. A small I2C scanner (Wire.begin(21, 22)) finds 0x68 (IMU) and 0x51 (RTC).

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials