🛠 Réveil par mouvement et économie d'énergie (HOLD/RTC)
Objectives
Put the M5StickC Plus2 v2 to sleep to save the 200 mAh battery and wake it up periodically. The major pitfall of the v2 without a PMIC: power is latched by the HOLD pin (G4); after a wake-up (IRQ from the BM8563 RTC), the firmware must immediately set HOLD=1, otherwise the device turns off again — which looks like a boot loop. Hardware-accelerated wake-up via the MPU6886 INT pin is not documented as being routed to a wake-up pin: >
Hardware
- 1× M5StickC Plus2 (v2) — charged battery
- USB-C cable for uploading (when charging, shutdown is only a screen sleep)
- Arduino IDE / PlatformIO, M5Unified
Complete code
//veille periodique + pique d'acceleration — architecture HOLD (G4) de la v2
#include <M5Unified.h>
const int PIN_HOLD = 4; // POWER HOLD = G4 (officiel)
const float SEUIL_MOUV = 1.5f; // g
uint32_t eveils = 0;
void setup {
pinMode(PIN_HOLD, OUTPUT);
digitalWrite(PIN_HOLD, HIGH); // VITAL: garder l'alim apres un reveil RTC
auto cfg = M5.config;
M5.begin(cfg);
M5.Display.setRotation(1);
M5.Display.setTextSize(2);
M5.Display.setTextColor(TFT_MAGENTA, TFT_BLACK);
M5.Display.printf("Reveil #%lu\n", (unsigned long)++eveils);
delay(400);
}
void loop {
M5.update;
float ax, ay, az;
M5.Imu.getAccel(&ax, &ay, &az);
float n = sqrtf(ax * ax + ay * ay + az * az);
if (n > SEUIL_MOUV) { // mouvement : on reste eveille et on bippe
M5.Display.setCursor(0, 40);
M5.Display.printf("MOUVEMENT %4.2f g", n);
M5.Speaker.tone(1568, 100);
vTaskDelay(300 / portTICK_PERIOD_MS);
} else {
// Pas de mouvement : deep-sleep 2 s (le timer reveille l'ESP32).
// Sans USB, retirer HOLD eteint vraiment ; avec timer-wakeup, l'ESP
// redemarre et setup remet HOLD=1.
esp_sleep_enable_timer_wakeup(2000000ULL);
esp_deep_sleep_start;
}
// Bouton C (G35) : appui long > 6 s sans USB = extinction materielle.
}
Expected verification
The device wakes up every 2 seconds (wake-up counter displayed) and consumes almost nothing between wake-ups. Shaking the stick keeps it awake with peak display and beep. Cutting HOLD (long press C without USB) turns everything off; restarting is done by holding C for more than 2 seconds.
Common errors
- Forgetting
digitalWrite(HOLD, HIGH)at the very beginning ofsetup: after an RTC wake-up, the machine turns off immediately — the "reboot loop" symptom documented by M5Stack. - Believing that
esp_deep_sleep_startis enough to turn off the device: without USB, you must release HOLD (G4=0); classic deep-sleep leaves the power latch active. - Relying on a hardware interrupt from the IMU: the routing of the MPU6886 INT to a wake-up pin is not confirmed in the documentation.
- Testing "shutdown" while plugged into USB: with USB, a long press of C only turns off the screen — the battery does not drain but the device remains powered.
- The green LED is not programmable: it is a sleep status indicator, not a user LED.
Other tutorials for this board
Code examples for this board
hello-stick.inoArduinoboutons.inoArduinobuzzer.inoArduinoinclinometre.inoArduinohorloge.inoArduinoir-tv.inoArduino