Remember settings with an EEPROM
Build it step by step
0 of 8 doneWire the I2C bus
A4 to both SDA pins, A5 to both SCL pins.
Wire the chip address pins
A0, A1, A2 and WP to GND.
Wire the buttons
D2 and D3 each to GND.
Upload the sketch
The display shows Count: 0.
Press the count button a few times
The number goes up.
Unplug the USB cable and plug it in again
The same count returns.
Press the reset button
The count goes to zero and stays zero after a restart.
Try holding a button
Only one count per press.
How do you build a remember settings with an EEPROM?
A small I2C EEPROM remembers a counter and settings even after the battery is removed. You need Arduino Nano, AT24C256 EEPROM module, Tactile push button 6 mm × 2, 16x2 LCD with I2C backpack (PCF8574), Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 60 minutes.
- Level
- Beginner
- Time
- About 60 min
- Parts
- 6
Wiring
| From | To | Why |
|---|---|---|
| Nano A4 (SDA) | EEPROM SDA and LCD SDA | Shared I2C data |
| Nano A5 (SCL) | EEPROM SCL and LCD SCL | Shared I2C clock |
| Nano 5V and GND | EEPROM and LCD power | Power |
| EEPROM A0, A1 and A2 | GND | Address 0x50 |
| EEPROM WP | GND | Writes allowed |
| Count button | Nano D2 to GND | Add one to the counter |
| Reset button | Nano D3 to GND | Set the counter to zero |
Code
The AT24C256 is a 32 kilobyte EEPROM chip on the I2C bus. Its memory is kept without power, but each cell wears out after about a million writes, so writing the same cell on every button press would eventually fail. This sketch spreads the wear: it stores the counter in a ring of 128 four-byte records and writes each new value to the next slot. Each record holds the counter, a sequence number that goes up with every save, and a checksum. At power on the sketch reads every slot, ignores blank or damaged ones using the checksum, and picks the one with the newest sequence number, so it finds the last saved value even after the battery was removed. The comparison of sequence numbers works across the wrap from 255 back to 0. After each write the chip is busy for a few milliseconds, so the code polls it until it answers again. With 128 slots the same count of writes is shared 128 ways, extending the life of the chip by that factor. Pressing the reset button saves a zero. The write protect pin is tied to ground so writes are allowed.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // try 0x3F if blank
const uint8_t EEPROM_ADDR = 0x50;
const int SLOTS = 128; // 128 records of 4 bytes, from address 0 to 511
const int COUNT_BTN = 2, RESET_BTN = 3;
uint16_t counter = 0;
uint8_t seq = 0;
int slot = 0;
uint8_t checksum(uint8_t lo, uint8_t hi, uint8_t s) { return lo ^ hi ^ s ^ 0xA5; }
void eepromRead(uint16_t addr, uint8_t* buf, uint8_t n) {
Wire.beginTransmission(EEPROM_ADDR);
Wire.write(addr >> 8); Wire.write(addr & 0xFF);
Wire.endTransmission();
Wire.requestFrom(EEPROM_ADDR, n);
for (uint8_t i = 0; i < n && Wire.available(); i++) buf[i] = Wire.read();
}
void eepromWrite(uint16_t addr, const uint8_t* buf, uint8_t n) {
Wire.beginTransmission(EEPROM_ADDR);
Wire.write(addr >> 8); Wire.write(addr & 0xFF);
for (uint8_t i = 0; i < n; i++) Wire.write(buf[i]);
Wire.endTransmission();
while (true) { // wait for the internal write to finish
Wire.beginTransmission(EEPROM_ADDR);
if (Wire.endTransmission() == 0) break;
delay(1);
}
}
void saveRecord() {
slot = (slot + 1) % SLOTS; // spread writes over all slots to share the wear
seq++;
uint8_t rec[4] = { (uint8_t)(counter & 0xFF), (uint8_t)(counter >> 8), seq,
checksum(counter & 0xFF, counter >> 8, seq) };
eepromWrite(slot * 4, rec, 4);
}
void loadLatest() {
bool found = false; uint8_t bestSeq = 0;
for (int i = 0; i < SLOTS; i++) {
uint8_t rec[4];
eepromRead(i * 4, rec, 4);
if (rec[3] != checksum(rec[0], rec[1], rec[2])) continue; // blank or damaged slot
if (!found || (int8_t)(rec[2] - bestSeq) > 0) { // newest sequence number wins
found = true; bestSeq = rec[2]; slot = i;
counter = rec[0] | (rec[1] << 8); seq = rec[2];
}
}
if (!found) { counter = 0; seq = 0; slot = SLOTS - 1; }
}
void draw() {
lcd.setCursor(0, 1);
lcd.print("Count: ");
lcd.print(counter);
lcd.print(" ");
}
void setup() {
pinMode(COUNT_BTN, INPUT_PULLUP);
pinMode(RESET_BTN, INPUT_PULLUP);
Wire.begin();
lcd.init(); lcd.backlight();
lcd.setCursor(0, 0); lcd.print("Saved counter");
loadLatest();
draw();
}
void loop() {
if (digitalRead(COUNT_BTN) == LOW) {
counter++; saveRecord(); draw();
while (digitalRead(COUNT_BTN) == LOW) {}
delay(40);
}
if (digitalRead(RESET_BTN) == LOW) {
counter = 0; saveRecord(); draw();
while (digitalRead(RESET_BTN) == LOW) {}
delay(40);
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Five presses | Count 5 |
| Power off and on | Count still 5 |
| Reset then power cycle | Count 0 |
| Blank chip at first start | Count 0 |
| Corrupt a record | Previous valid value used |
If it does not work
Display blank
Try address 0x3F and check the contrast screw.
Count resets on restart
Check the EEPROM wiring and address pins.
Wrong number after restart
Check the sequence and checksum code.
Chip not found
Run an I2C scanner, address should be 0x50.
Slow after each press
Normal, the code waits for the write to finish.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the remember settings with an EEPROM?
Arduino Nano, AT24C256 EEPROM module, Tactile push button 6 mm × 2, 16x2 LCD with I2C backpack (PCF8574), Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the remember settings with an EEPROM take to build?
About 60 minutes for a beginner maker, in 8 steps.
How do I know the remember settings with an EEPROM works?
Five presses: Count 5. Power off and on: Count still 5. Reset then power cycle: Count 0.
Display blank: what should I check?
Try address 0x3F and check the contrast screw.
Count resets on restart: what should I check?
Check the EEPROM wiring and address pins.
Wrong number after restart: what should I check?
Check the sequence and checksum code.
Chip not found: what should I check?
Run an I2C scanner, address should be 0x50.
