Ad Code

Responsive Advertisement

Arduino EEPROM Tutorial – Read, Write & Practical Examples

In most Arduino projects, data is stored in RAM, which is volatile—meaning all data is lost when power is turned off. However, many real-world applications require data to be saved permanently, such as calibration values, user settings, passwords, or counters.

This is where EEPROM (Electrically Erasable Programmable Read-Only Memory) becomes extremely useful.

Arduino EEPROM allows you to store data permanently, even when the board is powered off or reset.

What is EEPROM?

EEPROM is a type of non-volatile memory that retains stored data even after power is removed.

Key Features of EEPROM

  • Non-volatile memory
  • Data remains after power off
  • Can be erased and rewritten electrically
  • Limited write cycles (typically 100,000 writes per address)

Why would you use the internal EEPROM?

You would use the internal EEPROM (Electrically Erasable Programmable Read-Only Memory) when you need to store small amounts of data that must remain saved even after the device is powered off. Unlike RAM, which loses data when power is removed, EEPROM preserves information without requiring an external storage device. This makes it ideal for saving configuration settings, calibration values, user preferences, device IDs, counters, or system states in embedded systems such as Arduino, ESP32 (emulated EEPROM), and microcontroller-based automation projects.

For example, in a temperature controller project, you can store setpoints in EEPROM so that the system resumes with the last saved values after a power failure. Using internal EEPROM also reduces hardware complexity, cost, and PCB space since no additional memory chip is required. However, because EEPROM has a limited write cycle life (typically around 100,000 write cycles per memory location), it should be used wisely—mainly for infrequent data updates rather than continuous logging applications.

EEPROM Size in Different Arduino Boards

Arduino BoardEEPROM Size
Arduino UNO1 KB (1024 bytes)
Arduino Nano1 KB
Arduino Mega4 KB
Arduino Due❌ No true EEPROM (emulated in Flash)
ESP32❌ EEPROM emulated in Flash

⚠️ Note: Boards like ESP32 and Arduino Due do not have physical EEPROM but use Flash memory emulation.

Arduino EEPROM Library

Arduino provides a built-in library called EEPROM.h to easily access EEPROM memory.

Include the Library

#include <EEPROM.h>

EEPROM Addressing

EEPROM memory is byte-addressable.

  • Each address stores 1 byte (8 bits)
  • Arduino UNO addresses range from 0 to 1023
Address 0 → 1 byte
Address 1 → 1 byte
...
Address 1023 → 1 byte

Basic EEPROM Functions

Basic EEPROM Functions refer to the fundamental operations used to store and retrieve non-volatile data inside a microcontroller’s internal EEPROM memory. These core functions typically include write, read, and sometimes update operations. The write function stores a byte (or multiple bytes) of data into a specific memory address, while the read function retrieves the stored data from that address whenever needed. 

Many platforms, such as Arduino, also provide an update function, which writes data only if the new value is different from the existing one—helping to extend EEPROM lifespan by reducing unnecessary write cycles. Some systems also support functions for storing more complex data types like integers, floats, or custom structures. 

These basic EEPROM functions are essential in embedded systems for saving configuration parameters, calibration values, setpoints, device IDs, and other critical information that must remain intact even after power loss.

1. EEPROM.write()

Writes a single byte to an EEPROM address.

EEPROM.write(address, value);

Example:

EEPROM.write(0, 25);   // Store value 25 at address 0

2. EEPROM.read()

Reads a single byte from EEPROM.

value = EEPROM.read(address);

Example:

int data = EEPROM.read(0);

3. EEPROM.update() 

Writes data only if the value has changed, increasing EEPROM life.

EEPROM.update(address, value);

✔ Prevents unnecessary writes
✔ Extends EEPROM lifespan

Example 1: Store and Read a Number from EEPROM

Objective

Store a value in EEPROM and read it back after reset.

Code Example


#include <EEPROM.h>

void setup() {
  Serial.begin(9600);
  // Write value to EEPROM
  EEPROM.update(0, 123);
  // Read value from EEPROM
  int storedValue = EEPROM.read(0);
  Serial.print("Stored Value: ");
  Serial.println(storedValue);
}

void loop() {
}

Output

Stored Value: 123

Even after resetting or powering off the Arduino, the value remains stored.

Example 2: Store a Float Value in EEPROM

EEPROM stores bytes only, so storing float or int values requires conversion.

Using EEPROM.put() and EEPROM.get()

float temperature = 36.5;
EEPROM.put(10, temperature);

Read Float from EEPROM

float readTemp;
EEPROM.get(10, readTemp);

Complete Code


#include <EEPROM.h>

void setup() {
  Serial.begin(9600);
  float temp = 27.8;
  EEPROM.put(10, temp);
  float storedTemp;
  EEPROM.get(10, storedTemp);
  Serial.print("Temperature: ");
  Serial.println(storedTemp);
}
void loop() {
}

Example 3: Save User Settings (Brightness Level)

Application

Store LED brightness level permanently.


#include <EEPROM.h>

int ledPin = 9;
int brightnessAddress = 0;
void setup() {
  pinMode(ledPin, OUTPUT);
  int brightness = EEPROM.read(brightnessAddress);
  analogWrite(ledPin, brightness);
}
void loop() {
  int newBrightness = 150;
  EEPROM.update(brightnessAddress, newBrightness);
  analogWrite(ledPin, newBrightness);
  delay(5000);
}

Clearing EEPROM Memory

Clear Entire EEPROM


#include <EEPROM.h>

void setup() {
  for (int i = 0; i < EEPROM.length(); i++) {
    EEPROM.write(i, 0);
  }
}

void loop() {
}

EEPROM Write Cycle Limitation

⚠️ EEPROM has limited write cycles.

  • Typical limit: 100,000 writes per address
  • Avoid writing inside fast loops
  • Use EEPROM.update() instead of EEPROM.write()

Best Practices for Using Arduino EEPROM

✔ Use EEPROM only when necessary
✔ Avoid frequent writes
✔ Use EEPROM.update()
✔ Store configuration, not sensor data streams
✔ Keep track of memory addresses

Real-World Applications of Arduino EEPROM

EEPROM vs Flash vs RAM

FeatureEEPROMFlashRAM
Volatile
SpeedMediumFastVery Fast
Write CyclesLimitedLimitedUnlimited
Data PersistencePermanentPermanentTemporary

Conclusion

Arduino EEPROM is a powerful feature that allows you to store data permanently without external memory devices. By understanding its limitations and using it efficiently, you can build robust, professional-grade embedded systems.

Whether you're creating password locks, controllers, or configuration-based projects, EEPROM is an essential tool every Arduino developer should master.

Post a Comment

0 Comments

Ad Code

Responsive Advertisement