Language: EN


ESP32 Variables Storage and EEPROM Library

The EEPROM library in ESP32 provides a reliable way to store data persistently, so that it is accessible even when the power source is disconnected.

EEPROM (Electrically Erasable Programmable Read-Only Memory) is a type of non-volatile memory that is included in some Arduino models.

The ESP32 does not have an EEPROM as such. However, the developers of the ESP32 Core for Arduino included an EEPROM library that emulates the behavior, making 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 store a value between 0 and 255 in each address position.

Unlike RAM, the data we store with the EEPROM library is not lost when the power is cut off. This feature makes it an ideal choice for retaining settings, adjustments, and other important data in embedded devices such as the ESP32.

How to Use the EEPROM Library in ESP32

Initial Setup

Before using the EEPROM in ESP32, it is necessary to perform an initial setup.

#include <EEPROM.h>

Then, you need to initialize the EEPROM with the appropriate size in the setup() method:

void setup() {
  // Rest of the initialization code

Replace size with the size in bytes that you want to assign 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. Here’s how to store and read an integer in the EEPROM:

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

int readValue =; // Reads the value stored at the given address

Remember to call EEPROM.commit() after writing data to the EEPROM to ensure that 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 store the WiFi credentials in the EEPROM so that the device can connect automatically after a restart.

Here’s an example of how to do it:

#include <EEPROM.h>
#include <WiFi.h>

const char* ssid = "network-name";
const char* password = "network-password";
int addressSSID = 0;
int addressPassword = sizeof(ssid);

void setup() {
  EEPROM.writeString(addressSSID, ssid);
  EEPROM.writeString(addressPassword, password);
  // Rest of the initialization code, WiFi connection, etc.

In this example, we use the writeString() method to store the WiFi credentials in the EEPROM.

Additional Functions

Some additional useful functions, found in the eeprom.h file

uint8_t readByte(int address);
int8_t readChar(int address);
uint8_t readUChar(int address);
int16_t readShort(int address);
uint16_t readUShort(int address);
int32_t readInt(int address);
uint32_t readUInt(int address);
int32_t readLong(int address);
uint32_t readULong(int address);
int64_t readLong64(int address);
uint64_t readULong64(int address);
float_t readFloat(int address);
double_t readDouble(int address);
bool readBool(int address);
size_t readString(int address, char* value, size_t maxLen);
String readString(int address);
size_t readBytes(int address, void * value, size_t maxLen);
template <class T> T readAll (int address, T &);

size_t writeByte(int address, uint8_t value);
size_t writeChar(int address, int8_t value);
size_t writeUChar(int address, uint8_t value);
size_t writeShort(int address, int16_t value);
size_t writeUShort(int address, uint16_t value);
size_t writeInt(int address, int32_t value);
size_t writeUInt(int address, uint32_t value);
size_t writeLong(int address, int32_t value);
size_t writeULong(int address, uint32_t value);
size_t writeLong64(int address, int64_t value);
size_t writeULong64(int address, uint64_t value);
size_t writeFloat(int address, float_t value);
size_t writeDouble(int address, double_t value);
size_t writeBool(int address, bool value);
size_t writeString(int address, const char* value);
size_t writeString(int address, String value);
size_t writeBytes(int address, const void* value, size_t len);