Keeping a value
A pump's run-time meter counts minutes in a variable: minutes = 36. Variables live in SRAM, which needs power to hold its bits. Pull the battery and the 36 is gone: at the next start-up the meter reads 0.
EEPROM keeps its bytes with the power off: 1,024 bytes on the ATmega328P. When the pump stops, save() writes an 8-byte record into slot 0. Each byte takes about 3.3 ms, so the record takes about 26 ms.
Wear and flash
Every write wears an EEPROM cell a little, and the datasheet promises 100,000 write/erase cycles. Save once a minute into the same slot and it is used up in about 69 days. EEPROM.put skips bytes that have not changed, which helps a little.
So each save moves to the next slot, round a ring of 4. Its sequence number, a save counter stamped on it, grows by one. At 41 minutes record #2 goes to slot 1. 4 slots last 4 times longer; 128 slots, about 24 years.
Flash is bigger but erases in blocks: 128 bytes on the ATmega328P, 4,096 on an ESP32. Erasing sets every bit to 1; writing can only clear bits to 0, so 41 cannot become 44 in place. On an ESP32, Preferences manages this for you.
Surviving a power cut
The pump stops at 44 minutes and record #3 starts into slot 2. After 4 of its 8 bytes, about 13 ms in, the battery is pulled. Slot 2 now holds the new sequence number but a blank count and check value: a torn record.
At power on, load() reads each slot and works out the CRC, a check value computed from the other 6 bytes. Slots 0 and 1 match their stored CRC. Torn slot 2 and empty slot 3 do not, so they are ignored.
Of the good records, #2 has the highest sequence number, so minutes = 41 comes back into SRAM, and the next save goes to slot 2. The power cut cost 3 minutes of counting, not all 44.
What happens
;; step 1/8 · volatile minutes = 36 in SRAM, the chip's working memory SRAM needs power to hold each bit power off -> every bit lost power on -> globals start at 0: minutes = 0 the pump really ran 36 minutes; the meter forgot
On the ATmega328P the SRAM, the EEPROM and the flash are all inside the one chip; the figure draws them apart. The Arduino Uno R3 keeps your program in the 32 KB of flash. The datasheet advises enabling the Brown-out Detector so the chip is held in reset while the supply is too low, and says a byte write already in progress still completes if the voltage is enough. That protects one byte, not an 8-byte record, which is why the record carries a CRC.