fbpixel
Tags:

The EEPROM is an internal memory of the ESP8266 microcontroller which allows to keep in memory data after restarting the card. When working with microcontrollers, it is interesting to keep in memory data such as the identifier and the password of the Wifi.

Material

  • Computer
  • NodeMCU ESP8266
  • USB A Male/Micro B Male Cable

Principle of operation

The ESP8266 microcontroller has a Flash memory area to simulate the EEPROM of the Arduino. This is a special memory location in the microcontroller where data remains in memory even after the board is turned off. One important thing to note is that the EEPROM has a limited size and life span. The memory cells can be read as many times as necessary but the number of write cycles is limited to 100,000. It is advisable to pay close attention to the size of the stored data and how often you want to update it. The overall flash memory size is usually 4MB. The EEPROM of the ESP8266 has a size of 4kB.


If you want to record real-time data from a fleet of sensors to plot curves, it is best to go for an SD card module to store the data.

Code with the EEPROM library

To interface with the EEPROM of the ESP8266, we can use the EEPROM.h library, very similar to the one for Arduino with some differences. Before using the function, we have to initialize the size of the memory with begin() and the update function does not exist but the write function does something similar.

  • begin() to initialize the memory size
  • put() to write
  • get() to read
  • commit() to validate changes

Other functions of the library can be used depending on the use you have of the EEPROM.

//Libraries
#include <EEPROM.h>//https://github.com/esp8266/Arduino/blob/master/libraries/EEPROM/EEPROM.h

//Constants
#define EEPROM_SIZE 12

void setup() {
  //Init Serial USB
  Serial.begin(115200);
  Serial.println(F("Initialize System"));
  //Init EEPROM
  EEPROM.begin(EEPROM_SIZE);

  //Write data into eeprom
  int address = 0;
  int boardId = 18;
  EEPROM.put(address, boardId);
  address += sizeof(boardId); //update address value

  float param = 26.5;
  EEPROM.put(address, param);
  EEPROM.commit();

  //Read data from eeprom
  address = 0;
  int readId;
  EEPROM.get(address, readId);
  Serial.print("Read Id = ");
  Serial.println(readId);
  address += sizeof(readId); //update address value

  float readParam;
  EEPROM.get(address, readParam); //readParam=EEPROM.readFloat(address);
  Serial.print("Read param = ");
  Serial.println(readParam);

  EEPROM.end();
}

void loop() {}

Result

The read values correspond to the stored values. You can remove the writing part and run the code again to check that the values are well kept in memory.

arduino-monitor-eeprom-result Using the EEPROM with the ESP8266

Applications

  • Keep in memory the ssid and password of the Wifi network on an ESP8266

Sources

Retrouvez nos tutoriels et d’autres exemples dans notre générateur automatique de code
La Programmerie