EEPROM (Electrically Erasable Programmable Read-Only Memory) allows you to store data permanently on an Arduino, even when power is lost. This is useful for storing settings, calibration values, or user preferences.
Basics of Using EEPROM in Arduino
- The EEPROM on most Arduino boards has 1,024 bytes (1KB) of storage (on ATmega328-based boards like the Uno).
- EEPROM has a limited write cycle (~100,000 writes per location), so avoid excessive writes.
- EEPROM stores data as bytes (0-255), but multiple bytes can represent integers, floats, and even strings.
We use the built-in EEPROM.h library to provide the functions to read and write to EEPROM.
Writing and Reading a Single Integer Value
This example stores and retrieves an integer value in EEPROM.
Writing an Integer to EEPROM
#include <EEPROM.h> int addr = 0; // EEPROM address to store the value int settingValue = 123; // Example setting to store void setup() { Serial.begin(9600); EEPROM.put(addr, settingValue); Serial.println("Setting saved to EEPROM."); } void loop() { }
Reading the Stored Integer
#include <EEPROM.h> int addr = 0; int readValue = 0; void setup() { Serial.begin(9600); EEPROM.get(addr, readValue); Serial.print("Stored Value: "); Serial.println(readValue); } void loop() { }
EEPROM.put() and EEPROM.get() work for different data types, including floats and structs.
Storing and Retrieving Multiple Settings
If you have multiple settings, use different EEPROM addresses.
Example: Saving Multiple Settings
#include <EEPROM.h> struct Settings { int brightness = 75; float volume = 0.8; bool mode = true; }; int addr = 0; // EEPROM starting address void setup() { Serial.begin(9600); Settings userSettings; userSettings.brightness = 100; userSettings.volume = 0.5; userSettings.mode = false; EEPROM.put(addr, userSettings); Serial.println("Settings saved."); } void loop() { }
Example: Retrieving the Settings
#include <EEPROM.h> struct Settings { int brightness; float volume; bool mode; }; int addr = 0; void setup() { Serial.begin(9600); Settings userSettings; EEPROM.get(addr, userSettings); Serial.print("Brightness: "); Serial.println(userSettings.brightness); Serial.print("Volume: "); Serial.println(userSettings.volume); Serial.print("Mode: "); Serial.println(userSettings.mode ? "ON" : "OFF"); } void loop() { }
Using a struct helps organize multiple values cleanly.
Storing a String in EEPROM
Since the EEPROM works with bytes, storing strings requires handling character arrays.
Writing a String
#include <EEPROM.h> void writeStringToEEPROM(int addr, const String &str) { for (int i = 0; i < str.length(); i++) { EEPROM.write(addr + i, str[i]); } EEPROM.write(addr + str.length(), '\0'); // Null terminator } void setup() { Serial.begin(9600); writeStringToEEPROM(0, "Arduino"); Serial.println("String saved."); } void loop() { }
Reading a String
#include <EEPROM.h> String readStringFromEEPROM(int addr) { String str = ""; char ch; while ((ch = EEPROM.read(addr++)) != '\0') { str += ch; } return str; } void setup() { Serial.begin(9600); String storedString = readStringFromEEPROM(0); Serial.print("Stored String: "); Serial.println(storedString); } void loop() { }
Stores and retrieves text, useful for names, messages, or small config settings.
Erasing EEPROM (Resetting Values)
To clear the EEPROM which can be useful when debugging, set all bytes to 255 (default empty state).
#include <EEPROM.h> void resetEEPROM() { for (int i = 0; i < EEPROM.length(); i++) { EEPROM.write(i, 255); } } void setup() { Serial.begin(9600); resetEEPROM(); Serial.println("EEPROM erased."); } void loop() { }
Useful for resetting stored settings.
Reducing EEPROM Wear
The EEPROM has a limited write lifespan (~100,000 writes per byte).
So you may want to reduce unnecessary writes:
1. Only write when the value changes:
#include <EEPROM.h> void updateEEPROM(int addr, int value) { int storedValue; EEPROM.get(addr, storedValue); if (storedValue != value) { // Only write if different EEPROM.put(addr, value); } } void setup() { Serial.begin(9600); updateEEPROM(0, 42); // Only updates if different } void loop() { }
2. Use EEPROM.update() instead of EEPROM.write() to avoid redundant writes.
EEPROM.update(address, value); // Only writes if value is different
Conclusion
Use EEPROM.put() / EEPROM.get() for integers, floats, and structs.
Store multiple settings by assigning different addresses.
Use custom functions to store strings efficiently.
Minimize EEPROM wear by only writing when values change.
Reset EEPROM when needed for debugging.