Settings that survive a reboot (EEPROM and CRC)
Keep a settings struct in EEPROM with a CRC-8, fall back to defaults when it is blank or corrupt, and change it over Serial.
Firmware · Intermediate
In a real product: Smart thermostat (Smart home): The schedule and setpoints survive a power cut, and corrupt memory is never trusted.
Every product has settings: a brightness, a calibration offset, a device address. They must survive power cycles, and the firmware must never trust them blindly: a fresh chip has erased EEPROM, and a power cut in the middle of a write leaves half old and half new bytes. A version byte and a CRC catch both.
What you'll learn
- Laying out settings as a struct and storing it with
EEPROM.putandEEPROM.get - Computing a CRC-8 over the struct, without the CRC byte itself
- Validating on boot, and falling back to defaults when the check fails
- Rebooting from firmware with the watchdog, to prove the settings came back
How it works
The settings are one struct: a version, the LED's brightness and its blink period, then a CRC-8 (polynomial 0x07) of every byte before it. offsetof(Settings, crc) is the number of bytes the CRC covers, so the CRC field is never part of its own check.
On boot the sketch reads the struct back and recomputes the CRC. An erased EEPROM reads all 0xFF, which fails the check, so a new chip starts from the defaults; so does a struct from an older firmware, through the version byte.
Type into the Serial Monitor: b 40 sets the brightness, t 200 the blink period, save writes the struct with a fresh CRC, and reboot restarts the chip through the watchdog. After the reboot it prints Loaded from EEPROM with your values.
Try this
- Change the settings,
save, thenreboot: they come back. - Change them and
rebootwithout saving: the old ones come back. - Add a field to the struct and bump
VERSION: old saved settings are refused instead of misread. - Break the CRC on purpose by computing it over
sizeof(Settings): every boot now falls back to the defaults.
sketch.ino
// Settings in EEPROM, checked with a CRC-8: b <0-255> sets brightness, t <ms> the blink period, save stores them,
// reboot restarts through the watchdog. The LED on pin 9 shows them.
#include <EEPROM.h>
#include <avr/wdt.h>
#include <stddef.h>
const uint8_t VERSION = 1;
struct Settings {
uint8_t version;
uint8_t brightness;
uint16_t blinkMs;
uint8_t crc; // CRC-8 of every byte before it
};
Settings settings;
uint8_t crc8(const uint8_t *data, size_t length) {
uint8_t crc = 0;
for (size_t i = 0; i < length; i++) {
crc ^= data[i];
for (int bit = 0; bit < 8; bit++) crc = crc & 0x80 ? (crc << 1) ^ 0x07 : crc << 1;
}
return crc;
}
uint8_t checksum(const Settings &s) { return crc8((const uint8_t *)&s, offsetof(Settings, crc)); }
void load() {
EEPROM.get(0, settings);
if (settings.version == VERSION && settings.crc == checksum(settings)) {
Serial.println("Loaded from EEPROM");
return;
}
Serial.println("EEPROM blank or corrupt: using defaults");
settings = { VERSION, 128, 500, 0 };
}
void save() {
settings.crc = checksum(settings);
EEPROM.put(0, settings);
Serial.println("Saved");
}
void show() {
Serial.print("brightness ");
Serial.print(settings.brightness);
Serial.print(", blink ");
Serial.print(settings.blinkMs);
Serial.println(" ms");
}
void setup() {
MCUSR = 0;
wdt_disable();
Serial.begin(9600);
pinMode(9, OUTPUT);
load();
show();
}
void loop() {
if (Serial.available()) {
String line = Serial.readStringUntil('\n');
line.trim();
if (line.startsWith("b ")) settings.brightness = constrain(line.substring(2).toInt(), 0, 255);
else if (line.startsWith("t ")) settings.blinkMs = constrain(line.substring(2).toInt(), 50, 5000);
else if (line == "save") save();
else if (line == "reboot") {
Serial.println("Rebooting");
Serial.flush();
wdt_enable(WDTO_15MS);
while (true) {}
}
show();
}
bool on = (millis() / settings.blinkMs) % 2;
analogWrite(9, on ? settings.brightness : 0);
}