How to Work with EEPROM in ESP32

2 min read

The ESP32 is a powerful and versatile microcontroller that has gained immense popularity in the world of IoT and embedded systems. One of its many features is its ability to work with EEPROM (Electrically Erasable Programmable Read-Only Memory), which allows you to store data that persists even when the ESP32 is powered off. In this guide, we'll explore what EEPROM is, how to use it with the ESP32, and provide some practical examples.

What is EEPROM?

EEPROM is a type of non-volatile memory that can store data even when the power is removed. Unlike RAM (Random Access Memory), which loses its data when the power is disconnected, EEPROM retains its data indefinitely. This makes EEPROM a suitable choice for storing configuration parameters, calibration data, and other essential information in embedded systems.

The ESP32 microcontroller includes an EEPROM emulation library that allows you to use a portion of its flash memory as if it were EEPROM. While it's not true EEPROM, this emulation provides a similar interface for reading and writing data.

Setting up the ESP32 Arduino Environment:

Before we dive into using EEPROM on the ESP32, ensure that you have the Arduino IDE set up for ESP32 development. You can follow the official documentation for installation instructions:

Official Arduino ESP32 Core Installation Guide

Once you have the IDE set up, you're ready to start working with EEPROM.

Using EEPROM in ESP32

The ESP32 EEPROM emulation library provides functions to read and write data to the emulated EEPROM. Here are the basic steps to work with EEPROM on the ESP32:

1. Include the EEPROM Library

In your Arduino sketch, include the EEPROM library by adding the following line at the top of your code:

#include <EEPROM.h>

2. Initialize EEPROM

Before using EEPROM, you need to initialize it. This is typically done in the setup() function of your sketch:

void setup() {

// Initialize EEPROM with the size you want (in bytes)

EEPROM.begin(512); // You can change the size as needed

// Your setup code here

}

3. Reading from EEPROM

To read data from EEPROM, you use the EEPROM.read() and EEPROM.get() functions. Here's an example of reading a byte from EEPROM:

int intValue;

EEPROM.get(address, intValue);

5. Committing Changes

After making changes to the EEPROM, you need to commit them to ensure they are saved permanently. You can do this using the EEPROM.commit() function:

EEPROM.commit();

Practical Example: Storing WiFi Credentials:

Here's a practical example of using EEPROM to store WiFi credentials on an ESP32:

#include <WiFi.h>

#include <EEPROM.h>

const char* ssid = "Your_SSID";

const char* password = "Your_Password";

int eepromAddress = 0;

void setup() {

EEPROM.begin(512);

// Read WiFi credentials from EEPROM

char storedSSID[32];

char storedPassword[64];

EEPROM.get(eepromAddress, storedSSID);

eepromAddress += sizeof(storedSSID);

EEPROM.get(eepromAddress, storedPassword);

eepromAddress += sizeof(storedPassword);

// Connect to WiFi

WiFi.begin(storedSSID, storedPassword);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);

Serial.println("Connecting to WiFi...");

}

Serial.println("Connected to WiFi!");

EEPROM.end();

}

void loop() {

// Your loop code here

}


4. Writing to EEPROM

Writing to EEPROM is done using the EEPROM.write() and EEPROM.put() functions. Here's an example of writing a byte to EEPROM:


byte value = 42;

EEPROM.write(address, value);