Arduino

How to Use EEPROM library in Arduino

In Arduino programming when a sketch is uploaded to the Arduino board it remains there and is stored in the Arduino until it is erased or another sketch is uploaded. This data is stored in the EEPROM built in the Arduino. The use of EEPROM and its library is explained in this following context.

EEPROM of Arduino

This read only memory other than storage gives the option of editing the contents of the sketch using its functions. Similarly, this memory was designed to avoid the problem of difficulty of erasing data that was intercepted in the past. The EEPROM sizes of different Arduino boards are mentioned below:

Controller Size
Arduino Uno, Arduino Nano, Arduino Mini (ATmega328) 1024 bytes
Arduino Nano (ATmega168) 512 bytes
Arduino Mega (ATmega2560) 4096 bytes

The acronym EEPROM stands for “Electronically Erasable Permanent Read Only Memory“. There are 8 types of function that can be performed using the EEPROM library. This library already comes with the Arduino IDE software so there is no need to install the library:

Write function of EEPROM

When the data is to be saved in any address it can be done by using the EEPROM.write() function. The data will be stored until it is erased or updated.

In the code first the library for the memory is initialized and then the variable for address is declared and, in the loop, the EEPROM.write() function is used to write to the value on the address.

After each iteration the address changes and the same value is added to all the addresses of the EEPROM. Similarly, the data saved using the write function.

The program will run until the addresses become equal to the total length of the EEPROM and the length of the memory varies from board to board. In Arduino Uno it is 1 kilo bytes so the program will run when all the 1000 addresses have given the value of 200.

#include <EEPROM.h>
int address = 0;
int value = 200;
void setup() {
  Serial.begin(9600);
}
void loop() {
  EEPROM.write(address, value);
  Serial.print("This address:");
  Serial.println(address);
  Serial.print("has value of ");
  Serial.println(value);
  Serial.println();
  address = address + 1;
  if (address == EEPROM.length()) {
    address = 0;
  }
  delay(500);
}

Output

Read function of EEPROM

To read any data from any address of the memory the EEPROM.read() function is used. To further describe the working of the EEPROM.read() function an example code is given.

Since in the previous program we have given the value of 200 to each address of the memory so when we read each address of the memory using the EEPROM.read() function it displays the same output:

#include <EEPROM.h>
int address = 0;
byte value;
void setup() {
  Serial.begin(9600);
}
void loop() {
  value = EEPROM.read(address);
  Serial.print("This address:");
  Serial.println(address);
  Serial.print("has value of ");
  Serial.println(value);
  Serial.println();
  address = address + 1;
  if (address == EEPROM.length()) {
    address = 0;
  }
  delay(500);
}

Output

Put function of EEPROM

To store the data in the form of an array or the data is of float type then the EEPROM.put() function is used. To understand the use of EEPROM.put() function it is further explained using a simple Arduino program.

In the code first the value having float data type is stored in the address 0 of the memory and then a structure is constructed of the name data which has a byte type value, a float type value, and a character value.

The size for the whole structure is 12 bytes having 4 bytes for the integer and float type values and 8 bytes for the character value.

The address for the float type is initialized as zero whereas the address for the structure is after the next byte found after the float value.

#include <EEPROM.h>
struct data {
  float value1;
  byte value2;
  char word[8];
};
void setup() {
  Serial.begin(9600);
  float f = 967.817;
  int eeAddress = 0;  
  EEPROM.put(eeAddress, f);
  Serial.print("This address:");
  Serial.println(eeAddress);
  Serial.print("has float value  of ");
  Serial.println(f);
  Serial.println();
 data values= {
    2.65,
    89,
    "Hello!"
  };
  eeAddress += sizeof(float);
  EEPROM.put(eeAddress, values);
  Serial.print("This address:");
  Serial.print(eeAddress);
  Serial.print('\t');
  Serial.print("has structure having the information:");
  Serial.println();
  Serial.println(values.value1);
  Serial.println(values.value2);
  Serial.println(values.word);
}
void loop() {
}

Output

Get function of EEPROM

To retrieve the data stored in float data types or in the form of structure the get function is used. This function is different from the simple read and write function. The example for the use of EEPROM.get() function  provided which will give a clear  concept of the function:

#include <EEPROM.h>
void setup() {
  float f = 0.00;  
  int eeAddress = 0;
  Serial.begin(9600);
  Serial.print("Read float from EEPROM: ");
  EEPROM.get(eeAddress, f);
  Serial.println(f, 4);    
  structurevalues();
}
struct data {
  float value1;
  byte value2;
  char word[8];
};
void structurevalues() {
  int eeAddress = sizeof(float);
  data values;
  EEPROM.get(eeAddress, values);
  Serial.println("Read structure from EEPROM: ");
  Serial.println(values.value1);
  Serial.println(values.value2);
  Serial.println(values.word);
}
void loop() {
}

Here in the code a float value and a structure value stored in the Arduino memory is fetched which was previously stored using the EEPROM.put () function.

 Output

Update function of EEPROM

When data on any address needs to be updated the EEPROM.update() function is used. This function is only used when there is already some data on the respective address. Similarly, this function only updates the data if it is different from previously saved data.

#include <EEPROM.h>
int address = 4;
int value;
int value1=300;
void setup() {
  Serial.begin(9600);
   value = EEPROM.read(address);
  Serial.print("This address:");
  Serial.println(address);
  Serial.print("previously value of ");
  Serial.println(value);
  Serial.println();  
  EEPROM.update(address, value1);
  Serial.print("This address:");
  Serial.println(address);
  Serial.print(" updated value of ");
  Serial.println(value1);
  Serial.println();
}
void loop() {

In the example code the data on the address 4 is updated as the previous value at this address was 44. The data of address 4 was changed from 44 to 300.

For demonstration purposes the EEPROM.read() function is used to fetch the data stored in address 4 and then an updated value is stored in address 4 by using the EEPROM.update() function.

Output

Conclusion

Libraries in Arduino programming are mostly used to get some extra functionalities of the hardware interfaced. The EEPROM is the memory of the Arduino board which can be accessed using the EEPROM.h library. By using its functions, the data stored in the Arduino can be edited or erased. This write-up explains five main functions that can be used  to edit or erase the data of Arduino.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.