🛠 Réveil par mouvement et économie d'énergie (HOLD/RTC)

M5StickC Plus2 (M5Stack) — ESP32-PICO-V3-02 "stick" wearable kit (SKU K016-P2) · avance · Schéma : IMG-m5stack-stick-c-plus-2-avance-1-m5stick-c2-eveil-mouvement.svg — M5StickC Plus2 en cycle veille/réveil : broche HOLD G4 à 1 pendant l'activité, relâchée en veille, réveil par IRQ du RTC BM8563, avec la consigne « remettre HOLD=1 sinon réextinction ».

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials