As an Amazon Associate we earn from qualifying purchases.
EEPROM (Electrically Erasable Programmable Read-Only Memory) is a small block of non-volatile memory built into many microcontrollers. Data saved there stays in place when the board loses power or is reset. That makes it a good place for settings, calibration values, counters and anything else you want to remember between runs.
This guide covers the EEPROM on the ATmega chips used in common Arduino boards: how much space you get, how to read and write it, and what to watch out for so you don’t wear it out.
What is EEPROM and when should you use it?
A microcontroller usually has three kinds of memory. Flash holds your program, SRAM holds variables while the program runs, and EEPROM holds small pieces of data that must survive a power cycle. SRAM is wiped whenever power is removed, so anything you want to keep has to go to EEPROM.
Typical uses include:
- Saving user settings such as a brightness level or a selected mode
- Storing sensor calibration offsets
- Keeping a power-on counter or a simple event count
- Remembering the last state of a relay or output after a restart
EEPROM is not meant for fast, constant logging. It is slow compared with SRAM and each cell can only be rewritten a limited number of times.
EEPROM size on ATmega chips
The amount of EEPROM depends on the chip:
- ATmega328 (used on the Arduino Uno and Nano): 1 KB (1024 bytes)
- ATmega168: 512 bytes
- ATmega8: 512 bytes
Each address holds exactly one byte. Addresses start at 0, so on a 1 KB chip the valid range is 0 to 1023, and on a 512-byte chip it is 0 to 511. You can also call EEPROM.length() in your sketch to get the size of the EEPROM on the board you are using.
Data must be stored as bytes
Every EEPROM location stores 8 bits, which means a value from 0 to 255. The basic read() and write() functions work with one byte at a time. A character such as ‘E’ fits in a single byte, but larger values like an int (2 bytes on AVR boards) or a float (4 bytes) need more than one address. You either split them into bytes yourself or use EEPROM.put() and EEPROM.get(), described below.
EEPROM functions in the Arduino library
Include the library at the top of your sketch with #include <EEPROM.h>. The two core functions are:
read()
Reads one byte from the given address.
Syntax: EEPROM.read(address)
write()
Writes one byte to the given address.
Syntax: EEPROM.write(address, value)
Other useful functions
EEPROM.update(address, value)writes the byte only if it differs from what is already stored. This saves write cycles.EEPROM.put(address, data)writes any data type, such as an int, float or struct, and uses as many bytes as the type needs.EEPROM.get(address, data)reads a value of any type back into a variable.EEPROM.length()returns the number of bytes available.
Limits to keep in mind
- Write time: an EEPROM write takes about 3.3 ms to complete. That is very slow compared with normal variable access, so avoid writing inside a fast loop.
- Endurance: each cell is rated for about 100,000 write/erase cycles. Reads do not count against this limit, only writes.
A single address rewritten once per second would reach 100,000 writes in a little over a day. If your data changes often, write only when the value actually changes, spread writes across several addresses, or use EEPROM.update().
Example: write a character and read it back
The sketch below writes the letter E to address 10, reads it back from EEPROM and sends it to the serial port.
//writes a letter E to EEPROM and read it from EEPROM and send to serial port
#include <EEPROM.h>
void setup()
{
Serial.begin(9600);
EEPROM.write(10,'E');
delay(10);
}
void loop() {
char a =EEPROM.read(10);
Serial.println(a);
}
The byte actually stored at address 10 is the ASCII code of ‘E’, which is 69 (0x45, or 01000101 in binary). Use the text to ASCII converter to see the code for any character you plan to save, or the ASCII to text decoder to turn raw byte values read back from EEPROM into readable characters.
How the sketch works
Serial.begin(9600)starts serial communication so you can see the result in the Serial Monitor.EEPROM.write(10,'E')stores the character at address 10. The shortdelay(10)afterwards is more than the 3.3 ms the write needs.- In
loop(),EEPROM.read(10)fetches the byte andSerial.println()prints it as a character.
Open the Serial Monitor at 9600 baud and you should see the letter E printed over and over.
One thing to note is that this sketch writes to EEPROM every time the board resets. For a one-off test that is fine. In a real project, use EEPROM.update() in place of write() so an unchanged value does not use up a write cycle.
Example: keep a counter across resets
This sketch counts how many times the board has started. The value survives power loss because it lives in EEPROM.
#include <EEPROM.h>
void setup() {
Serial.begin(9600);
byte count = EEPROM.read(0);
count++;
EEPROM.update(0, count);
Serial.print("Starts: ");
Serial.println(count);
}
void loop() {
}
Because a byte holds at most 255, the counter rolls over to 0 after 255 starts. A brand new chip normally reads 255 (0xFF) in every location, so the first count may not begin at 1. If that matters, write a known value to address 0 once before relying on it.
Tips for reliable EEPROM use
- Plan your addresses. Write down which address holds which value so data does not overlap.
- When storing multi-byte values with
put(), leave enough room: usesizeof()to find out how many bytes a type takes. - Do not write on every pass through
loop(). Write only when something changes. - Do not rely on the initial contents of a fresh chip. Treat it as empty and write defaults first.
- Keep power stable while writing. A brownout in the middle of a write can leave a bad value.
EEPROM on other microcontrollers
The idea is the same on other chips, though the registers and library calls differ. If you also work with PIC chips, see our PIC16F877A EEPROM tutorial, and for a serial example see how to use UART and EEPROM together on the same chip.
FAQ
How much EEPROM does an Arduino Uno have?
The Uno uses an ATmega328, which has 1 KB (1024 bytes) of EEPROM. Boards based on the ATmega168 or ATmega8 have 512 bytes.
Does EEPROM data survive when the power is off?
Yes. EEPROM is non-volatile, so the stored bytes remain after power is removed or the board is reset. Normally they also stay in place when you upload a new sketch, unless the new sketch overwrites them.
How many times can I write to EEPROM?
Each cell is rated for about 100,000 write/erase cycles. Reading does not wear the memory. Using EEPROM.update() helps because it skips writes when the value is unchanged.
Can I store a number larger than 255?
Yes, but it takes more than one byte. Use EEPROM.put() and EEPROM.get() to store and read an int, long or float, or split the number into bytes yourself.