Storing Data on EEPROM with Arduino
Difficulty Level: Intermediate
This tutorial explains how to store data on an EEPROM using Arduino. The EEPROM (Electrically Erasable Programmable Read-Only Memory) is useful for saving data such as user settings, sensor readings, or calibration values that need to be retained even after the Arduino is powered down.
Components Required
- 1 x Arduino (any model, e.g., Uno, Nano, Mega)
- EEPROM Library (built into the Arduino IDE)
- Breadboard and jumper wires (optional, if using an external EEPROM like the 24LC256)
- Computer with Arduino IDE installed
What is EEPROM?
EEPROM is a type of non-volatile memory, meaning it retains data even when power is removed from the Arduino. Unlike SRAM (used for variables during runtime) or Flash (used for program storage), EEPROM is designed for small, persistent data storage. This makes it ideal for saving data that should persist across reboots, such as:
- User-defined settings (e.g., LED brightness preferences)
- Calibration data (e.g., sensor offsets)
- State tracking (e.g., last known position of a motor)
On most Arduino boards, the EEPROM is built into the microcontroller, but external EEPROM chips can also be used for larger storage needs.
Using the EEPROM Library
Arduino provides a built-in EEPROM
library that simplifies reading and writing data to the EEPROM memory. Each EEPROM location can store one byte (8 bits), which means values from 0 to 255. For larger values or complex data, you'll need to split the data across multiple addresses.
Arduino Code Explanation
In the following example, we’ll write a number to the EEPROM and read it back to verify the storage.
Basic Example: Storing and Reading a Byte
// Include the EEPROM library
#include <EEPROM.h>
void setup() {
// Start serial communication to monitor EEPROM actions
Serial.begin(9600);
// Define a byte to store in EEPROM
byte storedValue = 42;
// Write the value to EEPROM at address 0
EEPROM.write(0, storedValue);
Serial.print("Value written to EEPROM: ");
Serial.println(storedValue);
// Read the value back from EEPROM
byte readValue = EEPROM.read(0);
Serial.print("Value read from EEPROM: ");
Serial.println(readValue);
// Check if the written and read values match
if (storedValue == readValue) {
Serial.println("Success: Values match!");
} else {
Serial.println("Error: Values do not match!");
}
}
void loop() {
// Nothing needed here
}
Code Explanation
- EEPROM.write(address, value)
: Writes a single byte (0–255) to the specified EEPROM address (e.g., 0 to 1023 on an Arduino Uno).
- EEPROM.read(address)
: Reads the byte stored at the specified address and returns it.
- In this example, we write the value 42
to address 0
, then read it back and compare it to ensure the operation was successful.
Advanced: Reading and Writing Larger Data Types
For values larger than a byte (e.g., int
, float
, or custom structures), you’ll need to use the EEPROM.put()
and EEPROM.get()
functions, which handle multi-byte data automatically.
Example: Storing and Reading an Integer
#include <EEPROM.h>
void setup() {
Serial.begin(9600);
// Define an integer to store
int someInt = 1234;
// Write the integer to EEPROM starting at address 0
EEPROM.put(0, someInt);
Serial.print("Integer written to EEPROM: ");
Serial.println(someInt);
// Read the integer back from EEPROM
int readInt;
EEPROM.get(0, readInt);
Serial.print("Integer read from EEPROM: ");
Serial.println(readInt);
}
void loop() {
// No code in the loop
}
Code Explanation
- EEPROM.put(address, variable)
: Writes the variable (e.g., int
, float
) to EEPROM starting at the specified address. It automatically handles the number of bytes required (e.g., 2 bytes for an int
on most Arduino boards).
- EEPROM.get(address, variable)
: Reads the data from EEPROM into the specified variable, assuming the correct number of bytes.
- In this example, 1234
(a 2-byte int
) is stored across addresses 0 and 1.
Example: Storing a Custom Structure
#include <EEPROM.h>
struct Settings {
int brightness;
float temperature;
char mode;
};
void setup() {
Serial.begin(9600);
// Create a settings instance
Settings mySettings = {150, 23.5, 'A'};
// Store the structure in EEPROM
EEPROM.put(0, mySettings);
// Read it back
Settings readSettings;
EEPROM.get(0, readSettings);
// Display the results
Serial.print("Brightness: "); Serial.println(readSettings.brightness);
Serial.print("Temperature: "); Serial.println(readSettings.temperature);
Serial.print("Mode: "); Serial.println(readSettings.mode);
}
void loop() {}
Upload and Test
1. Connect your Arduino to your computer via USB.
2. Open the Arduino IDE, paste one of the example codes, and upload it to your board.
3. Open the Serial Monitor (Ctrl+Shift+M) and set the baud rate to 9600
.
4. Observe the output to confirm the data was written and read correctly.
EEPROM Memory Size
The EEPROM capacity varies by Arduino model:
- Arduino Uno/Nano: 1024 bytes (1 KB)
- Arduino Mega: 4096 bytes (4 KB)
- Arduino Due: No built-in EEPROM (requires external chip or emulation)
- External EEPROM (e.g., 24LC256): Up to 32 KB or more, accessed via I2C.
To check your board’s EEPROM size programmatically:
Serial.println(E2END + 1); // E2END is the last valid address
EEPROM Lifespan and Best Practices
EEPROM has a finite lifespan of approximately 100,000 write cycles per address. To maximize its longevity:
- Minimize Writes: Avoid writing to EEPROM in a loop; only update it when necessary (e.g., when a setting changes).
- Use Wear Leveling: Spread writes across multiple addresses if frequent updates are needed.
- Check Before Writing: Read the current value first and only write if it’s different to avoid unnecessary wear.
Example of write optimization:
byte currentValue = EEPROM.read(0);
if (currentValue != newValue) {
EEPROM.write(0, newValue);
}
Troubleshooting Tips
- Garbage Values: If you read unexpected data, the EEPROM address may not have been initialized. Write a known value first.
- Out-of-Bounds Errors: Ensure your address doesn’t exceed your board’s EEPROM size (e.g., 0–1023 for Uno).
- Serial Monitor Blank: Double-check the baud rate matches Serial.begin()
.
Practical Application: Saving Sensor Data
Here’s an example of saving the last temperature reading from a sensor:
#include <EEPROM.h>
void setup() {
Serial.begin(9600);
// Simulate a temperature reading
float temp = 25.7;
// Save to EEPROM
EEPROM.put(0, temp);
Serial.print("Saved temperature: ");
Serial.println(temp);
// Simulate a reboot and retrieve the value
float lastTemp;
EEPROM.get(0, lastTemp);
Serial.print("Last temperature: ");
Serial.println(lastTemp);
}
void loop() {}
Conclusion
EEPROM provides a simple way to store persistent data in Arduino projects, making it perfect for settings, logs, or sensor states. Use EEPROM.write()
and EEPROM.read()
for single-byte operations, and EEPROM.put()
and EEPROM.get()
for multi-byte data like integers, floats, or structures. By understanding its limitations (e.g., write cycles) and applying best practices, you can effectively integrate EEPROM into your projects. Have questions? Check the FAQ above or explore the Further Reading section for more inspiration!
For larger storage needs, consider external EEPROM chips or SD cards. Experiment with the examples provided, and don’t forget to browse the Resources section for additional tools and references. Happy coding!
FAQ
- What happens if I write to an EEPROM address more than 100,000 times?
- After exceeding the typical write cycle limit (about 100,000), the EEPROM address may become unreliable and fail to store data correctly. To extend lifespan, use wear leveling or check if the new value differs before writing.
- Can I store negative numbers in EEPROM?
-
Yes, but you need to use
EEPROM.put()
for multi-byte types likeint
orfloat
. A single byte (viaEEPROM.write()
) only supports values from 0 to 255, so negative values require special handling such as offsetting or byte-splitting. - Why am I getting random values when reading from EEPROM?
-
Uninitialized EEPROM locations may contain unpredictable data. To ensure consistency, write known defaults during setup or use a full clear loop like:
for (int i = 0; i < EEPROM.length(); i++) { EEPROM.write(i, 0); }
- Can I use EEPROM with an Arduino Due, which has no built-in EEPROM?
-
The Arduino Due lacks onboard EEPROM, but you can emulate it using libraries like
DueFlashStorage
or connect an external EEPROM chip (e.g., 24LC256) via I2C. - How do I know how much EEPROM space I’ve used?
-
Use
EEPROM.length()
to get the total size. Then track your usage manually based on data types — for example,int
uses 2 bytes,float
uses 4 bytes, etc. - Is there a way to erase the entire EEPROM?
- Yes. To clear the entire EEPROM, write a default value (such as 0) to every address using a loop. Refer to the example in the “random values” question above.
Further Reading
Want to dive deeper into Arduino memory management or expand your project? Check out these articles on our site:
- "Understanding Arduino Memory: SRAM, Flash, and EEPROM Explained"
Learn the differences between Arduino’s memory types and when to use each.
- "Using External EEPROM with Arduino: A Step-by-Step Guide"
Explore how to add more storage with chips like the 24LC256 for bigger projects.
- "Saving Sensor Data: EEPROM vs. SD Cards"
Compare EEPROM with SD cards for data logging and decide which is best for your needs.
- "Optimizing Arduino Code for Longevity"
Discover techniques to reduce wear on hardware components like EEPROM.
- "Building a Smart Config System with EEPROM"
Create a project that saves and loads user settings dynamically.
Resources
Here are some helpful tools and references to support your EEPROM adventures:
- Arduino EEPROM Library Reference: Official Arduino Documentation – Detailed info on EEPROM.read()
, EEPROM.write()
, EEPROM.put()
, and more.
- ATmega328P Datasheet: Microchip ATmega328P – Technical specs for the Uno’s microcontroller, including EEPROM details (PDF download).
- EEPROM Playground: GitHub - EEPROM Examples – Sample sketches to experiment with EEPROM functions.
- I2C EEPROM Tutorial: SparkFun I2C EEPROM Guide – A beginner’s guide to using external EEPROM chips.
- Arduino Forum: Community Forum – Ask questions, share projects, and troubleshoot with other Arduino enthusiasts.
- EEPROM Wear Leveling Techniques: Hackaday Article – Advanced strategies for extending EEPROM life.