In this tutorial, you’ll learn how to use the ESP32 EEPROM library in Arduino IDE to save data that survives a reset or a power loss. You’ll learn how to read and write bytes, int, float, String, and struct variables, how much data the ESP32 EEPROM can really hold (measured, not guessed), how long the flash lasts, and when you should use the Preferences library instead.
The ESP32 doesn’t have a real EEPROM. The EEPROM.h library emulates one in the flash memory, and knowing how it does that explains every problem people hit with it. We’ll build a few example projects: a boot counter, saving the last LED state, storing all data types with put() and get(), a maximum EEPROM size test, and the same job done with Preferences. Without further ado, let’s get right into it!
ESP32 EEPROM Read & Write Example
To save data in the ESP32 EEPROM, include EEPROM.h, reserve the bytes you need with EEPROM.begin(size), change them with EEPROM.write(address, value), and call EEPROM.commit() to actually store them in flash. Read them back with EEPROM.read(address). Here is a complete boot counter that remembers how many times the ESP32 has started:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/* * LAB Name: ESP32 EEPROM Read & Write Example (Boot Counter) * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com */ #include <EEPROM.h> #define EEPROM_SIZE 1 // Bytes reserved in flash (NVS) for our data void setup() { Serial.begin(115200); EEPROM.begin(EEPROM_SIZE); // Load the EEPROM copy from flash into RAM uint8_t bootCount = EEPROM.read(0); // A never-written byte reads 255 (0xFF) if (bootCount == 0xFF) { bootCount = 0; } bootCount++; EEPROM.write(0, bootCount); // Change the RAM copy only EEPROM.commit(); // Save the RAM copy to flash Serial.printf("This ESP32 has booted %u times\n", bootCount); } void loop() { } |
Open the serial monitor at 115200 baud and press the EN (reset) button a few times: the count keeps going up, even after you unplug the board. Note the 0xFF check: a byte you’ve never written reads 255, not 0. The rest of this tutorial explains how the ESP32 EEPROM works, its real size limit, the write cycles, and the full examples.

Note: All examples in this tutorial are tested with: ESP32 DevKit V1 (ESP32-WROOM-32), Arduino-ESP32 core 3.3.12, Arduino IDE 2.x. The EEPROM library API didn’t change in core 3.x, so the same code also runs on the older core 2.x.
Does the ESP32 Have EEPROM?
No, the ESP32 has no internal EEPROM. The Arduino EEPROM.h library for the ESP32 emulates an EEPROM in the external SPI flash chip, inside the NVS (Non-Volatile Storage) partition. The data still survives resets and power loss, just like a real EEPROM, and the functions look like the classic Arduino EEPROM library that most of us are already familiar with.
An EEPROM (Electrically Erasable Programmable Read-Only Memory) is a type of NVM (Non-Volatile Memory) just like FLASH. Both are used for permanent data storage. The difference is that an EEPROM can rewrite a single byte, while a flash memory has to erase a whole 4kB sector before it can write to it again. That’s why the ESP32 needs a software layer (NVS) between your sketch and the flash.
Typical data you’d keep in the ESP32 EEPROM (or NVS) includes:
- WiFi credentials and passwords
- User configuration and app-level parameters
- Sensor calibration data
- The last sensor reading or the last output state
- State variables (for some state machines) and counters
Note: Espressif marks the EEPROM library as deprecated: it’s kept for backward compatibility with existing Arduino code, and new ESP32 projects should use the Preferences library. EEPROM.h still ships with core 3.3.12 and works fine, so you can keep using it. We’ll compare both later in this tutorial.
How ESP32 EEPROM Emulation Works (NVS)
The ESP32 EEPROM library keeps a copy of your whole EEPROM in RAM and saves it to flash as one NVS blob (namespace "eeprom", key "eeprom") every time you call commit(). read() and write() only touch the RAM copy, which is why nothing is saved until you commit.

Here is what happens under the hood, from the library’s source code in core 3.3.12:
- EEPROM.begin(size) opens the NVS namespace, allocates size bytes of RAM, and loads the stored blob into it. New (never-written) space is filled with 0xFF. If NVS doesn’t have room for the requested size, begin() returns false.
- EEPROM.write() / put() copy your data into the RAM buffer. write() only marks the buffer as changed (“dirty”) if the new byte is different from the old one.
- EEPROM.commit() writes the whole buffer to NVS as one blob, but only if something changed. It returns true on success.
- EEPROM.end() commits any pending changes and frees the RAM.
NVS itself lives in a 20kB partition ( nvs, 0x9000, 0x5000 in the default partition table), split into five 4kB pages. It writes data as 32-byte entries, appends every new version after the old one, and erases a page only when it’s full of old data. That’s built-in wear leveling, and it’s shared with WiFi, Bluetooth, and Preferences, which store their own keys in the same partition.
ESP32 EEPROM Library Functions
The ESP32 EEPROM library has the classic Arduino functions plus typed read and write functions for every common data type. Every address is a byte address from 0 to size – 1, and each byte holds a value from 0 to 255.
| Function | What It Does |
|---|---|
EEPROM.begin(size) | Loads (or creates) an EEPROM of size bytes. Returns false if there’s no room in NVS |
EEPROM.read(addr) | Returns the byte at addr (0 – 255). A never-written byte reads 255 |
EEPROM.write(addr, val) | Writes one byte to the RAM copy (only marks it changed if the value differs) |
EEPROM.commit() | Saves the RAM copy to flash. Returns true on success. Does nothing if nothing changed |
EEPROM.put(addr, var) | Writes any type or struct, sizeof(var) bytes starting at addr |
EEPROM.get(addr, var) | Reads any type or struct back into var |
EEPROM.writeInt(), writeFloat(), writeString(), … | Typed writes. Return the number of bytes written (0 on error) |
EEPROM.readInt(), readFloat(), readString(), … | Typed reads |
EEPROM.length() | Returns the size you passed to begin() |
EEPROM.isDirty() | True if there are changes that haven’t been committed yet |
EEPROM.end() | Commits pending changes and frees the RAM copy |
EEPROM Write & Commit
To use the ESP32 EEPROM library, include the EEPROM.h header file, then start the EEPROM with the number of bytes you need. If you know exactly how many bytes you need, use it as a limit. Otherwise, allocate a slightly larger chunk of memory to give yourself a safety buffer.
|
1 2 3 4 5 6 7 8 9 |
#include <EEPROM.h> #define EEPROM_SIZE 64 // Number of bytes you need void setup() { EEPROM.begin(EEPROM_SIZE); EEPROM.write(0, 123); // address 0 = 123 (in RAM) EEPROM.commit(); // now it's saved in flash } |
The data you pass to EEPROM.write() is not written to the flash memory until you call EEPROM.commit(). If the ESP32 resets before the commit, the change is lost.
EEPROM Read
To read a byte (single memory location), use EEPROM.read(address). It returns the content of that memory location from the RAM copy, so reading is fast and doesn’t wear the flash at all.
|
1 |
uint8_t data = EEPROM.read(0); |
EEPROM Write/Read String & Float
To write or read a float or a String, you don’t need to split it into bytes yourself. Use the typed functions, and remember that each one takes as many bytes as its type: a float takes 4 bytes, and a String takes its length + 1 byte for the null terminator.
|
1 2 3 4 5 6 |
EEPROM.writeFloat(10, 3.14159f); // bytes 10 - 13 float f = EEPROM.readFloat(10); EEPROM.writeString(20, "DeepBlue"); // bytes 20 - 28 (8 chars + '\0') String s = EEPROM.readString(20); EEPROM.commit(); |
EEPROM put() & get() for Any Data Type
The put() and get() functions write and read any data type, including your own structures, in one call. They use sizeof() of the variable to know how many bytes to copy.
|
1 2 |
EEPROM.put(address, myData); // Write any data type or struct EEPROM.get(address, myData); // Read it back into myData |
All ESP32 EEPROM read & write functions (EEPROM.h, core 3.x)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// [EEPROM Write Functions] return the number of bytes written (0 on error) size_t writeByte(int address, uint8_t value); size_t writeChar(int address, int8_t value); size_t writeUChar(int address, uint8_t value); size_t writeShort(int address, int16_t value); size_t writeUShort(int address, uint16_t value); size_t writeInt(int address, int32_t value); size_t writeUInt(int address, uint32_t value); size_t writeLong(int address, int32_t value); size_t writeULong(int address, uint32_t value); size_t writeLong64(int address, int64_t value); size_t writeULong64(int address, uint64_t value); size_t writeFloat(int address, float_t value); size_t writeDouble(int address, double_t value); size_t writeBool(int address, bool value); size_t writeString(int address, const char* value); size_t writeString(int address, String value); size_t writeBytes(int address, const void* value, size_t len); // [EEPROM Read Functions] uint8_t readByte(int address); int8_t readChar(int address); uint8_t readUChar(int address); int16_t readShort(int address); uint16_t readUShort(int address); int32_t readInt(int address); uint32_t readUInt(int address); int32_t readLong(int address); uint32_t readULong(int address); int64_t readLong64(int address); uint64_t readULong64(int address); float_t readFloat(int address); double_t readDouble(int address); bool readBool(int address); size_t readString(int address, char* value, size_t maxLen); String readString(int address); size_t readBytes(int address, void * value, size_t maxLen); |
ESP32 EEPROM Example Wiring & Parts
Only LAB 1 needs any wiring: an LED on GPIO25 and a push button on GPIO2. LAB 2, LAB 3, and LAB 4 run on the bare ESP32 board and print their results to the serial monitor.

3V3 goes to one leg of the push button (through the breadboard’s + rail).
GND goes to the LED’s cathode (short leg) and to the 10kΩ pull-down resistor, through the breadboard’s − rail.
GPIO2 goes to the other leg of the push button and to the 10kΩ pull-down resistor. The pin reads HIGH while the button is pressed.
GPIO25 goes to the 330Ω resistor, and the resistor goes to the LED’s anode (long leg).
For the full pin map of the board, check out the ESP32 DevKit V1 Pinout guide.
| QTY. | Component Name | Amazon.com | AliExpress |
| 1 | ESP32 DevKit V1 (DOIT) Board | Amazon | AliExpress |
| 1 | LEDs Kit | Amazon | AliExpress |
| 1 | Push Buttons | Amazon | AliExpress |
| 1 | Resistors Kit (330Ω, 10kΩ) | Amazon | AliExpress |
| 1 | Breadboard & Jumper Wires | Amazon | AliExpress |
| 1 | Micro USB Cable | Amazon | AliExpress |
ESP32 EEPROM Example: Save Last LED State (LAB 1)
In this example, we’ll test the ESP32 EEPROM by saving the last LED state. The LED is toggled by a push button, and whenever its state changes, the new state is saved to the EEPROM. Then we’ll reset the ESP32 board, and it should restore the last saved state from the EEPROM.
Wiring: the LED (with a 330Ω resistor) goes to GPIO25 and the push button goes between GPIO2 and 3V3 with a 10kΩ pull-down from GPIO2 to GND, as shown in the wiring diagram above.
Here is The Application Code For This Example (ESP32_EEPROM_LED_State_LAB1.ino)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* * LAB Name: ESP32 EEPROM Save Last LED State (Push Button Toggle) * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com */ #include <EEPROM.h> #define EEPROM_SIZE 1 // We only need 1 byte for the LED state #define LED_STATE_ADDR 0 // EEPROM address of the saved LED state #define LED_GPIO 25 #define BTN_GPIO 2 // Button to 3.3v with a 10k pull-down resistor int ledState = LOW; int btnState = LOW; int lastBtnState = LOW; unsigned long lastDebounceTime = 0; const unsigned long debounceDelay = 50; void setup() { Serial.begin(115200); pinMode(LED_GPIO, OUTPUT); pinMode(BTN_GPIO, INPUT); EEPROM.begin(EEPROM_SIZE); // Restore the last saved state. A blank (never-written) byte reads 0xFF uint8_t saved = EEPROM.read(LED_STATE_ADDR); ledState = (saved == HIGH) ? HIGH : LOW; digitalWrite(LED_GPIO, ledState); Serial.printf("Restored LED state from flash: %s\n", ledState ? "ON" : "OFF"); } void loop() { int reading = digitalRead(BTN_GPIO); if (reading != lastBtnState) { lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != btnState) { btnState = reading; if (btnState == HIGH) { ledState = !ledState; digitalWrite(LED_GPIO, ledState); EEPROM.write(LED_STATE_ADDR, ledState); EEPROM.commit(); // Save to flash only when the state really changes Serial.println("State Changed & Saved To FLASH!"); } } } lastBtnState = reading; } |
ESP32 EEPROM LED State Example Code Explanation
The code reads the button input pin and debounces it to make sure it’s not picking up any noise, and toggles the LED if the button is actually pressed. The LED toggle event also writes the current ledState to the LED_STATE_ADDR location in the EEPROM and commits it to the FLASH memory.
In the setup() function, we start the EEPROM with a size of 1 byte, then read the last saved LED state and apply it to the LED output. This happens only at startup, hence it’s done in the setup() function. A brand-new EEPROM byte reads 0xFF, so we treat anything other than HIGH as LOW. Otherwise, the very first boot would read 255 and you’d get an LED state that you never saved.
|
1 2 3 4 |
EEPROM.begin(EEPROM_SIZE); uint8_t saved = EEPROM.read(LED_STATE_ADDR); ledState = (saved == HIGH) ? HIGH : LOW; digitalWrite(LED_GPIO, ledState); |
In the loop() function, it’s mostly about reading the button and debouncing it. Once it’s clear that the button is pressed and it’s not noise, we toggle the LED and save the new state. We commit only on a real state change, never on every loop pass, which keeps the flash writes to a minimum.
|
1 2 3 4 |
ledState = !ledState; digitalWrite(LED_GPIO, ledState); EEPROM.write(LED_STATE_ADDR, ledState); EEPROM.commit(); |
Note: lastDebounceTime is an unsigned long, like the value millis() returns. Storing millis() in a signed int works at first, but it goes negative after about 24.8 days and the debounce logic breaks.
ESP32 EEPROM LED State Example Testing Result
Here is a short demo video for the testing result of this example on my ESP32 board. Note that the ESP32 remembers the last LED state after every reset. And that’s the most simple application for using non-volatile memories.
ESP32 EEPROM Store int, float, String & struct (LAB 2)
In this example, we’ll store an int, a float, a String, and a struct in the ESP32 EEPROM, then read them all back after a reset. The trick is the address map: every item starts right after the previous one, at an address you calculate with sizeof(). Two items that overlap will corrupt each other, and that’s the number one reason people get garbage values back.
| Item | Type | Address | Size |
|---|---|---|---|
| Counter | int32_t | 0 | 4 bytes |
| Calibration | float | 4 | 4 bytes |
| Device name | String (max 32 chars) | 8 | 33 bytes (+1 for ‘\0’) |
| Settings | struct Settings | 41 | sizeof(Settings) = 16 bytes |
Note that the struct takes 16 bytes, not the 11 bytes its members add up to: the compiler pads it so the float starts on a 4-byte boundary. That’s why we always use sizeof() instead of adding up the sizes by hand. The whole EEPROM for this example is 57 bytes.
The struct also has a magic number. On the first run, the EEPROM is blank (all 0xFF), the magic number doesn’t match, and the code writes default values instead of trusting garbage. You can change the magic number whenever you change the struct layout, and old data is ignored automatically.
Wiring: none, just the ESP32 board on USB.
Here is The Application Code For This Example (ESP32_EEPROM_Data_Types_LAB2.ino)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
/* * LAB Name: ESP32 EEPROM Store int, float, String & struct (put/get) * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com */ #include <EEPROM.h> struct Settings { uint32_t magic; // Tells us the data is ours and valid int16_t setpoint; float calibration; bool ledEnabled; }; #define MAGIC_NUMBER 0xDB32EE01 // Address map: each item starts right after the previous one #define ADDR_INT 0 // int32_t : 4 bytes #define ADDR_FLOAT (ADDR_INT + sizeof(int32_t)) // float : 4 bytes #define ADDR_STRING (ADDR_FLOAT + sizeof(float)) // up to 32 chars + '\0' #define STRING_MAX 33 #define ADDR_STRUCT (ADDR_STRING + STRING_MAX) #define EEPROM_SIZE (ADDR_STRUCT + sizeof(Settings)) void setup() { Serial.begin(115200); delay(1000); if (!EEPROM.begin(EEPROM_SIZE)) { Serial.println("EEPROM.begin() failed!"); while (1); } Serial.printf("EEPROM size: %u bytes\n", EEPROM.length()); // 1. Read what the last run saved Settings s; EEPROM.get(ADDR_STRUCT, s); if (s.magic != MAGIC_NUMBER) { Serial.println("No valid data yet (first run). Writing defaults..."); s = {MAGIC_NUMBER, 25, 1.0f, true}; } else { Serial.println("Data restored from flash:"); Serial.printf(" int = %ld\n", (long)EEPROM.readInt(ADDR_INT)); Serial.printf(" float = %.3f\n", EEPROM.readFloat(ADDR_FLOAT)); Serial.printf(" String = %s\n", EEPROM.readString(ADDR_STRING).c_str()); Serial.printf(" struct = {setpoint=%d, calibration=%.3f, ledEnabled=%d}\n", s.setpoint, s.calibration, s.ledEnabled); s.setpoint++; // Change something so we can see it next boot s.calibration *= 1.01f; } // 2. Write new values (RAM copy), then commit once EEPROM.writeInt(ADDR_INT, -12345); EEPROM.writeFloat(ADDR_FLOAT, 3.14159f); EEPROM.writeString(ADDR_STRING, "DeepBlueMbedded"); EEPROM.put(ADDR_STRUCT, s); if (EEPROM.commit()) { Serial.println("Saved. Press the EN (reset) button to read it back."); } else { Serial.println("EEPROM.commit() failed!"); } } void loop() { } |
ESP32 EEPROM Data Types Example Code Explanation
We define each address from the previous one, so changing a type or a size moves everything after it automatically. The total EEPROM_SIZE is the end of the last item:
|
1 2 3 4 5 6 |
#define ADDR_INT 0 #define ADDR_FLOAT (ADDR_INT + sizeof(int32_t)) #define ADDR_STRING (ADDR_FLOAT + sizeof(float)) #define STRING_MAX 33 #define ADDR_STRUCT (ADDR_STRING + STRING_MAX) #define EEPROM_SIZE (ADDR_STRUCT + sizeof(Settings)) |
We check the return value of EEPROM.begin() and EEPROM.commit(). Both return false when NVS is full or broken, and that’s much easier to debug than values that silently don’t stick. All the writes go to the RAM copy first, and one commit() at the end saves them together in a single flash write.
ESP32 EEPROM Data Types Example Result
On the very first run, the magic number doesn’t match, so the sketch writes the default values. Here is the serial monitor output on my ESP32 board for the next two resets: every value comes back from the flash, and the struct’s setpoint and calibration change on every boot, as the code tells them to.

ESP32 EEPROM Size: How Much Data Can You Store?
On Arduino-ESP32 core 3.3.12 with the default partition table, the largest EEPROM I could start, write, and commit was 6,144 bytes (LAB 3 below). It’s not 512 bytes or 4kB, as many pages and AI answers still claim: those numbers come from the old ESP8266 port and from the 2018 versions of the library.
The limit comes from NVS. The ESP-IDF NVS documentation limits a blob to 97.6% of the partition size − 4000 bytes, which is 15,988 bytes for the default 20kB partition. In practice, you get less than that because:
- NVS keeps one 4kB page free for its garbage collection.
- Every NVS entry is 32 bytes and a blob also needs index entries, so there’s some overhead.
- On begin(), the library checks for free space by writing a temporary blob of the extra size, and every commit() writes the new copy of the blob before it erases the old one.
- WiFi, Bluetooth, and Preferences store their data in the same 20kB partition.
On my board, NVS had 548 of its 630 entries free (82 were already used by other data stored on the board). That’s about 17.5kB, minus the 4kB page NVS keeps free, so roughly 13.5kB of usable space. Because begin() and commit() need room for about two copies of the EEPROM at the same time, the largest EEPROM that works is about half of that: 6,144 bytes worked and 6,656 bytes failed.
Back in 2023, on the Arduino-ESP32 core 2.x, I tested this on two identical ESP32 boards and got a little over 11kB on one and about 13kB on the other. On core 3.3.12, the same board tops out at 6,144 bytes, because begin() and commit() now need free NVS space for about two copies of the EEPROM at once. LAB 3 below closes and reopens the EEPROM and checks every byte from flash, so it gives the number for your core and your board. The exact number depends on what else is stored in NVS on your board, so check the return value of begin() and commit() instead of trusting any fixed number. If you need more space, use a custom partition table with a bigger NVS partition (core 3.3.12 ships a “Storage APP (2MB APP/1MB NVS/1MB SPIFFS)” scheme), or store files with LittleFS.
ESP32 EEPROM Maximum Size Test (LAB 3)
This sketch finds the largest working EEPROM size on your own board. It clears only the "eeprom" NVS namespace (your WiFi credentials and Preferences data are not touched), then tries EEPROM.begin() with sizes growing in 512-byte steps. For every size, it writes a test pattern, commits it, closes the EEPROM, opens it again, and checks every byte.
Wiring: none, just the ESP32 board on USB.
Here is The Application Code For This Example (ESP32_EEPROM_Max_Size_Test_LAB3.ino)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
/* * LAB Name: ESP32 EEPROM Maximum Size Test (NVS-backed EEPROM.h) * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com * * Finds the largest EEPROM.begin() size that can still be written and * committed on your board. It clears ONLY the "eeprom" NVS namespace. */ #include <EEPROM.h> #include <Preferences.h> #include <nvs.h> #define STEP_SIZE 512 void clearEepromNamespace() { Preferences p; p.begin("eeprom", false); // EEPROM.h keeps its data in this namespace p.clear(); p.end(); } void printNvsStats() { nvs_stats_t st; if (nvs_get_stats(NULL, &st) == ESP_OK) { Serial.printf(" NVS entries: used=%u free=%u total=%u (32 bytes each)\n", st.used_entries, st.free_entries, st.total_entries); } } bool testSize(size_t size) { if (!EEPROM.begin(size)) { return false; } for (size_t i = 0; i < size; i++) { EEPROM.write(i, (uint8_t)(i * 7 + 1)); // Pattern that differs from 0xFF } bool ok = EEPROM.commit(); EEPROM.end(); if (!ok) { return false; } // Read back after a fresh begin() to prove the data really is in flash if (!EEPROM.begin(size)) { return false; } for (size_t i = 0; i < size; i++) { if (EEPROM.read(i) != (uint8_t)(i * 7 + 1)) { ok = false; break; } } EEPROM.end(); return ok; } void setup() { Serial.begin(115200); delay(1500); Serial.println("\nESP32 EEPROM Maximum Size Test"); printNvsStats(); size_t lastGood = 0; for (size_t size = STEP_SIZE; size <= 20480; size += STEP_SIZE) { clearEepromNamespace(); // Start every size from an empty EEPROM bool ok = testSize(size); Serial.printf("EEPROM.begin(%5u): %s\n", size, ok ? "OK" : "FAILED"); if (!ok) { break; } lastGood = size; } clearEepromNamespace(); Serial.printf("Largest working EEPROM size: %u bytes\n", lastGood); printNvsStats(); } void loop() { } |
And here is the result on my ESP32 DevKit V1 board (core 3.3.12, default 4MB partition table): the largest working EEPROM size is 6,144 bytes, and NVS is back to its original state after the test.


ESP32 EEPROM Write Cycles & Flash Wear
The SPI flash on common ESP32 modules is rated for about 100,000 erase cycles per 4kB sector. A real EEPROM typically gives you 100,000 to 1,000,000 cycles per byte. Because the ESP32 EEPROM is emulated in flash, every commit() ends up as flash writes, and the sectors wear out eventually if you commit too often.
The good news is that NVS spreads the writes over its pages: each new version of your blob is appended to free space, and a page is erased only when it’s full of old data. The bad news is that the EEPROM library rewrites the whole blob on every commit, so a big EEPROM wears the flash faster than a small one.
Here is a worked example. With a 64-byte EEPROM, one commit takes about 4 entries (one header entry, two data entries, and a blob index entry). The 4 usable NVS pages hold about 4 x 126 = 504 entries, so roughly 126 commits fit before each page has to be erased once. 100,000 erase cycles x 126 commits is about 12.6 million commits. That’s 3.9 years if you commit once every 10 seconds, but only 146 days if you commit every second. With a 4kB EEPROM, the same flash lasts about 30 times less.
Note: Never call commit() inside a fast loop. Commit only when a value really changes, group several changes into one commit, and keep your EEPROM size small. You don’t need an EEPROM.update() function on the ESP32: write() already skips bytes that didn’t change, and commit() doesn’t write anything if nothing changed.
If your data changes very often (a counter every second, a log), a battery-backed RTC memory, an external FRAM chip, or an external EEPROM chip is a better fit. For data that only has to survive deep sleep (not a power loss), use RTC memory ( RTC_DATA_ATTR), which doesn’t wear at all. See the ESP32 Sleep Modes tutorial for that.
ESP32 EEPROM vs Preferences: Which One Should You Use?
Use Preferences for new ESP32 projects, and keep EEPROM.h for code you’re porting from other Arduino boards. Both libraries store data in the same NVS partition of the flash. The difference is that EEPROM.h stores one big blob of bytes that you manage by address, while Preferences stores each value as its own named key.
| Feature | EEPROM.h | Preferences.h |
|---|---|---|
| Storage | One NVS blob (namespace and key “eeprom”) | One NVS entry per key, grouped in namespaces |
| How you address data | Byte addresses you calculate yourself | Names, like "wifi_ssid" (max 15 characters) |
| Saving | Manual commit() of the whole buffer | Each put…() is saved right away, no commit |
| Default value for missing data | No (blank byte reads 255) | Yes, getInt("key", 0) |
| RAM use | A full copy of the EEPROM size | None beyond the value you read |
| Flash wear per change | Rewrites the whole blob | Rewrites only that key |
| Max data | 6,144 bytes measured (one blob) | Up to the NVS free space, strings up to 4000 bytes each |
| Portable Arduino code | Yes, same API as AVR boards | No, ESP32 only |
| Status in core 3.3.12 | Deprecated, kept for compatibility | Recommended by Espressif |
In other words: Preferences gives you named keys, default values, no commit to forget, and less flash wear. EEPROM.h gives you the familiar Arduino API and a raw byte array, which is handy when you port code or store one fixed struct.
ESP32 Preferences Example (EEPROM Replacement, LAB 4)
Here is the same kind of job done with the Preferences library: a boot counter, a float calibration factor, and a device name String, each stored under its own key in the "app" namespace. Notice there are no addresses, no sizes, and no commit.
Wiring: none, just the ESP32 board on USB.
Here is The Application Code For This Example (ESP32_Preferences_Example_LAB4.ino)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* * LAB Name: ESP32 Preferences Library (EEPROM Replacement) Example * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com */ #include <Preferences.h> Preferences prefs; void setup() { Serial.begin(115200); delay(1000); prefs.begin("app", false); // Namespace "app", read/write mode // get...() returns the default value when the key doesn't exist yet uint32_t bootCount = prefs.getUInt("boots", 0) + 1; float calFactor = prefs.getFloat("cal", 1.000f); String devName = prefs.getString("name", "ESP32-Node"); Serial.printf("Boot #%lu, cal=%.3f, name=%s\n", (unsigned long)bootCount, calFactor, devName.c_str()); prefs.putUInt("boots", bootCount); // Saved to flash right away, no commit() prefs.putFloat("cal", 1.024f); prefs.putString("name", "Bench-ESP32"); Serial.printf("Free NVS entries: %u\n", prefs.freeEntries()); prefs.end(); } void loop() { } |
ESP32 Preferences Example Code Explanation
prefs.begin("app", false) opens the "app" namespace in read/write mode. Each get…() function takes a key and a default value, which it returns when the key doesn’t exist yet, so the first boot needs no special case. Each put…() saves its value to flash immediately. freeEntries() tells you how many 32-byte NVS entries are still free, and prefs.end() closes the namespace.
Here is the serial monitor output on my ESP32 board after a few resets. The boot count keeps going up, and the float and String saved on the previous boot come back from flash. These three keys and their namespace took only 7 NVS entries (548 free before, 541 after).

How to Clear the ESP32 EEPROM
To clear the ESP32 EEPROM, write 0xFF (or 0) to every address and call commit() once. That keeps the EEPROM size and puts every byte back to its blank value:
|
1 2 3 4 |
for (int i = 0; i < EEPROM.length(); i++) { EEPROM.write(i, 0xFF); } EEPROM.commit(); |
To delete the EEPROM blob completely (and free its space in NVS), clear the "eeprom" namespace with the Preferences library: prefs.begin("eeprom"); prefs.clear(); prefs.end();, which is what LAB 3 does. To wipe everything, including WiFi data and all Preferences, enable Tools > Erase All Flash Before Sketch Upload in the Arduino IDE for one upload. A normal upload doesn’t erase the NVS partition, so your EEPROM data survives a new sketch upload.
ESP32 EEPROM Troubleshooting (Not Saving, Wrong Values)
ESP32 EEPROM Not Saving After Reset
You didn’t call EEPROM.commit() after writing, or the ESP32 reset before the commit. write() and put() only change the RAM copy. Also check that commit() returns true: it returns false when NVS is full.
ESP32 EEPROM.read() Returns 255
That address has never been written. The ESP32 EEPROM library fills new space with 0xFF (255), not 0. Check for 0xFF (or use a magic number, as in LAB 2) and write a default value on the first boot.
EEPROM.begin() Returns False
NVS doesn’t have enough free space for the size you asked for. Reduce EEPROM_SIZE, clear old data you don’t need, or use a partition table with a larger NVS partition. Run LAB 3 to see the real limit on your board.
Garbage Values From EEPROM.get() or readFloat()
Your items overlap. A float or an int takes 4 bytes, a double 8, a String its length + 1, and a struct sizeof() bytes. Calculate each address from the previous one (LAB 2), and make sure the last item ends before EEPROM_SIZE.
Data Lost After Changing EEPROM_SIZE
Calling EEPROM.begin() with a smaller size truncates the stored blob, and the bytes past the new size are lost. A larger size keeps the old data and fills the new bytes with 0xFF. Only increase the size in a sketch that already has data in the field.
EEPROM.writeString(address, &myString) Doesn’t Compile
Pass the String itself or a C string, not its address: EEPROM.writeString(address, myString) or EEPROM.writeString(address, myString.c_str()).
ESP32-S3, ESP32-C3 & ESP32-C6 EEPROM
The EEPROM library works the same way on the ESP32-S3, ESP32-S2, ESP32-C3, and ESP32-C6. None of them has a real EEPROM, and all of them use the same NVS-based EEPROM.h and Preferences libraries in Arduino core 3.3.12. Their default 4MB partition tables also give NVS the same 20kB, so the size limit and the code in this tutorial apply to them too (they compile for those boards without any change).
Wrap Up
To conclude this tutorial, we’ll highlight the fact that the ESP32 does not have an EEPROM, and the library called EEPROM.h is a wrapper that keeps a RAM copy of your data and saves it as one NVS blob in the flash memory. Always call commit(), check the return values, expect 0xFF in blank bytes, and keep your commits rare. For new projects, the Preferences library does the same job with named keys and less flash wear.
If you need to store larger data, check out the ESP32 Flash Memory tutorial. If you’ve got any questions or suggestions, drop me a comment below and I’ll be glad to help! And check out the ESP32 Programming Tutorials series for more ESP32 tutorials.
Download ESP32 EEPROM Project Files
Download all attachment files for this Article/Tutorial (project files, schematics, code, etc.) using the link below. Everything is tested and verified to work on real hardware, as you’ve seen in the demo videos & screenshots. Please consider supporting my work if it helped you.
ESP32 EEPROM FAQ
No. The ESP32 has no internal EEPROM. The Arduino EEPROM.h library emulates one in the external SPI flash, inside the NVS (Non-Volatile Storage) partition, so the data still survives resets and power loss. Espressif recommends the Preferences library for new projects.
The emulated EEPROM is stored as one NVS blob (namespace and key “eeprom”) in the 20 KB nvs partition at flash address 0x9000 in the default partition table. While your sketch runs, a full copy of it is kept in RAM until you call commit().
With Arduino-ESP32 core 3.3.12 and the default 20 KB NVS partition, the largest EEPROM that worked on my ESP32 DevKit V1 was 6,144 bytes. The NVS blob limit is 15,988 bytes, and the real number depends on what else is stored in NVS. It is not limited to 512 bytes or 4 KB.
The ESP32’s SPI flash is typically rated for about 100,000 erase cycles per 4 KB sector, and NVS spreads the writes over its pages. With a small EEPROM that’s millions of commits, but committing every second in a loop can still wear the flash out in months. Commit only when data changes.
Use the Preferences library (recommended) or the EEPROM library. Both store data in the NVS partition of the flash, so it survives resets, power loss, and new sketch uploads. With EEPROM.h, always call EEPROM.commit() after writing.
Dear Khaled Magdy,
At the age of 69 I started learning ESP32. I had a big issue of EEPROM data saving. This tutorial guide me perfectly. I wrote the code & its working.
My project includes Nextion Display + ESP32 + Modbus Sensor.
I will share details about my project for your information.
Kindly give details about FreeRTOS for ESP32.
Thanks for nice information.