0
0
Arduinoprogramming~15 mins

EEPROM read and write in Arduino - Deep Dive

Choose your learning style9 modes available
Overview - EEPROM read and write
What is it?
EEPROM stands for Electrically Erasable Programmable Read-Only Memory. It is a small memory inside microcontrollers like Arduino that keeps data even when the power is off. Reading from EEPROM means getting stored data, and writing means saving new data to it. This lets your Arduino remember things between restarts.
Why it matters
Without EEPROM, every time you turn off your Arduino, all data would be lost. This makes it impossible to save settings, scores, or states that should last. EEPROM solves this by providing a tiny, permanent storage inside the chip, so your projects can remember important information.
Where it fits
Before learning EEPROM, you should understand basic Arduino programming and variables. After EEPROM, you can explore external memory modules or data logging techniques to store larger amounts of data or more complex information.
Mental Model
Core Idea
EEPROM is like a tiny notebook inside your Arduino that keeps notes safe even when the power is off.
Think of it like...
Imagine your Arduino has a small sticky note pad where it writes reminders. When it needs to remember something later, it reads the note. Writing is like jotting down a new reminder, and reading is like looking at the note again.
┌───────────────┐
│   Arduino     │
│  ┌─────────┐  │
│  │ EEPROM  │  │
│  └─────────┘  │
│  Read <-----> Write
└───────────────┘
Build-Up - 7 Steps
1
FoundationWhat is EEPROM Memory
🤔
Concept: Introduce EEPROM as a special memory that keeps data without power.
EEPROM is a small part of your Arduino's memory that remembers data even when the board is turned off. Unlike normal variables that lose their value when power is lost, EEPROM keeps the data safe. It is limited in size, usually a few hundred bytes, so you use it for small pieces of information.
Result
You understand EEPROM is permanent memory inside Arduino for saving data across power cycles.
Knowing EEPROM is permanent memory helps you plan when to save important data that must survive resets.
2
FoundationBasic EEPROM Read and Write Functions
🤔
Concept: Learn the Arduino functions to read and write single bytes to EEPROM.
Arduino provides EEPROM.read(address) to get a byte from a specific address and EEPROM.write(address, value) to store a byte at an address. The address is a number from 0 up to the EEPROM size minus one. For example, EEPROM.write(0, 42) saves the number 42 at address 0, and EEPROM.read(0) retrieves it.
Result
You can save and retrieve single bytes from EEPROM using simple functions.
Understanding these functions is the foundation for storing any data in EEPROM.
3
IntermediateStoring and Reading Multiple Bytes
🤔Before reading on: do you think you can store a number larger than 255 directly with EEPROM.write? Commit to your answer.
Concept: Learn how to save and read data types larger than one byte by handling multiple bytes.
EEPROM.write only stores one byte (0-255). To save bigger numbers like int (2 bytes) or long (4 bytes), you must split them into bytes and store each byte separately. Arduino's EEPROM library also has EEPROM.put(address, data) and EEPROM.get(address, data) to handle this automatically for many data types.
Result
You can save complex data types safely in EEPROM by using multiple bytes or helper functions.
Knowing how to handle multi-byte data prevents data corruption and makes EEPROM useful for real projects.
4
IntermediateEEPROM Size and Address Limits
🤔
Concept: Understand the size limits of EEPROM and how to choose addresses carefully.
Each Arduino model has a fixed EEPROM size, like 512 bytes on Arduino Uno. You must not read or write outside this range, or you risk errors. Plan your data layout so each piece of data has a unique address or address range. For example, store a byte at address 0, an int at addresses 1 and 2, and so on.
Result
You avoid memory errors by respecting EEPROM size and managing addresses.
Knowing EEPROM size and address limits helps prevent bugs and data overwriting.
5
IntermediateMinimizing EEPROM Wear
🤔Before reading on: do you think EEPROM can be written unlimited times without damage? Commit to your answer.
Concept: Learn that EEPROM has a limited number of write cycles and how to reduce wear.
EEPROM can only be written about 100,000 times per address before it may wear out. To protect it, avoid writing the same address repeatedly in a loop. Use techniques like writing only when data changes or spreading writes across different addresses (wear leveling).
Result
Your EEPROM lasts longer by reducing unnecessary writes.
Understanding EEPROM wear prevents unexpected data loss and hardware damage.
6
AdvancedUsing EEPROM.put and EEPROM.get
🤔Before reading on: do you think EEPROM.put always writes data even if unchanged? Commit to your answer.
Concept: Use EEPROM.put and EEPROM.get to simplify reading and writing complex data safely.
EEPROM.put(address, data) writes any data type to EEPROM and only updates bytes that changed, reducing wear. EEPROM.get(address, data) reads data back. This makes saving structs, floats, or arrays easier without manual byte splitting.
Result
You write and read complex data types efficiently and safely.
Knowing these functions simplifies code and extends EEPROM life by avoiding unnecessary writes.
7
ExpertAdvanced EEPROM Management Techniques
🤔Before reading on: do you think EEPROM data is safe during power loss mid-write? Commit to your answer.
Concept: Explore techniques like checksums, wear leveling, and power-loss safety for robust EEPROM use.
To ensure data integrity, add checksums or CRCs to detect corruption. Wear leveling spreads writes across EEPROM to extend life. Also, consider power-loss scenarios where writes may be interrupted, causing partial data. Use double buffering or atomic writes to handle this. These techniques make EEPROM reliable in real-world applications.
Result
Your EEPROM data remains accurate and hardware lasts longer in demanding conditions.
Understanding these advanced techniques prepares you for professional-grade embedded systems.
Under the Hood
EEPROM memory stores data in floating-gate transistors that trap electric charge to represent bits. Writing changes the charge state by applying higher voltage pulses, which takes longer and wears the memory. Reading is fast and non-destructive, simply sensing the charge state. The microcontroller accesses EEPROM through special registers and commands internally.
Why designed this way?
EEPROM was designed to provide non-volatile storage inside microcontrollers without needing external chips. The floating-gate transistor technology balances permanence with electrical erasability. Alternatives like flash memory are larger but less byte-addressable, so EEPROM offers fine-grained control for small data.
┌───────────────┐
│   CPU Core    │
│  ┌─────────┐  │
│  │ EEPROM  │  │
│  │ Memory  │  │
│  └─────────┘  │
│   ↑     ↓     │
│ Read  Write   │
└───────────────┘

Inside EEPROM:
┌─────────────┐
│ Floating-   │
│ Gate Cells  │
│ (Charge)    │
└─────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Can you write to EEPROM unlimited times without damage? Commit to yes or no.
Common Belief:EEPROM can be written endlessly without any wear or damage.
Tap to reveal reality
Reality:EEPROM has a limited write cycle count (about 100,000 writes per cell) before it may fail.
Why it matters:Ignoring this causes premature EEPROM failure and data loss in projects.
Quick: Does EEPROM.read return the last value written even after power off? Commit to yes or no.
Common Belief:EEPROM data is volatile and lost when power is off, just like normal variables.
Tap to reveal reality
Reality:EEPROM is non-volatile and retains data even when power is removed.
Why it matters:Misunderstanding this leads to wrong assumptions about data persistence and project design.
Quick: Does EEPROM.put always write data even if unchanged? Commit to yes or no.
Common Belief:EEPROM.put writes data every time it is called, regardless of current content.
Tap to reveal reality
Reality:EEPROM.put compares existing data and only writes changed bytes, reducing wear.
Why it matters:Knowing this helps optimize EEPROM lifespan and avoid unnecessary writes.
Quick: Can you store any data type directly with EEPROM.write? Commit to yes or no.
Common Belief:EEPROM.write can store any data type like int or float directly.
Tap to reveal reality
Reality:EEPROM.write only stores one byte; larger data types must be split or use EEPROM.put.
Why it matters:Misusing EEPROM.write causes data corruption and bugs.
Expert Zone
1
EEPROM writes are slow compared to RAM, so timing matters in real-time systems.
2
Using EEPROM.put reduces wear by avoiding unnecessary writes, but manual wear leveling can further extend life.
3
Power loss during EEPROM write can corrupt data; implementing checksums and atomic writes is crucial for reliability.
When NOT to use
EEPROM is not suitable for large data storage or frequent writes. For big data, use external SD cards or flash memory. For very frequent writes, consider RAM with battery backup or FRAM memory.
Production Patterns
Professionals use EEPROM to store calibration data, device IDs, user settings, and counters. They implement wear leveling, data validation with checksums, and backup copies to ensure robustness.
Connections
Flash Memory
Related non-volatile memory technology with larger capacity but different write/erase behavior.
Understanding EEPROM helps grasp flash memory's strengths and limits, as both store data without power but differ in granularity and endurance.
File Systems
EEPROM storage is a primitive form of persistent storage, like a tiny file system without folders or files.
Knowing EEPROM basics clarifies how file systems build on raw memory to organize and protect data.
Human Memory
Both EEPROM and human long-term memory store information persistently, unlike short-term memory (RAM).
This analogy helps appreciate the difference between volatile and non-volatile storage in computing and cognition.
Common Pitfalls
#1Writing to EEPROM every loop iteration without checking if data changed.
Wrong approach:void loop() { EEPROM.write(0, sensorValue); delay(1000); }
Correct approach:void loop() { if (EEPROM.read(0) != sensorValue) { EEPROM.write(0, sensorValue); } delay(1000); }
Root cause:Not realizing EEPROM has limited write cycles and writing unnecessarily wears it out.
#2Trying to store an int directly with EEPROM.write causing data loss.
Wrong approach:int value = 300; EEPROM.write(0, value);
Correct approach:int value = 300; EEPROM.put(0, value);
Root cause:Misunderstanding EEPROM.write stores only one byte, so larger data types need special handling.
#3Reading EEPROM without initializing or checking data validity leading to garbage values.
Wrong approach:int storedValue = 0; storedValue = EEPROM.get(0, storedValue);
Correct approach:int storedValue = 0; EEPROM.get(0, storedValue); if (storedValue < 0 || storedValue > MAX_EXPECTED) { storedValue = DEFAULT_VALUE; }
Root cause:Assuming EEPROM always contains valid data without validation.
Key Takeaways
EEPROM is a small, permanent memory inside Arduino that keeps data even when power is off.
You read and write EEPROM using special functions that handle one byte or multiple bytes safely.
EEPROM has limited write cycles, so minimizing unnecessary writes is essential to avoid damage.
Advanced techniques like EEPROM.put, wear leveling, and checksums improve reliability and lifespan.
Understanding EEPROM helps build projects that remember settings, counters, or states across power cycles.