Read and write to the eeprom on the ESP8266

Description

The ESP8266 has 512 bytes of internal EEPROM, this could be useful if you need to store some settings, such as an IP address or some Wifi details

Code

The write example first

[codesyntax lang=”cpp”]

#include <EEPROM.h>

int addr = 0;

void setup()
{
  EEPROM.begin(512);  //Initialize EEPROM

  // write to EEPROM.
  EEPROM.write(addr, 'a');    
  addr++;                      //Increment address
  EEPROM.write(addr, 'b');   
  addr++;                      //Increment address
  EEPROM.write(addr, 'C');    

  //Write string to eeprom
  String sample = "testing eeprom";
  for(int i=0;i<sample.length();i++)
  {
    EEPROM.write(0x0F+i, sample[i]); //Write one by one with starting address of 0x0F
  }
  EEPROM.commit();    //Store data to EEPROM
}

void loop()
{   
}

[/codesyntax]

 

And now the read example

[codesyntax lang=”cpp”]

#include <EEPROM.h>

int addr = 0;
 
void setup()
{
  EEPROM.begin(512);  //Initialize EEPROM
  Serial.begin(9600); 
  Serial.println("");
  Serial.print(char(EEPROM.read(addr)));
  addr++;                      //Increment address
  Serial.print(char(EEPROM.read(addr)));
  addr++;                      //Increment address
  Serial.println(char(EEPROM.read(addr)));
 
  //Read string from eeprom (testing eeprom)
  String strText;   
  for(int i=0;i<14;i++) 
  {
    strText = strText + char(EEPROM.read(0x0F+i)); //Read one by one with starting address of 0x0F    
  }  
 
  Serial.print(strText);  //Print the text
}
 
void loop()
{ 
}

[/codesyntax]

Output

Open the serial monitor
abC
testing eeprom

2 COMMENTS

  1. Hi, can i ask a question about EEPROM?…Can EEPROM store some settings, such as an Buzzer, Flame Sensor , Wifi Details and LED code. Example if our IOT project are lose power and reboot, it can running / working well like ESP8266 Wifi Module can connect our default Wifi again and also Buzzer, Flame Sensor , LED code smoothly.

    Our IOT Project are like Application Fire Alarm using Wifi, like sending data value Flame Sensor from IOT Project to Our Application Fire Alarm (Phone). Our Application Fire Alarm (Phone) can give notification to user about Fire Has Been Detected.

LEAVE A REPLY

Please enter your comment!
Please enter your name here