sommeil-profond.ino

ESP32 (original, ESP32-WROOM-32 class) · File : sommeil-profond.ino · Language : Arduino · Use case : Réduire la consommation au sommeil profond minuté

In deep sleep the SoC draws a few microamps: wake by the minute or by button, and a battery lasts months. RTC_NOINIT_ATTR keeps the wake counter across the reset.

Hardware required

A classic ESP32 board (ESP32-WROOM-32 module, dual-core Xtensa 240 MHz, Wi-Fi + Bluetooth) such as a DevKitC / DOIT V1, powered over micro-USB. None to test; button on GPIO0 to wake manually (careful: GPIO0 is also the bootloader pin).

Wiring

Button GPIO0 to GND. On a battery build, remove the USB-serial bridge, which sips more than the sleeping SoC.

How it works

esp_sleep_enable_timer_wakeup arms the RTC clock, ext0 watches a level; esp_deep_sleep_start powers everything else down. On wake the program restarts at setup() — RAM variables are lost except RTC attributes. In the Arduino IDE, install the « esp32 » board package, pick « Dev Module », the port, then Upload (hold BOOT if detection fails). GPIO34-39 are input-only; avoid GPIO6-11 and GPIO12.

Going further

Wake on a sensor (ext1 across several pins), or sleep between two DHT reads from the dedicated sketch.

sommeil-profond.ino

// Sommeil profond : reveil toutes les 30 s ou sur GPIO0 (LOW).
// Le compteur de reveils est conserve en RTC memory (tenu sous tension).
RTC_NOINIT_ATTR int reveils;

void setup() {
  Serial.begin(115200);
  delay(150);                       // laisser le pont USB-serial s'ouvrir
  reveils++;
  Serial.printf("\nReveil n°%d — mesure, puis dodo 30 s\n", reveils);

  // (ici : lecture capteur, envoi radio, etc.)
  Serial.printf("heap libre avant dodo : %u\n", ESP.getFreeHeap());

  esp_sleep_enable_timer_wakeup(30ULL * 1000000);          // 30 s
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_0, 0);             // ou GPIO0 a LOW
  Serial.println("Entree en sommeil profond...");
  Serial.flush();
  delay(10);
  esp_deep_sleep_start();
}

void loop() {}   // jamais atteint : le deep sleep reinitialise tout

← Back to the board