Random Nerd Tutorials
Share

ESP32 Flash Memory – Store Permanent Data (Write and Read)

Share

In this article we’ll show you how to store and read values from the ESP32 flash memory using Arduino IDE. The data saved in the flash memory remains there even when the ESP32 resets or when power is removed. As an example we’ll show you how to save the last GPIO state.

Save permanent data on the ESP32 flash memory

Before proceeding with this tutorial you should have the ESP32 add-on installed in your Arduino IDE. Follow one of the following tutorials to install the ESP32 on the Arduino IDE, if you haven’t already.

We also recommend taking a look at the following resources:

Watch the Video Tutorial

This tutorial is available in video format (watch below) and in written format (continue reading).


Parts Required

To follow this tutorial you need the following components:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Flash Memory

The data saved in the flash memory remains there even when the ESP32 resets or when power is removed. The flash memory is very similar to the EEPROM. Both are non-volatile memories.

Saving data in the flash memory is specially useful to:

  • remember the last state of a variable;
  • save settings;
  • save how many times an appliance was activated;
  • or any other type of data that you need to have saved permanently.

One limitation with flash memory is the number of times you can write data to it. Data can be read from flash as many times as you want, but most devices are designed for about 100,000 to 1,000,000 write operations.

EEPROM Library

To read and write from the ESP32 flash memory using Arduino IDE, we’ll be using the EEPROM library. Using this library with the ESP32 is very similar to using it with the Arduino. So, if you’ve used the Arduino EEPROM before, this is not much different.

So, we also recommend taking a look at our article about Arduino EEPROM.

With the ESP32 and the EEPROM library you can use up to 512 bytes in the flash memory. This means you have 512 different addresses, and you can save a value between 0 and 255 in each address position.

cloud-download

ESP32 flash memory number of bytes available

Write

To write data to the flash memory, you use the EEPROM.write() function that accepts as arguments the location or address where you want to save the data, and the value (a byte variable) you want to save:

EEPROM.write(address, value);

For example, to write 9 on address 0, you’ll have:

EEPROM.write(0, 9);

Followed by

EEPROM.commit();

For the changes to be saved.

Read

To read a byte from the flash memory, you use the EEPROM.read() function. This function takes the address of the byte you want to read as an argument.

EEPROM.read(address);

For example, to read the byte stored previously in address 0, use:

EEPROM.read(0);

This would return 9, which is the value we stored in address 0.

Remember Last GPIO State

To show you how to save data in the ESP32 flash memory, we’ll save the last state of an output, in this case an LED. For example, imagine the following scenario:

  1. You’re controlling a lamp with the ESP32
  2. You set your lamp to turn on
  3. The ESP32 suddenly loses power
  4. When the power comes back on, the lamp stays off – because it doesn’t keep its last state

output resets state after ESP32 restarts

You don’t want this to happen. You want the ESP32 to remember what was happening before losing power and return to the last state.

To solve this problem, you can save the lamp’s state in the flash memory. Then, you just need to add a condition at the beginning of your sketch to check the last lamp state, and turn the lamp on or off accordingly.

The following figure shows what we’re going to do:

keep output last state after reset ESP32

Schematic

Wire a pushbutton and an LED to the ESP32 as shown in the following schematic diagram.

debounce button ESP32 schematic diagram

Code

Copy the following code to the Arduino IDE and upload it to your ESP32. Make sure you have the right board and COM port selected.

How the Code Works

Let’s take a quick look at the code.

This is a debounce code that changes the LED state every time you press the pushbutton. But there’s something special about this code – it remembers the last LED state, even after resetting or removing power from the ESP32. Let’s see what you have to do to make the ESP32 remember the last state of a GPIO.

First, you need to include the EEPROM library.

#include <EEPROM.h>

Then, you define the EEPROM size. This is the number of bytes you’ll want to access in the flash memory. In this case, we’ll just save the LED state, so the EEPROM size is set to 1.

#define EEPROM_SIZE 1

We also define other variables that are required to make this sketch work.

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 16; // the number of the LED pin

// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers

setup()

In the setup() you initialize the EEPROM with the predefined size.

EEPROM.begin(EEPROM_SIZE);

To make sure your code initializes with the latest LED state, in the setup(), you should read the last LED state from the flash memory. It is stored on address zero.

ledState = EEPROM.read(0);

Then, you just need to turn the LED ON or OFF accordingly to the value read from the flash memory.

digitalWrite (ledPin, ledState);

loop()

The following part of the loop() checks if the pushbutton was pressed and changes the ledState variable every time we press the pushbutton. To make sure we don’t get false positives we use a timer. This snippet of code is based on the pushbutton debounce sketch example from the Arduino IDE.

// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
  // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
  // whatever the reading is at, it's been there for longer than the debounce
  // delay, so take it as the actual current state:

   // if the button state has changed:
   if (reading != buttonState) {
    buttonState = reading;
    
    // only toggle the LED if the new button state is HIGH
    if (buttonState == HIGH) {
       ledState = !ledState;
     }
   }
 }
 // save the reading. Next time through the loop, it'll be the lastButtonState:
 lastButtonState = reading;

You simply need to save the LED state in the flash memory every time the LED state changes.

We check if the state of the GPIO is different from the ledState variable.

if (digitalRead(ledPin)!= ledState) {

If it is, we’ll change the LED state using the digitalWrite() function.

digitalWrite(ledPin, ledState);

And then, we save the current state in the flash memory. For that, we use EEPROM.write(), and pass as arguments the address position, in this case 0, and the value to be saved, in this case the ledState variable.

EEPROM.write(0, ledState);

Finally, we use the EEPROM.commit() for the changes to take effect.

EEPROM.commit();

Demonstration

After uploading the code to your ESP32, press the pushbutton to turn the LED on and off. The ESP32 should keep the last LED state after resetting or removing power.

Wrapping Up

In summary, in this unit you’ve learned how to save data in the ESP32 flash memory using the EEPROM library. Data saved on the flash memory remains there even after resetting the ESP32 or removing power.

We have other articles about ESP32 that you may like:

This is an excerpt from our course: Learn ESP32 with Arduino IDE. If you like ESP32 and you want to learn more, we recommend enrolling in Learn ESP32 with Arduino IDE course.


Learn ESP32 with Arduino IDE

This our complete guide to program the ESP32 with Arduino IDE, including projects, tips, and tricks! The registrations are open, so SIGN UP NOW »

Leave a Comment:

Add Your Reply

⏰ Registrations are open ⏰
Sign up for "Learn ESP32 with Arduino IDE" Course