EEPROM

In this project, you'll learn how to read and write data to the EEPROM on the Arudino.

There may be times when you need to remember some data even after you turn off power to the Arduino. If this is the case then you'll need to save the data into the EEPROM. Like your thumb drive, the EEPROM is a non-volatile memory, meaning that it will remember the data stored in it even after power is turned off. However, the EEPROM memory has a specified life of approximately 100,000 write/erase cycles, so you may need to be careful about how often you write to it. If you have a write command inside a loop, it will go throught this life span very quickly.

Note that two extra commands need to be added for using the EEPROM on the ESP8266 Wifi board. See step 4 below.

The Arduino has a built-in EEPROM with a 1024 byte capacity.

Parts needed: Arduino
1 Need to use the EEPROM library.
2 Here's the code to write and read a number to the EEPROM using the commands write() and read(). The write() and read() commands write and read only one byte at a time. So they are more difficult to use if you are working with multi-byte data types such as an int or a double, and you might get what seems to be incorrect results if you don't understand what is happening and how to interpret it. See question 2 in the Experiments for an example.
#include <EEPROM.h>
void setup() {
  Serial.begin(9600);
  int address;  // a number between 0 and 1023
  byte value ;  // a number between 0 and 255
  byte readback;
  
  // writes the number 28 into address location 0 of the EERPOM
  address = 0;
  value = 28;
  EEPROM.write(address, value);

  // writes the number 75 into address location 1
  address = 1;
  EEPROM.write(address, 75);
  
  // reads the value from address location 0 of the EEPROM
  address = 0;
  readback = EEPROM.read(address);
  Serial.print("Read value ");
  Serial.println(readback);

  // reads the value from address location 1
  address = 2;
  readback = EEPROM.read(address);
  Serial.print("Read value ");
  Serial.println(readback);
}

void loop() {
}
3 Instead of using the write() and read() commands from question 2 above, here's the code to write and read any data type or object (and not just a single byte) to the EEPROM using the commands put() and get(). The put() and get() commands write and read any data type or object. So this is much easier to use.
#include <EEPROM.h>
void setup() {
  Serial.begin(9600);
  int address = 0;  // a number between 0 and 1023
  char text[12] = "Hello world";
  float value = 3.14;
  char readbackText[12];
  float readbackValue;

  
  // writes the 12 character string starting at address location 0
  address = 0;
  EEPROM.put(address, text);
  // writes the number 3.14 into address location 12
  address = sizeof(text);
  EEPROM.put(address, value);

  // reads the value from address location 0 of the EEPROM
  address = 0;
  EEPROM.get(address, readbackText);
  address = sizeof(text);
  EEPROM.get(address, readbackValue);
  Serial.print("Read text ");
  Serial.println(readbackText);
  Serial.print("Read value ");
  Serial.println(readbackValue);
}

void loop() {
}
4 Sample code to write and read different data types to EEPROM. To use this code for the ESP8266 Wifi board, two lines in the code (flagged with ******) need to be uncommented.
#include <EEPROM.h>
String ssidString = "your ssid";
String passwordString = "your password";
char ssid[30];
char password[30];
byte abyte = 'R';
char achar = 'f';
bool abool = true;
int aint = 23;
double adouble = 3.14159;
float afloat = 2.1828;
String astring = "Hello World";
char acharArray[12];
struct {
  String name;
  unsigned int age;
} astruct = {"Hwang", 21};
char lastchar = 'e';
unsigned int address = 0;  

void printSizeof() {
  Serial.println("Number of bytes needed to store different data types");
  Serial.print("Sizeof(byte) = ");
  Serial.println(sizeof(byte));  
  Serial.print("Sizeof(char) = ");
  Serial.println(sizeof(char));
  Serial.print("Sizeof(bool) = ");
  Serial.println(sizeof(bool));  
  Serial.print("Sizeof(int) = ");
  Serial.println(sizeof(int));  
  Serial.print("Sizeof(double) = ");
  Serial.println(sizeof(double));
  Serial.print("Sizeof(float) = ");
  Serial.println(sizeof(float));
  Serial.print("Sizeof(a string) = ");
  Serial.println(sizeof(astring));
  Serial.print("Sizeof(a struct) = ");
  Serial.println(sizeof(astruct));
}

void writeData() {
  // write data out to cache
  address = 0;
  // cannot write a string with variable length
  // need to write a char array of fix size
  // so convert from String to char array
  ssidString.toCharArray(ssid, 30);
  EEPROM.put(address, ssid);
  address = address + sizeof(ssid); // reserve 30 bytes for ssid
  passwordString.toCharArray(password, 30);
  EEPROM.put(address, password);
  address = address + sizeof(password); // reserve 30 bytes for ssid

  EEPROM.put(address, abyte);
  address = address + sizeof(byte);
  EEPROM.put(address, achar);
  address = address + sizeof(char);
  EEPROM.put(address, abool);
  address = address + sizeof(bool);
  EEPROM.put(address, aint);
  address = address + sizeof(int);
  EEPROM.put(address, adouble);
  address = address + sizeof(double);
  EEPROM.put(address, afloat);
  address = address + sizeof(float);
  // need to write a char array of fix size
  astring.toCharArray(acharArray, sizeof(acharArray));
  EEPROM.put(address, acharArray);
  address = address + sizeof(acharArray);
  EEPROM.put(address, astruct);
  address = address + sizeof(astruct);
  EEPROM.put(address, lastchar);

  // write data from cache to flash. need to do this to actually save the data
  // this is only needed for the ESP8266
  // EEPROM.commit();  // ****** uncomment this for the ESP8266
}

void readData() {
  // read back data
  address = 0;
  EEPROM.get(address, ssid);
  Serial.print("ssid: ");
  Serial.println(String(ssid));
  address = address + sizeof(ssid);
  EEPROM.get(address, password);
  Serial.print("password: ");
  Serial.println(password);
  address = address + sizeof(password);

  EEPROM.get(address, abyte);
  Serial.print("byte: ");
  Serial.println(abyte);
  address = address + sizeof(byte);

  EEPROM.get(address, achar);
  Serial.print("char: ");
  Serial.println(achar);
  address = address + sizeof(char);

  EEPROM.get(address, abool);
  Serial.print("bool: ");
  Serial.println(abool);
  address = address + sizeof(bool);

  EEPROM.get(address, aint);
  Serial.print("int: ");
  Serial.println(aint);
  address = address + sizeof(int);

  EEPROM.get(address, adouble);
  Serial.print("double: ");
  Serial.println(adouble, 6);
  address = address + sizeof(double);

  EEPROM.get(address, afloat);
  Serial.print("afloat: ");
  Serial.println(afloat, 6);
  address = address + sizeof(float);
  
  EEPROM.get(address, acharArray);
  Serial.print("acharArray: ");
  Serial.println(acharArray);
  address = address + sizeof(acharArray);

  EEPROM.get(address, astruct);
  Serial.print("astruct: ");
  Serial.print(astruct.name);
  Serial.print(" ");
  Serial.println(astruct.age);
  address = address + sizeof(astruct);

  EEPROM.get(address, lastchar);
  Serial.print("lastchar: ");
  Serial.println(lastchar);
}

void setup() {
  Serial.begin(9600);
  // specify how many bytes of EEPROM to use. Max is 4096
  // this is only needed for the ESP8266
  // EEPROM.begin(512); // ****** uncomment this for the ESP8266

  printSizeof();  // print out number of bytes needed for different data types

  Serial.println("\nWriting...");
  writeData();
  Serial.println("\nReading...");
  readData();
  Serial.println("Done");
}

void loop() {
}
5 Sample code to write and read WiFi connection credentials to EEPROM. This code is for the ESP8266 Wifi board.
#include <EEPROM.h>
char ssid[30];
char password[30];

/** Load WLAN credentials from EEPROM */
void loadCredentials() {
  EEPROM.begin(512);
  EEPROM.get(0, ssid);
  EEPROM.get(0+sizeof(ssid), password);
  char ok[2+1];
  EEPROM.get(0+sizeof(ssid)+sizeof(password), ok);
  EEPROM.end();
  if (String(ok) != String("OK")) {
    ssid[0] = 0;
    password[0] = 0;
  }
  Serial.println("Recovered credentials:");
  Serial.println(ssid);
  Serial.println(sizeof(ssid));
  Serial.println(strlen(password)>0?password:"");
  Serial.println(sizeof(password));
}

/** Store WLAN credentials to EEPROM */
void saveCredentials() {
  EEPROM.begin(512);
  EEPROM.put(0, ssid);
  EEPROM.put(0+sizeof(ssid), password);
  char ok[2+1] = "OK";
  EEPROM.put(0+sizeof(ssid)+sizeof(password), ok);
  EEPROM.commit();
  EEPROM.end();
}

void setup() {
  Serial.begin(115200);
  Serial.println("Ready");
  String ssidString = "your ssid";
  String passwordString = "your password";
  // convert from String to char array
  ssidString.toCharArray(ssid, 30);
  passwordString.toCharArray(password, 30);

  saveCredentials();
  loadCredentials();
}

void loop() {
}