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.

In this blog, we will cover:

  • What EEPROM is
  • EEPROM size in different Arduino boards
  • EEPROM library functions
  • How to read and write EEPROM
  • Practical Arduino EEPROM examples
  • EEPROM best practices and limitations

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)

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

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