Build a task to track the number of times a microcontroller restarts (reset or power failure). Also provide functionality for the user to set the counter back to zero using a serial terminal (e.g., PuTTY, Arduino IDE).
Please refer to the video example.
Example:
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.
In microcontrollers, we typically work with three main types of memory: SRAM, Flash, and EEPROM — each serving a distinct purpose.
| 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 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:
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 access involves a few simple steps using dedicated registers:
Write Operation
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
| 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 | |
EEPROM[address] | Provides direct read/write access to EEPROM memory using array-style indexing. |
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
}