Sub topic 12
Saving Small Values in EEPROM
This chapter assumes you can write basic Arduino sketches, use C/C++ structs, and read values in the Serial Monitor. It focuses on saving configuration data that must survive a reset or power loss.
What EEPROM is for
EEPROM is a small, non-volatile memory area. Non-volatile means it keeps its contents when the board loses power. An Arduino Uno has 1 KB of EEPROM.
Use EEPROM for small values such as:
- A temperature limit
- A selected operating mode
- A calibration offset
- A sampling interval
- A counter that changes occasionally
EEPROM is separate from flash and SRAM:
| Memory | Keeps data without power? | Typical use |
|---|---|---|
| Flash | Yes | Program code and constant data |
| SRAM | No | Variables while the sketch runs |
| EEPROM | Yes | Small persistent settings |
EEPROM is slower than SRAM and has a limited write life. On typical AVR boards, each EEPROM byte supports about 100,000 write cycles. Rewriting a value on every pass through loop() can wear it out quickly.
Store a setting with EEPROM.put()
The EEPROM library provides put() and get() for storing complete values. A struct groups related settings into one object.
#include <EEPROM.h>
struct Settings {
uint16_t interval;
uint8_t mode;
};
const int EEPROM_ADDRESS = 0;
void setup() {
Serial.begin(9600);
Settings defaults = {1000, 2};
EEPROM.put(EEPROM_ADDRESS, defaults);
Settings loaded;
EEPROM.get(EEPROM_ADDRESS, loaded);
Serial.print(F("Interval: "));
Serial.println(loaded.interval);
Serial.print(F("Mode: "));
Serial.println(loaded.mode);
}
void loop() {
}The first argument to put() or get() is the starting EEPROM address. The object occupies several consecutive bytes, so the address must leave enough room for the entire object.
EEPROM.put() uses update behavior internally. It writes only bytes whose values changed. This reduces unnecessary wear compared with repeatedly calling EEPROM.write().
Validate data after a power loss
EEPROM can contain old data when you install a new sketch. It can also contain an incomplete value if power is removed during a write. Add a marker and a version number so the sketch can recognize valid settings.
#include <EEPROM.h>
struct Settings {
uint16_t marker;
uint8_t version;
uint16_t interval;
uint8_t mode;
};
const int EEPROM_ADDRESS = 0;
const uint16_t EXPECTED_MARKER = 0x4B52;
const uint8_t EXPECTED_VERSION = 1;
Settings settings;
void loadSettings() {
EEPROM.get(EEPROM_ADDRESS, settings);
if (settings.marker != EXPECTED_MARKER ||
settings.version != EXPECTED_VERSION) {
settings.marker = EXPECTED_MARKER;
settings.version = EXPECTED_VERSION;
settings.interval = 1000;
settings.mode = 2;
EEPROM.put(EEPROM_ADDRESS, settings);
}
}
void setup() {
Serial.begin(9600);
loadSettings();
Serial.println(settings.interval);
Serial.println(settings.mode);
}
void loop() {
}When the structure changes, increase EXPECTED_VERSION. The sketch then replaces the old layout with safe defaults.
Exercise
Exercise
Why should a sketch use EEPROM for a configuration value instead of SRAM, and why should it avoid writing that value every millisecond?
Show answer
EEPROM preserves the value through power loss, while SRAM does not. EEPROM writes are limited, so writing every millisecond would wear the memory quickly.