Objectives
- Put the ESP32 of the M5Stack Core into deep sleep (
esp_sleep) and wake it up via a timer and/or a button. - Use GPIO0 (rear BOOT button) as the EXT0 wake-up source, and GPIO35 (M-BUS) as EXT1 if needed.
- Distinguish between deep sleep (the ESP32 sleeps, the IP5306 continues to supply voltages) and
M5.Power.powerOFF (complete soft shutdown via the red button). - Measure the lifecycle of a battery-powered recorder on a 110 mAh battery: measure, display, sleep.
Hardware
- 1 charged M5Stack Core (110 mAh LiPo battery, 150 mAh before 2020.3).
- Know the rear buttons: BOOT = GPIO0 (strapping + wake-up possible), RESET.
- Optional: external button on GPIO35 (M-BUS) for EXT1.
Complete code
#include <M5Unified.h>
#include <esp_sleep.h>
#define REVEIL_US (60ULL * 1000000ULL) // 60 s
#define PIN_EXT0 0 // bouton BOOT arriere
#define PIN_EXT1 35 // M-BUS, optionnel
void setup {
auto cfg = M5.config;
M5.begin(cfg);
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause;
M5.Display.setTextSize(2);
M5.Display.fillScreen(TFT_BLACK);
M5.Display.setCursor(5, 5);
switch (cause) {
case ESP_SLEEP_WAKEUP_TIMER: M5.Display.println("Reveil : minuteur"); break;
case ESP_SLEEP_WAKEUP_EXT0: M5.Display.println("Reveil : BOOT G0"); break;
case ESP_SLEEP_WAKEUP_EXT1: M5.Display.println("Reveil : G35"); break;
default: M5.Display.println("Demarrage normal");
}
// Pseudo-mesure : niveau de batterie
M5.Display.printf("Batterie : %d %%\n", M5.Power.getBatteryLevel);
vTaskDelay(1500 / portTICK_PERIOD_MS);
M5.Display.setBrightness(0); // eteint le retroéclairage
esp_sleep_enable_timer_wakeup(REVEIL_US);
esp_sleep_enable_ext0_wakeup((gpio_num_t)PIN_EXT0, 0); // G0 a l'etat bas
esp_sleep_enable_ext1_wakeup(1ULL << PIN_EXT1, ESP_EXT1_WAKEUP_ANY_HIGH);
esp_deep_sleep_start;
}
void loop { } // tout se passe dans setup
Expected verification
- On first startup: "Normal startup", then the screen turns off; 60 seconds later the screen turns back on with "Wake-up: timer".
- In deep sleep, pressing BOOT (rear) wakes it up immediately with "Wake-up: BOOT G0".
- Significantly reduced power consumption during sleep (the screen and CPU are cut off) — to be confirmed with a micro-ammeter in series with the battery.
Common errors
- Confusing deep sleep and
powerOFF: powerOFF cuts off the IP5306 boost, making the board appear "bricked" until the red button is pressed; deep sleep keeps the IP5306 active. - Choosing GPIO39/38/37 (front buttons) for EXT0/EXT1: these input-only pins are not valid wake-up pins; use GPIO0 or the M-BUS pins.
- EXT0 wake-up never triggers: check the expected level (BOOT connects G0 to GND → level 0) and the pin retention states.
- Forgetting to turn off the backlight: it is the biggest power consumer; use
setBrightness(0) before sleeping. - Aiming for theoretical autonomy on 110 mAh: with the screen on and Wi-Fi, real autonomy is short; cyclic deep sleep is the only long-term regime.
- No RTC on this board: the deep-sleep timer is the only reliable wake-up clock.