ENع
←EEPROM and flash
Lesson 47 · Low power and reliability

Saving data in EEPROM and flash

14 min8 steps5 questionsAdvanced
About this lesson

SRAM forgets everything the moment the battery goes, so a device that must remember a count, a setting or a calibration copies it into non-volatile memory. Follow a pump run-time meter on an Arduino Uno R3 as it saves its total into the ATmega328P's 1,024-byte EEPROM: why each cell wears out after about 100,000 writes, how a ring of slots spreads the wear, why flash must be erased in blocks before it is written, and how a sequence number and a CRC let the firmware survive a power cut in the middle of a write. The lesson “Brown-out, resets and fault handling” explains what happens to the chip when the supply sags; this one is about keeping your data through it.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 47 · Interactive
01 / 08

SRAM forgets at power off

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.

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
Loading figure…
or ← →
Read the lesson step by step
  1. SRAM forgets at power off

    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.

    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
  2. EEPROM keeps it

    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.

    line 5    EEPROM.put(slot * 8, r);   slot 0, address 0
    record    struct Rec { uint32_t seq; uint16_t count; uint16_t crc; }
    seq       sequence number: counts the saves  ·  crc  check value (step 7)
    #1        seq 1  count 36  crc 0xca1a
    bytes     01 00 00 00 24 00 1a ca
    time      8 bytes x 3.3 ms = 26.4 ms
    EEPROM    1,024 bytes, kept with no power
  3. Each cell wears out

    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.

    endurance   100,000 write/erase cycles per cell
    one save a minute = 1,440 writes a day
    100,000 / 1,440 = 69.4 days
    EEPROM.put uses EEPROM.update: an unchanged byte is not rewritten
    but seq changes every save, so its low byte is always written
  4. Spread the writes round a ring

    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.

    line 2    Rec r = {++seq, n};      seq 1 -> 2
    line 4    slot = (slot + 1) % 4;  slot 0 -> 1
    #2        seq 2  count 41  crc 0x691e
    ring of 4     277.8 days at one save a minute
    whole EEPROM  1,024 / 8 = 128 slots -> 24.3 years
    this is wear levelling
  5. Flash: erase, then write

    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.

    erase     every bit -> 1     1111 1111  (0xff)
    write 41  clear bits         0010 1001
    then 44   needs a 0 -> 1     0010 1100  not allowed
    blocks    ATmega328P: 128-byte pages · ESP32: 4096-byte sectors
    endurance ATmega328P flash 10,000 cycles (EEPROM 100,000)
    ESP32 Preferences (NVS) appends records and erases
    whole sectors later, spreading the wear
  6. Power fails mid-write

    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.

    #3 wants  03 00 00 00 2c 00 1c e8
    written   03 00 00 00 ff ff ff ff
              4 bytes x 3.3 ms = 13.2 ms, then no power
    slot 2    seq 3  count 65535  crc 0xffff
    the datasheet only promises a byte in progress finishes if the voltage holds
    (use the Brown-out Detector); a record is 8 bytes either way
  7. Check every slot at start-up

    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.

    slot 0  crc16 = 0xca1a  stored 0xca1a  ok
    slot 1  crc16 = 0x691e  stored 0x691e  ok
    slot 2  crc16 = 0x9801  stored 0xffff  bad
    slot 3  crc16 = 0x9401  stored 0xffff  bad
    crc16 = avr-libc _crc16_update, start 0xFFFF
  8. The newest good record wins

    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.

    good      #1 (36)  #2 (41)
    newest    #2 in slot 1
    load()    seq = 2, slot = 1, minutes = 41
    next save -> slot 2, overwriting the torn record
    lost      44 - 41 = 3 minutes

Quiz Unlocks after the last step.