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

#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()
{   
}

 

And now the read example

#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()
{ 
}

Output

Open the serial monitor
abC
testing eeprom

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*