Arduino EEPROM : Explained

 


Introduction:-

This tutorial explains what is the Arduino EEPROM and what it is useful for. We’re also going to show you how to write and read from the EEPROM and build a project example to put the concepts learned into practice.

When you define and use a variable, the  generated data within a sketch only lasts as long as the Arduino is on. If you reset or power off the Arduino, the data stored disappears.

If you want to keep the data stored for future use you need to use the Arduino EEPROM. This stores the variable’s data even when the Arduino resets or the power is turned off.

What is EEPROM?

The microcontroller on the Arduino board (ATMEGA328 in case of Arduino UNO, shown in figure below) has EEPROM (Electrically Erasable Programmable Read-Only Memory). This is a small space that can store byte variables.

The variables stored in the EEPROM kept there, event when you reset or power off the Arduino. Simply, the EEPROM is permanent storage similar to a hard drive in computers.

The EEPROM can be read, erased and re-written electronically. In Arduino, you can read and write from the EEPROM easily using the EEPROM library.

How many bytes can you store?

Each EEPROM position can save one byte, which means you can only store 8-bit numbers, which includes integer values between 0 and 255.

The bytes you can store on EEPROM dependson the microcontrollers on the Arduino boards. Take a look at the table below:

Microcontroller   EEPROM
ATmega328 (Arduino Uno, Nano, Mini)   1024 bytes
ATmega168 (Arduino Nano)   512 bytes
ATmega2560 (Arduino Mega)   4096 bytes

However, if you need to store more data you can get an external EEPROM.

The EEPROM's finite life:-

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.

Read and Write:-

You can easily read and write into the EEPROM using the EEPROM library.

To include the EEPROM library:

#include <EEPROM.h>

Write

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 9 on address 0, you’ll have:

EEPROM.write(0, 9);

Read

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 a value

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 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.

 

Example: Using EEPROM to remember stored LED State

In this example, we’re going to show you how to make the Arduino remember the stored LED state, even when we reset the Arduino or the power goes off.

The following figure shows what we’re going to be doing:

Circuit:-

 

Code:-

https://gist.github.com/TheMicroKidBlog/cc77182ca06f0af46c74ecb7275b92d7#file-nano_eeprom_debounce 

Demo:-

 

 

Conclusion:-

In this post you’ve learned about the Arduino EEPROM and what it is useful for. You’ve created an Arduino sketch that remembers the last LED state even after resetting the Arduino.


 

Comments

Post a Comment

Popular Posts