inclinometre.ino
The MPU6886 IMU yields the gravity vector: two atan2 and the stick becomes a digital spirit level, bubble included. Filtering isn't even needed for static tilt.
Hardware required
An M5StickC Plus2 (ESP32-PICO-V3-02, 135×240 display, A/B/C buttons, passive buzzer on G2, IR + red LED on G19, MPU6886 IMU, microphone, 200 mAh battery). None.
Wiring
No wiring; the MPU6886 sits on the internal I2C bus (address 0x68).
How it works
getAccel returns acceleration in g per axis; roll/pitch follow from atan2 of the gravity vector. The bubble is the same angle mapped to screen coordinates. In the Arduino IDE (esp32 package), board « M5StickCPlus2 »; « M5StickCPlus2 » or M5Unified library. On the PMIC-less v2, hold button C for >2 s to power on; HOLD (G4) latches the power.
Going further
Step counter by threshold on |a|, fall detector (norm drop), or wrist-driven game control.
inclinometre.ino
// MPU6886 : inclinaison en degres via M5.Imu (M5Unified).
#include <M5Unified.h>
void setup() {
auto cfg = M5.config();
M5.Imu.setRotation(M5_IMU_COORDS_SYSTEM_ESP32);
M5.begin(cfg);
M5.Display.setRotation(1);
}
void loop() {
M5.update();
M5.Imu.update();
auto data = M5.Imu.getAccel();
// data.x/y/z en g ; angle = atan2 sur le plan pertinent
float roll = atan2f(data.y, data.z) * 180.0f / M_PI;
float pitch = atan2f(-data.x, sqrtf(data.y * data.y + data.z * data.z))
* 180.0f / M_PI;
M5.Display.fillRect(0, 0, 240, 135, TFT_BLACK);
M5.Display.setTextSize(2);
M5.Display.setCursor(6, 6);
M5.Display.printf("roll %+6.1f\n", roll);
M5.Display.printf("pitch %+6.1f\n", pitch);
// bille : position d'un point selon l'inclinaison
int x = 120 + constrain((int)roll, -45, 45) * 2;
int y = 68 - constrain((int)pitch, -30, 30);
M5.Display.fillCircle(x, y, 6, TFT_CYAN);
delay(50);
}