Remember a Counter After Reset
Persist state across power cycles.
Remember a Counter After Reset is a free Arduino & IoT Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Counter That Survives
Let's build a press counter that remembers its total even after a power cut. We'll combine debouncing with EEPROM storage.
The Plan in One Line
On startup, load the saved count. On each clean press, add one and save it back. Simple, but powerful.
Pick a Storage Slot
Reserve one EEPROM address for the counter. We'll keep it small enough to fit in a single byte for now.
const int ADDR = 0;
byte count = 0;Load on Boot
Inside setup(), read the saved value first. Now your counter picks up exactly where it left off.
void setup() {
count = EEPROM.read(ADDR);
}Wipe the Junk First
A fresh chip's EEPROM often reads 255. Treat that as empty and reset your counter to zero on the first run.
if (count == 255) count = 0;Reuse Your Debounce
Detect presses with the millis() debounce from before. Clean input is essential, or your saved count drifts upward.
Increment on a Clean Press
When a debounced press fires, bump the counter by one. This is the single moment your total grows.
count++;
Serial.println(count);Save Without Wearing Out
Use EEPROM.update to write the new total. It only touches memory when the value actually changed, protecting the chip.
EEPROM.update(ADDR, count);Test the Magic
Press a few times, then unplug and replug the board. The Serial Monitor shows your count continuing, not restarting. 🎉
Mind the Byte Limit
A byte counter wraps back to 0 after 255. For bigger totals, store an int with EEPROM.put and EEPROM.get instead.
Add a Reset Option
A real product often needs a way to clear the saved count. A long button hold could write 0 back to EEPROM.
EEPROM.update(ADDR, 0);Quick Check
Let's check the key step that lets the counter survive a reset.
Recap: Memory Across Resets
You built a counter that persists by loading from EEPROM at boot and saving each clean press. Your device finally has a memory! 🧠
Frequently asked questions
Is the “Remember a Counter After Reset” lesson free?
Yes — the full text of “Remember a Counter After Reset” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.
What will I learn in “Remember a Counter After Reset”?
Persist state across power cycles. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Arduino & IoT Academy?
No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Remember a Counter After Reset” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Arduino & IoT Academy lesson?
Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Buttons 'Bounce'
- Debounce with millis()
- Store a Value in EEPROM
- Remember a Counter After Reset