Reliable Settings Storage with an AT24C02 EEPROM Breakout
Remembering a calibration value after power is removed sounds simple: write it to EEPROM and read it at startup. In a finished project, the harder questions are what happens during an interrupted save, how firmware recognises an old record format and whether repeated updates will wear out the same memory locations.
An external memory board gives you a convenient place to develop those behaviours. This guide uses the AT24C02 EEPROM breakout board from the PMD Way memory breakout collection as a small settings-storage example. It focuses on a robust application design rather than treating one successful write as the finish line.
Count bytes before choosing a record format
The listed AT24C02 capacity is 2 kilobits, organised as 256 bytes. That is enough for a compact configuration, but it is not 2 kilobytes. Make a storage budget before adding long text fields or several historical copies.
For a classroom instrument, the useful settings might be a calibration offset, scale factor, display preference and a short device label. Keep temporary measurements in RAM unless the application actually needs them after restart.
Define an explicit byte layout with fixed-width values and a stated byte order. Saving the raw contents of a C structure can unintentionally depend on compiler padding or type sizes. A small encoding routine makes the file format easier to preserve when the program moves to another controller.
Verify the actual chip and bus connections
Check the full chip marking and board circuit before applying a datasheet. The Microchip AT24C01C/AT24C02C datasheet is a useful reference for the C revision; earlier suffixes and other capacities must be checked separately.
The store board includes pull-up resistors. Confirm which rail supplies them and whether that rail suits the controller's I2C pins. A memory device's supply range does not establish that a mixed-voltage bus is correctly connected.
Record the address-select and write-protect connections. An I2C scan confirms an acknowledgement at an address, not the memory capacity or write behaviour. Start by identifying one board on a short, otherwise uncomplicated bus.
Respect the physical write-page boundary
The AT24C02C has eight-byte write pages. A write that crosses a page boundary wraps within that page rather than continuing into the next one. Split longer records into page-bounded writes.
For that eight-byte example, a write beginning at address 6 has room for only two bytes before the boundary at address 8. To store five bytes, write two at 6–7, wait for completion, then write three at 8–10. Sending all five in one transaction risks changing other locations in the first page.
Base the chunk size on the remaining space in the current page, the bytes still to save and the controller library's buffer limit. Test addresses just before each boundary; a routine tested only at address zero can conceal this bug.
Separate bus success from completed storage
After the write transaction, the chip performs an internal programming cycle. The datasheet describes acknowledgement polling to detect completion. Bound the wait with a timeout; an absent or faulty device must not trap the whole application in an endless loop.
For Arduino, the official AVR Wire implementation shows separate transmission and read-request operations and a bounded buffer. Check the corresponding core for your board rather than assuming all Arduino-compatible platforms share identical limits.
Make the storage routine return a useful result: success, communication failure, completion timeout or readback mismatch. Pass that result to the user interface. Displaying “saved” before the operation is verified creates a misleading success message even if the bus transaction itself was accepted.
Give each saved record an identity
| Field | Purpose |
|---|---|
| Signature | Recognise a record belonging to this application |
| Format version | Identify how the following bytes should be decoded |
| Length | Reject impossible or incomplete record sizes |
| Sequence number | Distinguish successive valid saves |
| Settings payload | Hold the explicitly encoded configuration values |
| CRC or checksum | Detect accidental corruption within its stated coverage |
Specify exactly which bytes the integrity check covers, including the relevant header fields. A checksum is an error-detection aid, not encryption or a guarantee against every possible error.
After validating the record, validate the values too. A correctly encoded calibration value can still be outside the instrument's permitted range. Define a documented default configuration and a visible recovery message for a missing or rejected record.
Plan for an interrupted save
EEPROM retains completed data without power, but a write interrupted by power loss can corrupt the data being programmed. Microchip's high-reliability EEPROM counter note discusses redundant storage and readback verification as ways to improve recovery.
For a small settings application, consider two non-overlapping record slots on separate write pages. Keep the previous valid slot intact while writing the replacement. Read back and validate the replacement before treating it as the active configuration.
At startup, inspect both slots independently and choose the newest valid record using a defined sequence-number comparison, including rollover behaviour. If neither passes validation, use the recovery policy. Do not overwrite both slots as part of one save operation.
This is a design pattern to implement and test, not a promise of atomic storage under every fault. An inadequate supply, a software addressing error or a fault affecting both records can still defeat it. Include stable power and the exact device's operating requirements in the design.
Avoid writing on every loop iteration
Keep the current settings in RAM while the user adjusts them. Save on an intentional action or after a suitable period without further changes, and skip a save when the encoded record has not changed.
Count writes in a development build. A menu button that appears to save once might trigger many calls while held down. Repeatedly storing a status counter alongside otherwise unchanged settings can also defeat an update-only strategy.
Use the exact chip's endurance specification and its conditions when estimating life. For illustration, one save per second makes 86,400 saves per day; one per minute makes 1,440. These calculations describe software behaviour, not a lifetime guarantee for the board.
Test recovery deliberately
- Write known patterns at the beginning and end of each reserved region.
- Exercise writes that end at and cross physical page boundaries.
- Confirm that a normal restart restores the most recent verified settings.
- Interrupt power at different points during a save on a test setup and inspect both slots afterwards.
- Supply deliberately invalid versions, lengths and checksums to the decoder.
- Disconnect the memory and confirm that the application reports the problem without hanging.
Keep a diagnostic memory dump and a description of the record format with the project. The result should be a storage system whose failures are understandable: the application either restores a validated configuration or explains why it has fallen back to a known default.