The EEPROM library on the ESP32 provides a reliable way to store data persistently, making it accessible even when the power supply is disconnected.
EEPROM (Electrically Erasable Programmable Read-Only Memory) is a type of non-volatile memory, included in some Arduino models.
The ESP32 does not have a true EEPROM. However, the developers of the ESP32 Core for Arduino included an EEPROM library that emulates its behavior to make it easy to use.
With the ESP32 and the EEPROM library, you can use up to 512 bytes in flash memory. This means you have 512 different addresses, and you can save a value between 0 and 255 at each address position.
Unlike RAM, the data we save with the EEPROM library is not lost when power is cut. This characteristic makes it an ideal choice for retaining configurations, settings, and other important data in embedded devices like the ESP32.
How to use the EEPROM library on the ESP32
Initial Setup
Before using the EEPROM on the ESP32, an initial setup is necessary.
#include <EEPROM.h>
Then, it’s necessary to initialize the EEPROM with the appropriate size in the setup() method:
void setup() {
EEPROM.begin(size);
// Rest of the initialization code
}
Replace size with the size in bytes you want to allocate to the EEPROM for your data.
Storing and Reading Data
The EEPROM can be used to store various types of data, such as integers, floats, characters, and custom structures.
int value = 42;
int address = 0; // Memory address where the value will be stored
EEPROM.write(address, value); // Stores the value at the given address
EEPROM.commit(); // Saves the changes to the EEPROM memory
int readValue = EEPROM.read(address); // Reads the value stored at the given address
Remember you must call EEPROM.commit() after writing data to the EEPROM to ensure the changes are saved permanently.
Code Example
Suppose you are working on a project that uses a WiFi connection to communicate with a server. It would be useful to save the WiFi credentials in the EEPROM so the device can connect automatically after a restart.
Here is an example of how to do it:
#include <EEPROM.h>
#include <WiFi.h>
const char* ssid = "network-name";
const char* password = "network-password";
int ssidAddress = 0;
int passwordAddress = sizeof(ssid);
void setup() {
EEPROM.begin(512);
EEPROM.writeString(ssidAddress, ssid);
EEPROM.writeString(passwordAddress, password);
EEPROM.commit();
// Rest of the initialization code, WiFi connection, etc.
}
In this example, we use the writeString() method to store the WiFi credentials in the EEPROM.

