Battery-powered ESP32 designs usually sleep most of the time, and the first surprise is how much of the chip's state goes away when they do. This reading review covers three pages of Espressif's ESP-IDF documentation and applies them to one question: which of your variables survive deep sleep, and what should you do about the rest?
1. What stays powered in deep sleep (ESP-IDF sleep modes)
The ESP-IDF "Sleep Modes" page states that in deep sleep the CPUs, most of the RAM and all digital peripherals clocked from APB_CLK are powered off. Only a short list of parts stays on, including the RTC controller, RTC peripherals and RTC memory (RTC fast and RTC slow). The page also notes that by default the sleep functions power down every RTC power domain that the enabled wake-up sources do not need, and that esp_sleep_pd_config() overrides this.
Lesson: "the chip slept" does not mean "the program paused". Ordinary variables are gone after deep sleep, and the device boots again. Source: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/sleep_modes.html
2. Wake-up sources decide what must stay on (same page)
The same page lists the ways to wake up: a timer, touch pads, ext0 and ext1 external wake-up on RTC GPIOs, GPIO wake-up, and the ULP coprocessor. They are not free of side effects. It says RTC IO is part of the RTC peripherals domain, so that domain stays powered when such a source is requested. It also says that on ESP32 the ULP wake-up source cannot be used when the RTC_PERIPH domain is forced on, or when ext0 is used. Pull-ups and pull-downs for wake pins have to be configured with the rtc_gpio_* functions before esp_deep_sleep_start() is called.
Lesson: the wake design, the pin choice and the power budget are one decision, and the compatibility notes in the documentation are worth reading before the board is wired.
3. Keeping a value across deep sleep (ESP-IDF memory types and wake stubs)
The "Memory Types" page describes RTC_NOINIT_ATTR, which places data in RTC memory so that "the values placed into this section keep their value after waking from deep sleep". The sleep page adds that variables placed in RTC slow memory with RTC_DATA_ATTR keep that memory powered by default. The "Deep-sleep Wake Stubs" page covers a smaller trick: a function that runs right after wake-up, before the full boot. It must be loaded into RTC fast memory, and the page says it can only call ROM functions or code in RTC fast memory, because the SPI flash is not yet mapped and other RAM holds random data. Its size is limited to what RTC fast memory can hold.
Lesson: RTC memory is a small place for a counter or a flag, such as a boot count or a last-known state. It is not a general store, and a wake stub is for very short work.
Sources: https://documentation.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/memory-types.html and https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/deep-sleep-stub.html
Reading it against a published prototype
The smart-event-hall-esp32 sketch holds its people count in ordinary RAM, so the count is lost on any reset. This is an observation about the published code, which contains no sleep or RTC-memory calls. The point is general: a counter that matters needs a decision about where it lives, and the ESP-IDF documentation shows the options (RTC memory for short sleeps and small values, flash or a broker for anything that must outlive a power loss). This is a prototype, not a medical or production system, and the pages above are documentation reviews, not measurements of any board.
Checklist from the sources
- List which variables must survive sleep. Put only those in RTC memory.
- Choose the wake-up source first, then check its power-domain and compatibility notes.
- Configure RTC pull-ups and pull-downs before entering deep sleep.
- Keep wake stub code tiny and ROM-only or RTC-fast-only.
Top comments (1)
On battery ESP32 builds, keep an eye on the ~8KB RTC slow memory ceiling for RTC_DATA_ATTR buffers, and remember outputs float during deep sleep unless you latch them with gpio_hold_en() plus gpio_deep_sleep_hold_en() and release on wake. Another common board bug is wiring an external interrupt to a non-RTC GPIO that cannot trigger ext0 or ext1 wake. When double-checking which pins are RTC wake-capable, gokino.dev/en/tools/esp32-pinout is handy to reference (I built that quick pinout map on gokino.dev).