EEPROM & Data Persistence: Storing Data Beyond Power Loss
Most Arduino projects run fine with temporary data that disappears when power is removed. But some projects need to save data that persists across power cycles.
The Arduino EEPROM library provides functions to read and write bytes of data to the microcontroller's built-in EEPROM, which retains data even when power is off.
Reading and Writing Single Bytes
#include <EEPROM.h>
const int addr = 0; // EEPROM address to read/write
void setup() {
Serial.begin(9600);
// Write a byte to address 0
byte valueToWrite = 42;
EEPROM.write(addr, valueToWrite);
// Read it back
byte valueRead = EEPROM.read(addr);
Serial.print("Written: ");
Serial.println(valueToWrite);
Serial.print("Read back: ");
Serial.println(valueRead);
}
void loop() {
// nothing here
}Writing Structured Data (EEPROM.put)
For more complex data, use EEPROM.put() to write structures:
struct UserSettings {
byte brightness; // 0–255
bool ledState; // true/false
unsigned long lastUpdate; // milliseconds
};
struct UserSettings settings;
const int settingsAddr = 0;
void setup() {
Serial.begin(9600);
// Initialize settings
settings.brightness = 128;
settings.ledState = false;
settings.lastUpdate = millis();
// Write to EEPROM
EEPROM.put(settingsAddr, settings);
// Read back
EEPROM.get(settingsAddr, settings);
Serial.print("Brightness: ");
Serial.println(settings.brightness);
Serial.print("LED state: ");
Serial.println(settings.ledState);
}
void loop() {
// nothing here
}Reading and Writing Multiple Bytes (EEPROM.update)
Note the difference between EEPROM.write() and EEPROM.update():
EEPROM.write()always writes (but EEPROM cells have limited write cycles — ~100,000)EEPROM.update()only writes if the value is different (extends EEPROM life)
void setup() {
Serial.begin(9600);
// Write only if different (extends EEPROM life)
EEPROM.update(0, 255);
}Storing Calibration Data
A common use case: storing sensor calibration offsets:
struct CalibrationData {
float offsetTemp; // Temperature offset
float offsetLight; // Light offset
int lastCalTime; // Unix timestamp of last calibration
};
CalibrationData cal;
const int calAddr = 0;
void saveCalibrationData() {
cal.offsetTemp = readTemperature();
cal.offsetLight = readLightLevel();
cal.lastCalTime = millis();
EEPROM.put(calAddr, cal);
}
void loadCalibrationData() {
EEPROM.get(calAddr, cal);
}Always read-back values after writing to EEPROM to confirm data integrity. EEPROM cells have a limited write cycle count (~100,000 writes), so use EEPROM.update() instead of EEPROM.write() when writing the same value repeatedly.
Key Takeaways
- Arduino Uno has 1024 bytes of built-in EEPROM
- Use
EEPROM.write(addr, value)to write a single byte - Use
EEPROM.read(addr)to read a single byte - Use
EEPROM.put(addr, struct)to write structured data - Use
EEPROM.get(addr, struct)to read structured data - Use
EEPROM.update()instead ofwrite()to extend EEPROM life (only write if changed) - Do not exceed the EEPROM size limits for your board
Resources
- Arduino EEPROM Library: https://www.arduino.cc/reference/en/language/functions/storage/eeprom/
- Video: Using EEPROM on Arduino - https://www.arduino.cc/en/Tutorial/BuiltInExamples/eeprom_clear
- Arduino.cc - EEPROM: https://www.arduino.cc/reference/en/language/functions/storage/eeprom/