Last Updated: 08/12/2021
Arduino Built in EEPROM
Components >> Arduino Built in EEPROM
Arduino Built in EEPROM
Although the Arduino restarts it's program when it is turned on, it is possible to save information to a small amount of built in EEPROM memory.
An UNO has 512 bytes of available storage, however if we are storing integers, each value requires 2 bytes so only 256 values. This may not sound a lot but is more than enough for many projects.
Other types of Arduino have different amounts of EEPROM, see Arduino EEPROM library reference for more information.
This example is part of the DCC Turntable project and shows how the memory is read from and written to.
With the turntable I wanted the system to remember the position of the exits of the turntable but I also wanted a couple of adjuster buttons to be able to tweak the positions so the new values are written to the EEPROM and read when the sketch restarts.
Arduino internal EEPROM needs to be used carefully, each position of EEPROM can only be overwritten approx 100,000 times before it wears out. For the turntable project this is not a problem as it may be updated once or twice a day but it means any form of data logging could quickly wear out the Arduino EEPROM.
If you want to store more data and more often I would suggest using an external EEPROM chip such as the 24LC256 that can store 256kb and will survive over 1,000,000 write cycles.
EEPROM.update is used instead of EEPROM.write as although the EEPROM.update is slightly slower it will only update the information stored if it is different so saving on write cycles and increasing the life of the EEPROM
Circuit Diagram
As the EEPROM is built in there is no circuit diagram
Example 1: EEPROMv1.ino
Click to Download code: EEPROMv1.ino
Simple sketch showing data being written and read from EEPROM
/* EEPROMv1
* Simple sketch that uses the Arduino built in EEPROM
*
* Part of the DCC Turntable Project at
* http://www.digitaltown.co.uk/project7ModelRailwayTurntable.php
*
* EEPROM is used to store the turntable exit positions, these are adjustable
* using a couple of push to contact buttons allowing positions to be turned over time.
* These values are stored in EEPROM so that they are updated when the Arduino restarts
*/
//EEPROM
//system will use built in eeprom as it should not be written to very much
//it has a limited lifecycle compared to external eeprom chips.
#include "EEPROM.h"
const int eepromOffset = 200;//this is the position of the first byte stored in EEPROM
//An UNO only has 512 bytes of EEPROM, I used 200 as a value to stop all my projects using the 1st byte
//This value can be set to 0 if you have not stored data in EEPROM before
//This array holds the positions for the number 1 end of the turntable
//A default value has been put in to populate the EEPROM
int trackPositions[4] = {247, 10747, 13047, 23547}; //default positions in steps of track exits from end 1
long eepromUpdatePeriod = 60000;//60 secs between updates...also used to turn the power off
unsigned long eepromUpdateTimer;
//update the eeprom with current trackpositions
void updateeeprom() {
int q;
//writing 4 integer values to EEPROM
for (q = 0; q < 4; q++) {
//EEPROM.update used as it only writes a value if it is different to the existing value.
//This prevents constant unrequired writes.
//As the step postion is an integer it needs to br written across 2 locations
//As each location can only store 1 byte
EEPROM.update((q * 2) + eepromOffset, highByte(trackPositions[q]));
EEPROM.update((q * 2) + eepromOffset + 1, lowByte(trackPositions[q]));
}
//Once we have written the values to the EEPROM we will read them back
geteepromdata();
}
void geteepromdata() {
int q;
//get the 4 bytes of data
byte getbytes[8];//2 bytes per position
Serial.println("getbytes[]: ");
//get the data from eeprom and store in array
for (q = 0; q < 8; q++) {
getbytes[q] = EEPROM.read(q + eepromOffset);
Serial.println( getbytes[q]);
}
Serial.println("trackPositions[]: ");
//now assemble the data and put into trackpositions[4]
for (q = 0; q < 4; q++) {
trackPositions[q] = (getbytes[q * 2] * 256) + (getbytes[(q * 2) + 1]);
Serial.println(trackPositions[q]);
}
}
void setup() {
Serial.begin(9600);
Serial.println("EEPROMv1...");
//updateeeprom();//comment this line out once the EEPROM has been updated for the first time
geteepromdata();
}
void loop() {
//This updates the EEPROM every 60 seconds, more than enough for turntable position updates.
if (millis() > eepromUpdateTimer) {
//reset the time period for the next update
eepromUpdateTimer = millis() + eepromUpdatePeriod;
//update the eeprom
updateeeprom();
}
}
Additional Resource Links
Lesson 6: Basic Numeric variables, boolean, byte, int, unsigned int, long , unsigned long
Lesson 7: delay() v's millis(), controlling timing of programs
Arduino EEPROM library reference
Comments
This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site. To add a comment or ask a question please email the address in this image: and use Arduino Built in EEPROM as a reference.