Zbotic Logo Zbotic Logo
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

  • Shop
  • About Us
  • Contact Us
  • Reseller
  • Blogs
020 69134444
1800 209 0998
[email protected]
Help Desk
Facebook Twitter Instagram Linkedin YouTube
Zbotic Logo Zbotic Logo
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

All departments
  • 3D Print Service
  • 3D Printer
  • Batteries & Chargers
  • Development Boards
  • Drone Parts
  • EBike parts
  • Sensor Modules
  • Electronic Components
  • Electronic Modules
  • IoT and Wireless
  • Mechanical Parts and Workbench Tools
  • Motors & Drivers & Pumps & Actuators
  • DIY and Robot Kits
  • Show more
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
Return to previous page
Home Arduino & Microcontrollers

Arduino EEPROM Tutorial: Read, Write & Wear Levelling Guide

Arduino EEPROM Tutorial: Read, Write & Wear Levelling Guide

March 11, 2026 /Posted byJayesh Jain / 0

Every Arduino project eventually faces the same challenge: how do you store data that survives a power cut? Whether you’re logging sensor readings, saving user settings, or recording calibration values, you need non-volatile memory. That’s exactly what Arduino’s built-in EEPROM provides — and in this tutorial you’ll learn everything from basic read/write operations to advanced wear levelling techniques that keep your EEPROM healthy for years.

What Is EEPROM and Why Does It Matter?

EEPROM stands for Electrically Erasable Programmable Read-Only Memory. Unlike RAM which loses its contents the moment power is removed, EEPROM retains data indefinitely without any power supply. The ATmega328P inside an Arduino Uno packs 1 KB (1024 bytes) of onboard EEPROM, while the ATmega2560 on the Arduino Mega provides a generous 4 KB.

EEPROM is ideal for storing:

  • Wi-Fi credentials or device configuration
  • Sensor calibration offsets
  • High scores or usage counters
  • Last known state for fault recovery
  • Serial numbers and factory settings

The critical limitation is write endurance. The ATmega datasheet guarantees at least 100,000 write cycles per byte. Write the same byte a million times and it will eventually fail. This is exactly why wear levelling matters — and we’ll cover that in detail later.

Recommended: Arduino Uno R3 Beginners Kit — perfect starting point for EEPROM experiments; includes the ATmega328P with 1 KB EEPROM and all breadboard components you need.

The EEPROM Library: Basic Read and Write

Arduino ships with the EEPROM.h library included — no installation needed. Include it at the top of your sketch:

#include <EEPROM.h>

Writing a Single Byte

The simplest operation writes one byte to a specific address:

EEPROM.write(address, value);
// address: 0 to EEPROM.length()-1
// value: 0 to 255

For example, to save a brightness setting of 128 at address 0:

#include <EEPROM.h>

void setup() {
  int address = 0;
  byte brightness = 128;
  EEPROM.write(address, brightness);
  Serial.begin(9600);
  Serial.println("Value written!");
}

void loop() {}

Reading a Single Byte

byte value = EEPROM.read(address);

Full example:

#include <EEPROM.h>

void setup() {
  Serial.begin(9600);
  int address = 0;
  byte value = EEPROM.read(address);
  Serial.print("Stored value: ");
  Serial.println(value);
}

void loop() {}

Reading EEPROM does not wear the memory — only writes do. You can read as many times as you like.

EEPROM.update() — The Smart Write

Here’s a critical tip most beginners miss: always use EEPROM.update() instead of EEPROM.write().

The difference: write() always performs a physical erase-write cycle regardless of whether the value has changed. update() first reads the existing byte and only writes if the new value differs. This simple change can multiply EEPROM lifespan several times over in projects that frequently refresh the same location.

// Bad practice — wastes write cycles:
EEPROM.write(0, sensorValue);

// Good practice — only writes if value changed:
EEPROM.update(0, sensorValue);
Recommended: Arduino Nano Every with Headers — features the ATMega4809 with 256 bytes of EEPROM, perfect for compact data-logging builds where every byte counts.

Storing Integers, Floats and Structs

Real projects rarely store single bytes. The EEPROM.put() and EEPROM.get() functions handle any data type using type casting:

Storing an Integer (2 bytes)

#include <EEPROM.h>

void setup() {
  Serial.begin(9600);

  int myInt = 32000;
  EEPROM.put(0, myInt);  // writes 2 bytes at address 0 and 1

  int readBack;
  EEPROM.get(0, readBack);
  Serial.println(readBack);  // prints 32000
}
void loop() {}

Storing a Float (4 bytes)

float calibrationOffset = 3.1416;
EEPROM.put(10, calibrationOffset);  // uses addresses 10-13

float retrieved;
EEPROM.get(10, retrieved);
// retrieved == 3.1416

Storing a Struct

For complex settings, pack everything into a struct:

#include <EEPROM.h>

struct Settings {
  byte brightness;
  byte volume;
  int  threshold;
  float calibration;
};

void saveSettings(Settings s) {
  EEPROM.put(0, s);
}

Settings loadSettings() {
  Settings s;
  EEPROM.get(0, s);
  return s;
}

void setup() {
  Serial.begin(9600);
  Settings mySettings = {200, 75, 512, 1.05};
  saveSettings(mySettings);

  Settings loaded = loadSettings();
  Serial.println(loaded.brightness);  // 200
  Serial.println(loaded.calibration); // 1.05
}
void loop() {}

Make sure you track address offsets carefully — overlapping writes silently corrupt data.

Recommended: Arduino Mega 2560 R3 Board — with 4 KB of EEPROM, the Mega is ideal when you need to store large config structs, multiple user profiles, or event logs.

Iterating Over All EEPROM Addresses

Use EEPROM.length() to write portable code that works across different Arduino boards:

// Clear all EEPROM (factory reset)
void clearEEPROM() {
  for (int i = 0; i < EEPROM.length(); i++) {
    EEPROM.update(i, 0xFF);  // 0xFF is the blank/erased state
  }
}

// Dump all EEPROM over serial for debugging
void dumpEEPROM() {
  for (int i = 0; i < EEPROM.length(); i++) {
    Serial.print(i);
    Serial.print(": ");
    Serial.println(EEPROM.read(i), HEX);
  }
}

Note: the erased state of EEPROM is 0xFF (255), not zero. When you read an uninitialised address you’ll get 0xFF back. Your code should handle this gracefully — for example by checking for a magic number (a known sentinel value) at a fixed address to detect first-boot conditions.

Wear Levelling: Extending EEPROM Lifespan

With 100,000 guaranteed write cycles per byte, a naive counter that updates every second would wear out that single byte in under 28 hours. Wear levelling spreads writes across many addresses to extend overall memory life.

Simple Rotating Counter

The simplest technique rotates writes across a block of addresses:

#include <EEPROM.h>

const int WL_START = 0;    // start address
const int WL_SIZE  = 100;  // 100 addresses = 100× more writes
const byte VALID_MARKER = 0xAA;

int findCurrentSlot() {
  for (int i = 0; i < WL_SIZE; i++) {
    int addr = WL_START + (i * 2);
    if (EEPROM.read(addr) != VALID_MARKER) {
      return i;  // first unmarked slot
    }
  }
  return -1;  // all slots used — reset
}

void writeWL(byte value) {
  int slot = findCurrentSlot();
  if (slot == -1) {
    // Erase all slots and start over
    for (int i = 0; i < WL_SIZE; i++) {
      EEPROM.update(WL_START + (i * 2), 0xFF);
    }
    slot = 0;
  }
  int addr = WL_START + (slot * 2);
  EEPROM.update(addr,     VALID_MARKER);
  EEPROM.update(addr + 1, value);
}

byte readWL() {
  int slot = findCurrentSlot();
  if (slot == 0) return 0;  // no data yet
  int addr = WL_START + ((slot - 1) * 2) + 1;
  return EEPROM.read(addr);
}

This spreads 100,000 physical writes across 100 addresses, giving you effectively 10 million logical writes before any address is stressed.

Page Rotation for Structs

For struct storage, divide EEPROM into pages of the struct size and rotate through them. A version counter (incrementing byte) in each page tells you which page holds the latest data:

struct Config {
  byte version;      // increments each save
  byte brightness;
  int  threshold;
  byte checksum;
};

const int PAGE_SIZE = sizeof(Config);  // e.g. 5 bytes
const int NUM_PAGES = EEPROM.length() / PAGE_SIZE;

byte computeChecksum(Config &c) {
  return c.version ^ c.brightness ^ (c.threshold >> 8) ^ (c.threshold & 0xFF);
}

void saveConfig(Config &c) {
  // Find page with highest version
  byte maxVersion = 0;
  int writeSlot = 0;
  for (int i = 0; i < NUM_PAGES; i++) {
    Config tmp;
    EEPROM.get(i * PAGE_SIZE, tmp);
    if (tmp.version > maxVersion) {
      maxVersion = tmp.version;
      writeSlot = (i + 1) % NUM_PAGES;
    }
  }
  c.version = maxVersion + 1;
  c.checksum = computeChecksum(c);
  EEPROM.put(writeSlot * PAGE_SIZE, c);
}
Recommended: Arduino Nano 33 IoT with Header — for IoT projects that constantly write MQTT configs and credentials to EEPROM, wear levelling is essential; the Nano 33 IoT’s 256 bytes of EEPROM benefit greatly from a rotating scheme.

Going Beyond Built-in EEPROM: External I2C EEPROM

When 1–4 KB isn’t enough, external EEPROM chips over I2C are the answer. The 24LC256 (32 KB) and 24LC512 (64 KB) are popular choices. Connect SDA to A4, SCL to A5 on an Uno, and use the Wire library for page-write operations:

#include <Wire.h>

const int EEPROM_ADDR = 0x50;  // A0-A2 pins tied to GND

void externalWrite(unsigned int address, byte data) {
  Wire.beginTransmission(EEPROM_ADDR);
  Wire.write((int)(address >> 8));    // MSB
  Wire.write((int)(address & 0xFF));  // LSB
  Wire.write(data);
  Wire.endTransmission();
  delay(5);  // write cycle time
}

byte externalRead(unsigned int address) {
  Wire.beginTransmission(EEPROM_ADDR);
  Wire.write((int)(address >> 8));
  Wire.write((int)(address & 0xFF));
  Wire.endTransmission();
  Wire.requestFrom(EEPROM_ADDR, 1);
  return Wire.read();
}

External EEPROMs typically offer 1,000,000 write cycles per byte — ten times the ATmega’s built-in cells — which further reduces wear concerns.

Recommended: Arduino Starter Kit with 170 Pages Project Book — covers memory and storage projects with guided experiments; great for learners who want structured EEPROM practice.

EEPROM Best Practices Summary

  • Use update() not write() — prevents unnecessary write cycles when the value hasn’t changed.
  • Always validate with a magic number — on first boot EEPROM contains 0xFF; check for your marker byte before trusting stored data.
  • Add a checksum — detect corruption caused by power loss mid-write.
  • Use EEPROM.length() — never hardcode 1024; write portable code that adapts to any Arduino.
  • Avoid writing in loop() — only write when data actually changes, not every iteration.
  • Consider external EEPROM — for data-heavy applications, a 24LC series chip costs less than ₹50 and gives 32–512 KB.

Frequently Asked Questions

How many times can I write to Arduino EEPROM?

The ATmega328P datasheet guarantees a minimum of 100,000 write/erase cycles per byte. In practice many chips exceed this, but you should design your code to stay well within this limit. Using EEPROM.update() and wear levelling techniques can effectively multiply this limit many times over.

Does reading EEPROM wear it out?

No. Read operations do not stress EEPROM cells in any way. You can read the same address millions of times without degradation. Only write and erase operations consume the endurance budget.

What happens if I write to EEPROM inside loop() every cycle?

At 16 MHz with a typical loop running thousands of times per second, you would exhaust the 100,000 write budget in under two minutes. Always add a change-detection condition — only write when the value actually differs from what’s currently stored, which is exactly what EEPROM.update() does automatically.

Can I store strings in EEPROM?

Yes. Use a loop to write each character byte by byte, or use EEPROM.put() with a char array. Remember to store the null terminator and always bounds-check string length against available EEPROM space.

Is EEPROM data preserved when I upload a new sketch?

Yes — uploading a new sketch via USB does not erase EEPROM. EEPROM data persists across sketch uploads, power cycles, and resets. Only a deliberate erase operation (like running a clear sketch) wipes it. This is both useful (settings survive firmware updates) and a potential source of bugs (stale data from old firmware).

Start Building With Arduino

EEPROM is one of the most practical tools in the Arduino programmer’s toolkit. Once you understand the read/write API and protect your data with update-only writes and wear levelling, you can build robust projects that survive power cycles and retain state for the lifetime of the hardware.

Ready to put these techniques into practice? Browse all Arduino boards and accessories at Zbotic.in — India’s trusted source for genuine Arduino hardware shipped fast.

Tags: Arduino EEPROM, arduino memory, Arduino programming, arduino storage, EEPROM tutorial, wear levelling
Share Post
  • Facebook
  • Linkedin
  • Whatsapp
Arduino Compass Module HMC5883...
blog arduino compass module hmc5883l heading and navigation 594833
blog arduino 12v relay control safe wiring for mains appliances 594837
Arduino 12V Relay Control: Saf...

Related posts

Svg%3E
Read more

Arduino Batch Programming: Flash Multiple Boards Quickly

April 1, 2026 0
Table of Contents Introduction Components and Hardware Setup Wiring Diagram and Connections Complete Code with Explanation Customization and Improvements Troubleshooting... Continue reading
Svg%3E
Read more

Arduino Based Radar System with Ultrasonic Sensor

April 1, 2026 0
Table of Contents Introduction Components and Hardware Setup Wiring Diagram and Connections Complete Code with Explanation Customization and Improvements Troubleshooting... Continue reading
Svg%3E
Read more

Arduino Automatic Plant Monitor: Sunlight, Moisture, Temperature

April 1, 2026 0
Table of Contents Introduction Components and Hardware Setup Wiring Diagram and Connections Complete Code with Explanation Customization and Improvements Troubleshooting... Continue reading
Svg%3E
Read more

Arduino Lie Detector: GSR Sensor Polygraph Project

April 1, 2026 0
Table of Contents Introduction Components and Hardware Setup Wiring Diagram and Connections Complete Code with Explanation Customization and Improvements Troubleshooting... Continue reading
Svg%3E
Read more

Arduino Metal Detector: Build a Treasure Finder

April 1, 2026 0
Table of Contents Introduction Components and Hardware Setup Wiring Diagram and Connections Complete Code with Explanation Customization and Improvements Troubleshooting... Continue reading

Add comment Cancel reply

Your email address will not be published. Required fields are marked

Facebook Twitter Instagram Pinterest Linkedin Youtube

Get the latest deals and more.

Download on Google Play Download on the App Store

Call us: 020 69134444 / 1800 209 0998

Monday - Saturday 09:30 AM - 06:00 PM
For Technical Supports Email: [email protected]
For Sales / Enquiries Email: [email protected]

  • My Account

    • Cart

    • Wishlist

    • Checkout

    • My Orders

    • Track Order

    • My Account

  • Information

    • FAQs

    • Blogs

    • Career

    • About Us

    • Contact Us

    • Payment Options

  • Policies

    • Privacy Policy

    • Terms & Conditions

    • GST Input Tax Credit

    • Shipping Return Policy

    • E-Waste Collection Points

    • Our Sitemap

© Zbotic.in is registered trademark of Moxie Supply Pvt Ltd – All Rights Reserved
Login
Use Phone Number
Use Email Address
Not a member yet? Register Now
Reset Password
Use Phone Number
Use Email Address
Register
Already a member? Login Now