Table of Contents
RTC (Real-Time Clock) memory is a small portion of memory in the ESP32 that remains powered during deep sleep. It allows data retention while keeping power consumption low.
Key Features of RTC Memory
- Preserved in deep sleep, but lost in hibernation mode.
- Limited to 8KB of RTC slow memory (retained in deep sleep).
- Useful for storing variables like counters, sensor readings, or flags across sleep cycles.
This is the simplest method to store and retrieve data across deep sleep cycles.
#include <esp_sleep.h>
RTC_DATA_ATTR int bootCount = 0; // Stored in RTC memory
void setup() {
Serial.begin(115200);
delay(1000);
bootCount++; // Increment counter across deep sleep cycles
Serial.print("Boot count: ");
Serial.println(bootCount);
Serial.println("ESP32 entering deep sleep...");
// Sleep for 10 seconds
esp_sleep_enable_timer_wakeup(10 * 1000000);
esp_deep_sleep_start();
}
void loop() {
// This will not execute after wake-up
}
Explanation
- The variable bootCount is stored in RTC slow memory using RTC_DATA_ATTR.
- The value persists after deep sleep but resets on power loss or full reset.
- The ESP32 wakes up, prints the counter, and goes back to sleep.