sommeil-profond.ino
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
An ESP32-C3 board (RISC-V 160 MHz, Wi-Fi + BLE 5) such as a DevKitM-1 or SuperMini, powered over USB (native on the C3). 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 (esp32 package), select « ESP32C3 Dev Module », enable « USB CDC On Boot » if needed, then Upload. GPIO9 is often the BOOT button; keep GPIO8 free at boot.
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