0
0
Arduinoprogramming~5 mins

EEPROM read and write in Arduino

Choose your learning style9 modes available
Introduction

EEPROM lets your Arduino save data even when it is turned off. You can read and write small pieces of information that stay saved.

Saving user settings like brightness or volume that should stay after power off.
Storing scores or progress in a game that you want to keep between sessions.
Remembering calibration values for sensors so you don't have to set them every time.
Logging small amounts of data that you want to keep safe during power loss.
Syntax
Arduino
EEPROM.write(address, value);
value = EEPROM.read(address);

address is the location in EEPROM memory (0 to 1023 on many boards).

value is a byte (0-255) to write or read.

Examples
This saves the number 123 at the first EEPROM spot and then reads it back.
Arduino
EEPROM.write(0, 123);  // Save number 123 at address 0
byte val = EEPROM.read(0);  // Read from address 0
This writes multiples of 10 to the first 10 EEPROM addresses and then prints them.
Arduino
for (int i = 0; i < 10; i++) {
  EEPROM.write(i, i * 10);
}

for (int i = 0; i < 10; i++) {
  byte val = EEPROM.read(i);
  Serial.println(val);
}
Sample Program

This program saves the number 42 at EEPROM address 5 and then reads it back to print on the serial monitor.

Arduino
#include <EEPROM.h>

void setup() {
  Serial.begin(9600);
  // Write value 42 to EEPROM address 5
  EEPROM.write(5, 42);
  // Read value from EEPROM address 5
  byte value = EEPROM.read(5);
  Serial.print("Value at address 5: ");
  Serial.println(value);
}

void loop() {
  // Nothing here
}
OutputSuccess
Important Notes

EEPROM can only store bytes (0-255). For bigger data, you need to split it into bytes.

Writing to EEPROM too often can wear it out. Avoid writing in a fast loop.

Use EEPROM.update() if you want to write only when the value changes to save EEPROM life.

Summary

EEPROM stores data that stays after power off.

Use EEPROM.write(address, value) to save a byte.

Use EEPROM.read(address) to get the saved byte back.