How to use the EEPROM memory on the NodeMCU ESP8266

What is the the EEPROM memory

The EEPROM memory (acronym for Electrically Erasable Programmable Read-Only Memory) is a type of memory present in many electronic devices (including our beloved devices such as Arduino or NodeMCU ESP8266 / ESP32 etc) used to store small amounts of data in a ” almost” permanent way. In practice, the stored data is maintained until it is modified (by the user or, in general, by the software that manages the device) and remains stored even in the event of a power failure. In other words, the data remains unchanged even if the power fails and is modified (updated) only if foreseen by the user or by the software. This type of memory is therefore very useful for memorizing the status of some variables that you don’t want to lose in the event of a power failure (for example the status of a GPIO or the ON/OFF time of a device, etc.). More generally, it allows you to store the settings of a device that we don’t want to lose when it is turned off.

One fact must be underlined: EEPROM memories do not have an “infinite” life. In fact, the number of possible writes is limited (albeit quite high). Typically about 100,000 data writing/updating cycles are possible, a number which is however sufficient for our experiments.

How to use the EEPROM on NodeMCU

The use of the EEPROM is quite simple and follows the one on the Arduino. The EEPROM library is used and must be included in the usual way:

#include <EEPROM.h>

The first thing to do is to initialize the EEPROM in the setup function:

EEPROM.begin(512)

Our device has 512 memory locations.

A data stored in a certain address can be read:

EEPROM.read(address)

In this case, the data stored (as bytes) at address address is returned. If the location at that address has never been written, the data returned will be a random value.

Or it can be written in a certain location:

EEPROM.write(address, value)

In this case the value value (as a byte) is written to the address address.

Data already written to a certain address can be updated using the command:

EEPROM.update(address, value)

The update function updates the data only if it is different from the present one. This way unnecessary writings are avoided and write cycles are saved (prolonging the life of the EEPROM).

To read any type of data or object (such as a float or a struct) present in a certain address we use the get function:

EEPROM.get(address, data)

To write any type of data or object to a given address (such as a float or a struct) we use the put function:

EEPROM.put(address, data)

To make the writing effective, it is always necessary to issue the commit command:

EEPROM.commit()

Examples of code for reading and writing the EEPROM

Let’s create the project for the writing sketch on PlatformIO

We have already seen in a previous article how to create a project using the excellent PlatformIO IDE. So let’s create our project following the indications of the article How to create a project for NodeMCU ESP8266 with PlatformIO. In this case we can skip the part about adding the libraries to the project as we won’t be using them.

Now let’s download the sample code:

Let’s overwrite the main.cpp file just downloaded on the one of the previously created project and load the sketch on the device.

Let’s analyze the code

Initially the libraries we need are included:

#include <Arduino.h>
#include <EEPROM.h>

Now let’s define the variables that contain the values ​​we want to store in the EEPROM:

String hhon = "16";                 
String mmon = "25";
String hhoff = "18";
String mmoff = "32";

.......

String timerStatusEE = "1";

We are imagining that we want to memorize the ON and OFF times of a device. The variables hhon and mmon contain the ON time (16:25) while the variables hhoff and mmoff contain the OFF time (18:32). The timerStatusEE variable contains a status, for example the status of the timer (ON or OFF) that we want to store in the EEPROM.

We also define the variables that contain the addresses of the EEPROM where we will store the values:

int hhon_address = 2;
int mmon_address = 4;
int hhoff_address = 6;
int mmoff_address = 8;

......

int timerStatusEE_address = 10;

We store the first variable (hhon) starting at address 2 (hhon_address). This variable has length 2 (cells), so we will store the next variable (mmon) at address 4. This variable also has length 2, so we will store the next (hhoff) at address 6 and so on.

Now let’s go to the setup function:

Serial.begin(9600);
delay(1000);
EEPROM.begin(512);  //Initialize EEPROM

First we initialize the serial port and the EEPROM.

Then we set up the writing of the variables:

for(int i = 0; i < hhon.length(); i++) {
    EEPROM.write(hhon_address + i, hhon[i]);
}

for(int i = 0; i < mmon.length(); i++) {
    EEPROM.write(mmon_address + i, mmon[i]);
}

for(int i = 0; i < hhoff.length(); i++) {
    EEPROM.write(hhoff_address + i, hhoff[i]);
}

for(int i = 0; i < mmoff.length(); i++) {
    EEPROM.write(mmoff_address + i, mmoff[i]);
}

EEPROM.write(timerStatusEE_address, timerStatusEE[0]);

Finally we finalize the writing using the commit function and print a message on the Serial Monitor:

EEPROM.commit();

Serial.println("Done!");

We leave the loop function empty as we don’t need it.

At this point the data has been stored in the EEPROM in the indicated addresses. This data will persist even after the device is turned off, and that’s what we’re going to check out shortly. Before moving on to the next step, let’s unplug the NodeMCU from the USB in order to interrupt its power supply.

Let’s create the project for the reading sketch on PlatformIO

We can now create another project related to reading the EEPROM as done in the previous point and replace, as usual, the main.cpp file with the one we will download from the link below:

Let’s reconnect the NodeMCU to the USB and load the sketch.

Let’s analyze the code

As usual we include the libraries:

#include <Arduino.h>
#include <EEPROM.h>

and initialize the variables into which we will put the values ​​read from the EEPROM as empty strings:

String hhonEE = "";
String mmonEE = "";
String hhoffEE = "";
String mmoffEE = "";

.......

String timerStatusEE = "";

Let’s set the addresses of the EEPROM from which we will have to extract the data with, obviously, the same values ​​of the writing sketch:

int hhonEE_address = 2;
int mmonEE_address = 4;
int hhoffEE_address = 6;
int mmoffEE_address = 8;

.....

int timerStatusEE_address = 10;

In the setup function we initialize the serial port and the EEPROM:

Serial.begin(9600);
delay(1000);
EEPROM.begin(512);  //Initialize EEPROM

Now let’s see the reading of the first value hhonEE:

hhonEE = char(EEPROM.read(hhonEE_address));
hhonEE += char(EEPROM.read(hhonEE_address + 1)); 

In the first line, the value located at hhonEE_address is read and stored in the variable hhonEE. In the second line, the value stored in the next location is read (because we know that the total value occupies 2 locations) and is concatenated with the previously read value to form the complete value in the hhonEE variable.

The same concept is used for reading the other variables:

mmonEE = char(EEPROM.read(mmonEE_address));
mmonEE += char(EEPROM.read(mmonEE_address + 1));

hhoffEE = char(EEPROM.read(hhoffEE_address)); 
hhoffEE += char(EEPROM.read(hhoffEE_address + 1));

mmoffEE = char(EEPROM.read(mmoffEE_address));
mmoffEE += char(EEPROM.read(mmoffEE_address + 1));

Finally there is the reading of the timerStatusEE variable, which occupies only one location, and the printing of a message on the Serial Monitor:

timerStatusEE = char(EEPROM.read(timerStatusEE_address));

In the loop function we have the cyclic printing (every 5 seconds) of the read variables:

Serial.println(hhonEE);
Serial.println(mmonEE);
Serial.println(hhoffEE);
Serial.println(mmoffEE);

Serial.println(timerStatusEE);

delay(5000);

If all went well we should see the values ​​we have stored with the writing sketch printed on the Serial Monitor, as shown in the following image:

Reading of the EEPROM of the NodeMCU module displayed on the Serial Monitor
Reading of the EEPROM of the NodeMCU module displayed on the Serial Monitor

An example of use in a real project

You can see a practical example of use on a real project by reading the How to make an automatic sprinkler controlled by Telegram with the ESP8266 NodeMCU article.

Newsletter

If you want to be informed about the release of new articles, subscribe to the newsletter. Before subscribing to the newsletter read the page Privacy Policy (UE)

If you want to unsubscribe from the newsletter, click on the link that you will find in the newsletter email.

Enter your name
Enter your email
0 0 votes
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
Scroll to Top