Clearing EEPROM on an Arduino means writing a chosen value to each address in its nonvolatile memory. EEPROM keeps data when the board is switched off, so old settings can remain after you upload a new sketch. To reset those settings, you can run a short program that loops through the EEPROM and overwrites each byte. The exact setup depends on your Arduino board: classic AVR boards such as the Uno and Nano use EEPROM differently from boards that emulate EEPROM in flash memory.
This guide explains how to clear Arduino EEPROM, provides example code, and covers important precautions such as memory size, board compatibility, and write endurance. In most cases, the process is straightforward: include the EEPROM library, write a reset value to every address, and then verify the result.
What Arduino EEPROM Is
EEPROM stands for electrically erasable programmable read-only memory. It is a small area of nonvolatile storage used for information that should survive a power cycle. For example, a sketch might save a calibration value, a counter, a selected operating mode, or a user preference in EEPROM.
Unlike ordinary variables in RAM, EEPROM contents are not automatically cleared when you reset or turn off an Arduino. Uploading a new sketch usually does not clear EEPROM either. That persistence is useful, but it can cause confusing behavior when a program expects a different data format or default setting. Clearing EEPROM gives the sketch a known starting state.
EEPROM is limited in size and write endurance. On an Arduino Uno, for example, the EEPROM has 1,024 bytes, while other boards may provide a different amount or use flash memory to imitate EEPROM. Each storage location also has a finite number of writes, so clearing the entire memory repeatedly is not a good routine operation.
Does Arduino Have an EEPROM.clear() Function?
The standard Arduino EEPROM library does not provide a universal EEPROM.clear() function. Instead, you clear the memory by looping over its addresses and writing a value to each one. The library provides functions such as EEPROM.read(), EEPROM.write(), and EEPROM.update() for working with individual bytes.
For classic AVR-based Arduino boards, including many Uno, Nano, and Mega models, EEPROM.length() reports the number of EEPROM addresses available. You can use it as the loop limit, which makes the code easier to reuse across AVR boards with different EEPROM capacities.
Arduino Code to Clear EEPROM
The following example writes 0xFF to every EEPROM address. On classic AVR EEPROM, an erased byte normally reads as 255, or 0xFF. Using this value can make cleared storage distinguishable from data that your program has deliberately saved as zero.
#include <EEPROM.h>
void setup() {
Serial.begin(9600);
for (int address = 0; address < EEPROM.length(); address++) {
EEPROM.update(address, 0xFF);
}
Serial.println("EEPROM clear complete.");
}
void loop() {
// Nothing to do here.
}
Upload this sketch to the board and let it run. The loop starts at address zero and continues until it reaches the last available EEPROM address. EEPROM.update() checks the current value before writing, so it writes only when the stored byte differs from 0xFF. This can reduce unnecessary writes compared with calling EEPROM.write() unconditionally.
After the message appears in the Serial Monitor, the clear operation has finished. If you are resetting EEPROM for a different project, you can then upload your main sketch. The clear operation normally only needs to run once, not every time the board starts.
Should You Clear EEPROM to 0 or 255?
You can write any byte value from 0 through 255. The right choice depends on how your sketch interprets stored data. Writing 0xFF is a common choice for resetting AVR EEPROM because that is the usual erased state. Writing zero is also valid, but your program may then treat every location as containing a legitimate zero value.
Choose a reset value that your program can recognize as “not initialized.” For example, if a setting can only be between 1 and 10, a value of 255 can serve as an unused marker. Your setup code can check for that marker and load a default value when it finds one.
#include <EEPROM.h>
const int modeAddress = 0;
const byte uninitialized = 0xFF;
void setup() {
byte savedMode = EEPROM.read(modeAddress);
if (savedMode == uninitialized) {
savedMode = 1; // Default mode
EEPROM.update(modeAddress, savedMode);
}
}
void loop() {
}
Do not assume that zero always means “empty.” If zero is a valid saved setting, use a separate marker or a small data structure that includes a version number or validity flag.
Using EEPROM.write() Instead of EEPROM.update()
You can replace EEPROM.update(address, 0xFF) with EEPROM.write(address, 0xFF). Both functions store a byte, but write() performs the write even when the location already contains the requested value. update() avoids writing when the old and new values match.
Because EEPROM cells can only handle a limited number of write cycles, update() is usually the better option for a clearing loop. It does not eliminate wear on bytes that actually change, but it avoids needless writes to bytes that are already cleared. This matters particularly if you are experimenting with the sketch and running it many times.
Run the Clear Operation Only When Needed
A common mistake is to leave the EEPROM-clearing loop in setup() permanently. The setup() function runs after every reset or power-up, so that sketch would clear the EEPROM each time the board starts. Any data saved by another program would be lost as soon as the clear sketch ran again.
Use a dedicated, temporary clearing sketch when you want to erase the contents. Once the clear is complete, upload the application sketch you intend to use. Alternatively, add a deliberate reset condition to your main program, such as a button press held during startup, and clear the memory only when that condition is detected.
Before clearing EEPROM, make sure you do not need the saved information. If the data matters, read it back or make a copy first. Clearing is destructive: the old values cannot be recovered through the normal EEPROM library after they have been overwritten.
Board Compatibility and EEPROM Emulation
The AVR example is appropriate for boards whose core supports the standard EEPROM library and exposes EEPROM directly. Other Arduino-compatible boards may handle EEPROM differently. Some use a section of flash memory as an EEPROM emulation layer, and some require a memory size to be configured before reading or writing.
For example, ESP8266 and ESP32 Arduino cores commonly require you to initialize the EEPROM emulation with a call such as EEPROM.begin(size). On these platforms, changes may need to be saved with EEPROM.commit(). The exact requirements and supported library behavior can differ by board package, so check the documentation for your selected board before using AVR code unchanged.
Flash-based emulation may also have different wear characteristics and erase behavior from physical AVR EEPROM. Do not assume that EEPROM.length(), initialization, or commit behavior is identical across every board. If the compiler reports that a method is unavailable, or if changes do not persist after a restart, consult the board core’s EEPROM examples and reference.
How to Check Whether EEPROM Was Cleared
You can verify the result by reading each address and printing its value to the Serial Monitor. For a quick test on an AVR board, add this code after the clearing loop:
for (int address = 0; address < EEPROM.length(); address++) {
Serial.print(address);
Serial.print(": ");
Serial.println(EEPROM.read(address));
}
If you wrote 0xFF, the output should show 255 for each address. If you wrote zero, each address should show 0. For a large EEPROM, printing every byte can produce a lot of output, so you can check only a few locations or count how many bytes do not match the expected value.
Remember that a successful compile does not necessarily mean the operation suits your board. Confirm that the correct board and port are selected in the Arduino IDE, upload the sketch, and open the Serial Monitor at the baud rate specified by Serial.begin().
Common Problems When Clearing Arduino EEPROM
-
“EEPROM was not declared”: Check that
#include <EEPROM.h>is present and that the selected board supports the library. - “EEPROM.length() was not declared”: The selected board core may use a different API. Check its EEPROM examples and documentation.
- Data returns after restarting: A flash-emulated EEPROM may require initialization or an explicit commit operation.
-
The EEPROM clears on every startup: The clearing loop is still running in the sketch’s
setup(). Remove it or add a condition so it runs only on request. - The application reads strange values: Confirm that the sketch uses the same address, data type, and layout that were used to save the information.
Conclusion
To clear EEPROM on a classic Arduino, include EEPROM.h and loop through the available addresses, writing a reset value such as 0xFF with EEPROM.update(). Use EEPROM.length() to find the memory size, verify the result if needed, and run the clearing sketch only when you intend to erase saved data.
Board differences matter: Arduino-compatible devices that emulate EEPROM in flash may require initialization and a commit step. Check your board’s documentation, choose a reset value your program can identify, and avoid repeated full-memory writes to protect storage endurance. With those precautions, clearing Arduino EEPROM is a simple way to restore a known starting state for your project.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.