🛠 Deep sleep à quelques microampères sur batterie

ESP32-S2 · avance · Schéma : IMG-esp32-s2-avance-1-esp32-s2-deep-sleep-batterie.svg — ESP32-S2 alimentée par un pack de piles, bouton de réveil sur GPIO1 (réveil EXT0), LED témoin sur GPIO2, multimètre en série mesurant la consommation en microampères.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials