Introduction
In embedded development, the persistence of stored data is crucial. The ESP32 offers various storage options, with flash memory being the most commonly used medium. However, flash memory has a limited number of write cycles, and frequent writes can shorten its lifespan. In particular, the EEPROM implementation of the ESP32 stores data in the NVS area of flash memory, and each write triggers a rewrite of the entire EEPROM, accelerating flash wear.
RTC RAM: The Guardian of Deep Sleep
The ESP32 has an internal real-time clock (RTC) area that contains RAM, which remains powered during deep sleep mode. This provides us with a method to maintain data persistence during deep sleep, effectively extending the lifespan of flash memory.
ESP32_RTC_EEPROM Library: A Tool for Simulating EEPROM
The ESP32_RTC_EEPROM library utilizes RTC RAM to simulate the functionality of Arduino EEPROM, storing data in RTC RAM and retaining it during deep sleep mode. This library provides an interface similar to Arduino EEPROM, allowing developers to easily port existing code to the new storage scheme.
Use Cases
This library is suitable for the following scenarios:
-
• Need to maintain data persistence during deep sleep mode, such as recording boot counts, sensor data, configuration information, etc.
-
• Existing code has used the Arduino EEPROM.h library and wishes to migrate to a more persistent and low-power storage method.
Considerations
-
• Data will be lost after rebooting or re-flashing firmware.
-
• RTC RAM has limited capacity and can only store a small amount of data.
Using the Library
-
1. Install the Library
-
• Use the library manager in the Arduino IDE to search for and install the ESP32_RTC_EEPROM library.
-
• Alternatively, clone the library’s code into the Arduino libraries folder.
2. Replace Code
-
• In your code, replace
<span>#include <EEPROM.h></span>with<span>#include <ESP32_RTC_EEPROM.h></span>.
3. Start Using
-
• Use
<span>EEPROM.begin(size)</span>to initialize the library, where<span>size</span>indicates the amount of RTC RAM to use. -
• Use
<span>EEPROM.read(address)</span>to read data, and<span>EEPROM.write(address, data)</span>to write data.
Example Code
#define BUTTON_GPIO GPIO_NUM_0
#define BUTTON_ACTIVE LOW
#include <ESP32_RTC_EEPROM.h>
void setup() {
Serial.begin(115200);
// Initialize RTC EEPROM, using 512 bytes of space
EEPROM.begin(512);
// Read boot count
uint8_t bootcount = EEPROM.read(0);
// Increment boot count
bootcount++;
// Print boot count
Serial.println("Boot count: " + String(bootcount));
// Check if data was restored from NVS
if (EEPROM.wasRestored()) {
Serial.println("(This value was restored from NVS, so we may have missed a few.)");
}
// Write new boot count
EEPROM.write(0, bootcount);
// Backup data to NVS every 10 boots
if (bootcount % 10 == 0) {
Serial.println("Backing up the current EEPROM data from RTC RAM to NVS (flash).");
EEPROM.toNVS();
}
// Enter deep sleep mode
Serial.println("Going to deep sleep, wake me up with the button.");
// Set wakeup condition: button pressed
esp_sleep_enable_ext0_wakeup(BUTTON_GPIO, BUTTON_ACTIVE);
// Enter deep sleep mode
esp_deep_sleep_start();
}
void loop() {
// No operation here
}
Optional NVS Backup
The library provides the <span>EEPROM.toNVS()</span> function to back up data from RTC RAM to the NVS area, allowing data recovery after rebooting or re-flashing firmware. The <span>EEPROM.fromNVS()</span><span> function can be used to manually restore data from NVS.</span>
Conclusion
The ESP32_RTC_EEPROM library offers a simple and efficient solution for developers to save data while the ESP32 is in deep sleep mode, effectively extending the lifespan of flash memory. The library is easy to use and compatible with the Arduino EEPROM interface, making it an ideal choice for developers.
Project Address: https://github.com/ropg/ESP32_RTC_EEPROM