Table of Contents

RTC memory cannot be used in Hibernation Mode, but data can be saved persistently using flash storage.

#include <Preferences.h>
#include <esp_sleep.h>

Preferences preferences;

void setup() {
    Serial.begin(115200);
    delay(1000);

    preferences.begin("storage", false);

    int bootCount = preferences.getInt("bootCount", 0);
    bootCount++;

    Serial.print("Boot count: ");
    Serial.println(bootCount);

    preferences.putInt("bootCount", bootCount);
    preferences.end();

    Serial.println("ESP32 entering deep sleep...");
    esp_sleep_enable_timer_wakeup(10 * 1000000);
    esp_deep_sleep_start();
}

void loop() {
    // This will not execute after wake-up
}

Which One to Use?

FeatureRTC Memory (RTC_DATA_ATTR)Preferences (NVS)
Retained after deep sleep✅ Yes✅ Yes
Retained after power loss❌ No✅ Yes
Data size limit~8KB64KB
Read/Write Speed⚡ Fast🚀 Slower
Use caseTemporary storagePermanent storage

Categorized in:

Deep Sleep,

Tagged in: