Try Embedded

Settings in an external EEPROM (24LC256)

Store a settings record with a checksum in a 24LC256 I²C EEPROM, using page writes and ACK polling.

Logic · Intermediate

In a real product: Industrial controller (Industrial): Settings and counters live in an external EEPROM with a checksum.

Settings, calibration values and logs must survive a power cut. A 24LC256 adds 32 KB of non-volatile memory on the I²C bus, and using it well means respecting its pages and its write time.

What you'll learn

  • Addressing an EEPROM: the chip's I²C address and a two-byte memory address
  • Page writes, and why a write must not cross a 64-byte page boundary
  • Waiting for the write cycle with ACK polling, and guarding data with a checksum

How it works

The chip answers at 0x50 plus its A0–A2 pins. A write sends the memory address, then the data; the chip stores it when the STOP arrives, which takes about 5 ms. During that time it does not acknowledge its address, so waitReady() polls until it does. That is faster than a fixed delay(5) and correct for every chip.

The settings record is a struct with a checksum byte. On start the sketch reads it back; a wrong checksum (an erased chip reads all 0xFF) means "no settings yet", so it writes defaults. Each start then counts one more boot.

The two 4.7 kΩ resistors from SDA and SCL to 5V are the bus's pull-ups. I²C devices only ever pull the lines low, so something must pull them high again; a breakout module carries its own pull-ups, but this bare chip has none.

Try this

  • Run it twice: the boot count goes up, as it would across power cycles. (Here the chip starts erased each run, so it restarts from 1; press the board's reset button while it runs instead.)
  • Tie WP to 5V: writes are ignored, so the checksum stays wrong and the count never moves.
  • Make the record bigger than 64 bytes and split its write at page boundaries.

sketch.ino

// A settings record in a 24LC256 at 0x50: read it on start, count a boot, write it back with ACK polling.
#include <Wire.h>

const uint8_t EEPROM_ADDR = 0x50;

struct Settings {
  uint32_t boots;
  uint16_t threshold;
  uint8_t brightness;
  uint8_t checksum;
};

uint8_t sum(const Settings &s) {
  const uint8_t *p = (const uint8_t *)&s;
  uint8_t c = 0;
  for (size_t i = 0; i < sizeof s - 1; i++) c += p[i];
  return ~c;
}

void waitReady() {
  do Wire.beginTransmission(EEPROM_ADDR);
  while (Wire.endTransmission() != 0);
}

void writeBlock(uint16_t at, const void *data, uint8_t n) {   // n <= 30 and within one page
  Wire.beginTransmission(EEPROM_ADDR);
  Wire.write(at >> 8);
  Wire.write(at & 0xFF);
  Wire.write((const uint8_t *)data, n);
  Wire.endTransmission();
  waitReady();
}

void readBlock(uint16_t at, void *data, uint8_t n) {
  Wire.beginTransmission(EEPROM_ADDR);
  Wire.write(at >> 8);
  Wire.write(at & 0xFF);
  Wire.endTransmission();
  Wire.requestFrom(EEPROM_ADDR, n);
  for (uint8_t i = 0; i < n && Wire.available(); i++) ((uint8_t *)data)[i] = Wire.read();
}

void setup() {
  Serial.begin(9600);
  Wire.begin();
  Settings s;
  readBlock(0, &s, sizeof s);
  if (s.checksum != sum(s)) {
    Serial.println("No settings yet: writing defaults");
    s = {0, 512, 7, 0};
  }
  s.boots++;
  s.checksum = sum(s);
  writeBlock(0, &s, sizeof s);
  Serial.print("Boot number ");
  Serial.print(s.boots);
  Serial.print(", threshold ");
  Serial.print(s.threshold);
  Serial.print(", brightness ");
  Serial.println(s.brightness);
}

void loop() {}

Lessons behind it