EEPROM Quick Reference Guide
🚀 Practice real-world tasks & problems for EEPROM to build pro-level skills — Click here
EEPROM
EEPROM is a type of memory that keeps data even when the power is turned off. You can write, read, and erase data many times.
EEPROM is used to store important data like user settings, calibration values, or device states that must be remembered even after power is turned OFF.
Some microcontrollers, like ATmega328P, have built-in EEPROM, while others, like 8051, need an external EEPROM.
EEPROM vs Flash vs RAM
In microcontrollers, we typically work with three main types of memory: SRAM, Flash, and EEPROM — each serving a distinct purpose.
- SRAM (Static RAM) → A microcontroller stores temporary data like variables and the stack during program execution.
- Flash → stores our program code.
- EEPROM → stores small amounts of user data (like config settings)
Feature | EEPROM | Flash | RAM |
Volatility | Non-volatile | Non-volatile | Volatile |
Speed | Slow | Medium | Fast |
Access | Byte-level | Page/block level | Byte-level |
Write Cycles | ~100k–1M | ~10k–100k | billions or more |
EEPROM operations
- Read: Data is read from a given EEPROM address.
- Write: Data is stored at a given EEPROM address.
- Erase: Data is erased by overwriting with 0xFF.
EEPROM Access Methods
- Internal EEPROM is accessed using Special Function Registers (SFRs).
- External EEPROM is accessed using I2C or SPI communication.
EEPROM Wear & Tear
EEPROM cells usually handle around 100K to 1M write cycles, and store data for 10 to 100 years, depending on usage and temperature.
Reading data does not degrade EEPROM cells. Thus, some datasheets may specify a very high read endurance (e.g., >10 M reads).
To extend EEPROM life:
- Avoid frequent writes — update only when needed.
- Use wear leveling — spread writes across different addresses to prevent wearing out specific cells.
Wear Leveling Techniques
- Log-Structured Writes – Instead of overwriting, append new data, reducing repeated wear on the same location.
- Block Rotation – Periodically move data between memory blocks to distribute wear evenly.
- EEPROM Emulation in Flash – Some MCUs (like STM32) use Flash memory to mimic EEPROM, with built-in wear leveling for better endurance.
EEPROM Registers
Many microcontrollers (like AVR, STM32, ARM) use similar registers to handle EEPROM operations.
Considering AVR controllers:
Register | Purpose |
---|---|
EEARH / EEARL | Hold the EEPROM address where data will be read from or written to. |
EEDR | Holds the data byte to be read or written. |
EECR | Controls read/write actions, enables interrupts, and sets operation modes. |
EEPROM Programming Steps
EEPROM access involves a few simple steps using dedicated registers:
Write Operation
- Set Address – Load the target EEPROM address into the address register.
- Load Data – Place the byte to write into the data register.
- (Optional) Disable Interrupts – To avoid timing issues during the write.
- Start Write – Set the correct bits in the control register to begin writing.
- Wait for Completion – Monitor the control flag until the write finishes.
Note: External EEPROMs usually support page write operations, allowing us to write 16, 32, or 64 bytes at once, depending on the chip. This speeds up writing compared to byte-by-byte access.
Read Operation
- Set Address – Load the desired EEPROM address into the address register.
- Start Read – Set the read enable bit in the control register.
- Get Data – Read the byte from the data register.
EEPROM Library and Functions in Arduino UNO
Type | Name | Description |
---|---|---|
Libraries | #include <EEPROM.h> | provides an easy-to-use interface for reading and writing data to the microcontroller's built-in EEPROM memory |
Functions
| EEPROM.read(address) | Reads a byte from the specified address |
EEPROM.write(address, val) | Writes a byte to the specified address | |
EEPROM.update(addr, val) | Writes only if the value is different | |
EEPROM.get(addr, var) | Reads any data type/structure | |
EEPROM.put(addr, var) | Writes any data type/structure | |
EEPROM.length() | Returns EEPROM size |
Examples
1. Basic Example of writing one byte data on EEPROM and reading that data, and printing on the serial monitor.
Code:
#include <EEPROM.h>
void setup() {
Serial.begin(115200);
EEPROM.write(10, 123); // Write 123 to address 10
int val = EEPROM.read(10); // Read from address 10
Serial.println(val); // Outputs: 123
}
void loop() {
}
2. Basic Example of writing structure data on EEPROM and reading that structure data, and printing on the serial monitor
Code:
#include <EEPROM.h> // Include EEPROM library
struct Data {
int sensorValue;
float temperature;
};
void setup() {
Serial.begin(115200);
Data data = {1023, 36.5}; // Initialize struct with sample data
EEPROM.put(0, data); // Write struct to EEPROM
data.sensorValue = 0; // Clear values to demonstrate EEPROM read
data.temperature = 0.0;
EEPROM.get(0, data); // Read struct from EEPROM
Serial.print("Sensor: ");
Serial.println(data.sensorValue);
Serial.print("Temperature: ");
Serial.println(data.temperature);
}
void loop() {
// Nothing here
}