Language: EN

esp32-eeprom

ESP32 Variables Storage and EEPROM Library

The EEPROM library on ESP32 provides a reliable way to store data persistently, so that it remains 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 an EEPROM as such. However, the developers of the ESP32 Core for Arduino included an EEPROM library that emulates the 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 store a value between 0 and 255 at each address position.

Unlike RAM, the data we save with the EEPROM library does not get lost when power is cut. This feature 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 ESP32

Initial Setup

Before using the EEPROM on 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() {
  EEPROM.begin(size);
  // Rest of the initialization code
}

Replace size with the size in bytes that 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 that you must 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 save the WiFi credentials in the EEPROM so that the device can automatically connect after rebooting.

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.

Additional Functions

Some additional useful functions, which appear 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);