Learn using EEPROM in Arduino to store data. Electronically Erasable and Programmable ROM (EEPROM) is a type of ROM used to store data. The micro-controller in Arduino also has EEPROM in which data is stored even if Arduino is turned off. It can be used in cases where there is a need to permanently store sensor data of anything else.
The supported micro-controllers on the various Arduino and Genuino boards have different amounts of EEPROM: 1024 bytes on the ATmega328P, 512 bytes on the ATmega168 and ATmega8, 4 KB (4096 bytes) on the ATmega1280 and ATmega2560. The Arduino and Genuino 101 boards have an emulated EEPROM space of 1024 bytes.
So, let’s start using EEPROM in Arduino.
Different Functions in EEPROM library:
- write() – Write data bytes to the EEPROM.
- read() – Read data bytes from EEPROM.
- put() – Similar to write(), but can write any data types.
- get() – Similar to read(), but can read any data types.
- clear() – Clear the EEPROM by writing ‘0’.
- update() – Updates the data byte at specified memory location.
We are going to see the basic use of write() and read().
Code for EEPROM
We are going to take a string and write characters to each memory address. These characters will be converted into ASCII code and then stored in the memory location. We can iterate through memory locations using loops.
While reading data from memory locations, again we have to iterate through each memory location. Since data in EEPROM is in ASCII form, we have to use char() to convert those ASCII codes into respective strings.
Below is sample code for write() and read() functions of EEPROM in Arduino.
Code for Writing in EEPROM
Code for Reading in EEPROM
Output of EEPROM
You can see that we have stored the string “iotguider” in the EEPROM. While reading, characters with respect to their memory location are printed.