IoT/ESP32 B.Tech/Diploma Intermediate

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

Learn how to use the ESP32 flash memory to store permanent data. To write data to the flash memory, you use the EEPROM.write() function that accepts as arguments

  • Suitable forB.Tech/Diploma IoT/ESP32 students
  • Build time1-2 weeks
  • LevelIntermediate
  • Available atAjvika Labs, Kakinada

About this project

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.
This tutorial is outdated
. Follow the new tutorial instead:
[NEW]
ESP32 Save Data Permanently using Preferences Library
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.
Installing the ESP32 Board in Arduino IDE (Windows instructions)
Installing the ESP32 Board in Arduino IDE (Mac and Linux instructions)
We also recommend taking a look at the following resources:
Getting Started with ESP32 Dev Module
ESP32 Pinout Reference: Which GPIO pins should you use?
Parts Required
To follow this tutorial you need the following components:
ESP32 DOIT DEVKIT V1 Board
– read
ESP32 Development Boards Review and Comparison
5mm LED
330 Ohm resistor
Pushbutton
10k Ohm resistor
Breadboard
Jumper wires
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.
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

, 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:
You’re controlling a lamp with the ESP32
You set your lamp to turn on
The ESP32 suddenly loses power
When the power comes back on, the lamp stays off – because it doesn’t keep its last state
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:
Schematic
Wire a pushbutton and an LED to the ESP32 as shown in the following 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.

// include library to read and write from flash memory
#include <EEPROM.h>

// define the number of bytes you want to access
#define EEPROM_SIZE 1

// 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

void setup() {
Serial.begin(115200);

// initialize EEPROM with predefined size
EEPROM.begin(EEPROM_SIZE);

pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);

// read the last LED state from flash memory
ledState = EEPROM.read(0);
// set the LED to the last stored state
digitalWrite(ledPin, ledState);
}

void loop() {
// 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;

// if the ledState variable is different from the current LED state
if (digitalRead(ledPin)!= ledState) {
Serial.println("State changed");
// change the LED state
digitalWrite(ledPin, ledState);
// save the LED state in flash memory
EEPROM.write(0, ledState);
EEPROM.commit();
Serial.println("State saved in flash memory");
}
}
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 pi

Technologies used

ESP32ArduinoBLEArduino IDE

Hardware components

  • ESP32
  • Arduino

Software & tools

  • Arduino IDE

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.