Arduino EEPROM For Absolute Beginners

EEPROM is like a small hard drive which can store small amounts of data even when there is no electricity.


What is EEPROM?chip icon

The microcontroller on the Arduino and Genuino AVR based board has EEPROM: memory whose values are kept when the board is turned off (like a tiny hard drive).

EEPROM stands for electrically erasable programmable read-only memory and is a type of non-volatile memory used in computers, integrated in microcontrollers for storing small amounts of data even when the power is turned off like a small hard drive.



Memory capacity

Microcontroller Internal EEPROM(Memory Capacity)
ATmega328 (Arduino UNO,Nano,Mini)1024 bytes
ATmega2560 (Arduino Mega)4096 bytes

Life of EEPROM

The EEPROM has a finite life. In Arduino, the EEPROM is specified to handle 100 000 write/erase cycles for each position. However, reads are unlimited. This means you can read from the EEPROM as many times as you want without compromising its life expectancy.


How to use

To access EEPROM in Arduino we need to use this library #include <EEPROM.h>.

Functions

Below are the basic functions to write, read, update and clear the memory.

Write function ✍️

To write data into the EEPROM, you use the EEPROM.write() function that takes in two arguments. The first one is the EEPROM location or address where you want to save the data, and the second is the value we want to save:

EEPROM.write(address, value);

For example, to write 2 on address 0, you’ll have:

EEPROM.write(0, 9);

Read function 📖

To read a byte from the EEPROM, you use the EEPROM.read() function. This function takes the address of the byte has an argument.

EEPROM.read(address);

For example, to read the byte stored previously in address 0.:

EEPROM.read(0);

This would return 9, which is the value stored in that location.

Update function 📚

The EEPROM.update() function is particularly useful. It only writes on the EEPROM if the value written is different from the one already saved.

As the EEPROM has limited life expectancy due to limited write/erase cycles, using the EEPROM.update() function instead of the EEPROM.write() saves cycles.

You can use the EEPROM.update() function as follows:

EEPROM.update(address, value);

At the moment, we have 9 stored in the address 0. So, if we call:

EEPROM.update(0, 9);

It won’t write on the EEPROM again, as the value currently saved is the same we want to write.


How to clear memory 🧻

For clearing the EEPROM there is no predefined functions so what we need to do is update all the address with the value using a loop.

for (int i = 0 ; i &lt; EEPROM.length() ; i++) {
    EEPROM.write(i, 0);
  }

There are some more functions in the Arduino EEPROM library like EEPROM.get( ), EEPROM.put( ) which we will be learning in the next lesson.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top