🛠 Deep sleep à quelques microampères sur batterie
Objectives
Design a battery-powered setup: put the ESP32-S2 into deep sleep (microampere-level consumption), wake it up via timer and button on an RTC pin, and measure the power consumption. The S2 is a single-core Xtensa LX7 at 240 MHz, well-suited for low-power periodic operation.
Hardware
- ESP32-S2 board (the regulators on DevKit boards add standby current: to aim for µA levels, use a bare board or disconnect the USB regulator)
- Battery pack or 3.7 V LiPo
- 1 push button on GPIO1, 1 LED on GPIO2 + 330 Ω
- Multimeter in current measurement mode
Full code
// Deep sleep ESP32-S2 : reveil toutes les 30 s ou sur bouton GPIO1
#include <esp_sleep.h>
const uint8_t BTN_REVEIL = 1; // broche RTC valide pour EXT0
const uint8_t LED_PIN = 2;
const uint64_t PERIODE_US = 30ULL * 1000000ULL;
void setup {
Serial.begin(115200);
delay(300); // fenetre pour ouvrir le moniteur
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause;
Serial.printf("Reveil : %s\n",
cause == ESP_SLEEP_WAKEUP_TIMER ? "minuteur" :
cause == ESP_SLEEP_WAKEUP_EXT0 ? "bouton" : "premiere mise sous tension");
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH); // temoin bref de reveil
delay(300);
digitalWrite(LED_PIN, LOW);
// Fenetre de 5 s apres reset pour permettre le reflash USB
// (l'USB natif GPIO19/20 est inactif pendant le deep sleep)
unsigned long t0 = millis;
while (millis - t0 < 5000) {
if (Serial) break;
delay(100);
}
esp_sleep_enable_ext0_wakeup((gpio_num_t)BTN_REVEIL, 0); // reveil sur LOW
esp_sleep_enable_timer_wakeup(PERIODE_US);
Serial.println("Endormissement profond...");
delay(10);
esp_deep_sleep_start; // ne revient jamais ici
}
void loop {} // inatteignable
Expected verification
At each wake-up (30 s timer or button press), the LED blinks briefly and the console indicates the cause. Between wake-ups, the multimeter in series should display very low consumption (µA on a bare board, mA on a DevKit due to the regulator and LEDs). The millis clock resets to zero at each cycle: deep sleep cuts off the core.
Common errors
- Wiring the wake-up pin to GPIO0, 45, or 46: these are strapping pins, their level at startup can block booting; prefer GPIO1-14.
- Using GPIO33-37: on many S2 boards, these pins are used by flash/PSRAM and are not available on the connectors.
- Waiting for the USB port during sleep: native USB (GPIO19/20) is disconnected, reflashing requires a reset + Boot button press; keep a startup window.
- Forgetting that deep sleep clears RAM: all state must be saved in RTC memory or flash before entering sleep.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinorespiration.inoArduinodht22.inoArduinooled-i2c.inoArduinoserveur-web.inoArduino