🛠 Lire l'accélération avec l'IMU MPU6886
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
- 1× M5StickC Plus2 (v2)
- USB-C cable + CH9102 driver
- Arduino IDE / PlatformIO, M5Unified library
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
- Scanning the wrong bus: the Grove HY2.0-4P connector (PORT.CUSTOM, G32/G33) is a separate I2C bus from the internal G21/G22 bus.
- Using the old API
M5.Power.*or the deprecated libraryM5StickCPlus2.h: the v2 no longer has an AXP192 PMIC and the library is no longer maintained. - Expecting degrees or m/s²:
getAccelreturns values in g; convert them yourself if needed. - Not testing
M5.Imu.isEnabledand displaying zeros without understanding why (wrong bus, wrong board). - Confusing the accelerometer and the gyroscope:
getGyroreturns angular velocity, not tilt.
Other tutorials for this board
Code examples for this board
hello-stick.inoArduinoboutons.inoArduinobuzzer.inoArduinoinclinometre.inoArduinohorloge.inoArduinoir-tv.inoArduino