ESP32 Filesystem and Flash Storage
Now we move to local storage inside the ESP32.
So far, we've stored data in variables:
int temperature = 30;
But when the ESP32 restarts, that variable normally disappears.
With flash storage, we can save data so that it survives reset and power loss.
ESP32 │ ├── RAM │ └── Temporary data │ └── Flash └── Persistent data
1. RAM vs Flash
RAM
RAM is temporary.
ESP32 ON ↓ RAM contains data ↓ ESP32 OFF ↓ Data lost
Example:
int count = 10;
After restarting, count is initialized again.
Flash
Flash is non-volatile.
ESP32 ON ↓ Save data ↓ ESP32 OFF ↓ ESP32 ON ↓ Data still available
This is useful for:
- Wi-Fi configuration
- Device settings
- Sensor calibration values
- Counters
- Web-server files
- User preferences
- Small logs
2. ESP32 Flash Memory
The ESP32 has flash memory connected to the chip.
Conceptually:
ESP32 ┌──────────────┐ │ CPU │ │ RAM │ │ GPIO │ │ Wi-Fi │ │ Bluetooth │ └──────┬───────┘ │ Flash Memory ┌──────────────┐ │ Program │ │ Filesystem │ │ Data │ └──────────────┘
The exact usable flash size and partition layout depend on the ESP32 board/module and partition scheme.
3. What is a Filesystem?
A filesystem organizes files inside storage.
Your computer has:
C:/ ├── Users ├── Program Files └── Windows
Similarly, ESP32 can have:
/ ├── index.html ├── style.css ├── config.json └── data.txt
This is extremely useful for an ESP32 web server.
4. LittleFS
LittleFS is a filesystem commonly used on ESP32 for storing files in flash.
For example:
LittleFS │ ├── index.html ├── style.css ├── script.js └── config.json
Your ESP32 can read these files when needed.
5. SPIFFS
Another filesystem you may encounter is:
SPIFFS = SPI Flash File System
Older ESP32 projects frequently use SPIFFS.
For newer projects, LittleFS is generally a good choice when supported by your board/core setup.
Conceptually:
LittleFS ↓ Flash ↓ Files
6. LittleFS Example
Include:
#include "LittleFS.h"
Initialize:
if (!LittleFS.begin(true)) { Serial.println("LittleFS Mount Failed"); return; }
Now ESP32 can access its filesystem.
7. Creating a File
You can open a file for writing:
File file = LittleFS.open("/data.txt", "w"); file.println("Hello ESP32"); file.close();
The file:
/data.txt
is now stored in flash.
8. Reading a File
File file = LittleFS.open("/data.txt", "r"); if (file) { String data = file.readString(); Serial.println(data); file.close(); }
Output:
Hello ESP32
9. File Modes
Common modes include:
"r" → Read "w" → Write "a" → Append
Example:
LittleFS.open("/data.txt", "r");
means read.
LittleFS.open("/data.txt", "w");
means write/replace.
LittleFS.open("/data.txt", "a");
means append.
10. Writing Multiple Values
For example:
File file = LittleFS.open("/sensor.txt", "w"); file.println("Temperature: 29.5"); file.println("Humidity: 62"); file.println("Motion: YES"); file.close();
Stored data:
Temperature: 29.5 Humidity: 62 Motion: YES
11. JSON Configuration
Instead of storing random text, you can store structured configuration.
Example:
{ "deviceName": "ESP32-Home", "light": true, "fan": false, "temperatureLimit": 30 }
File:
/config.json
This is very useful for IoT projects.
12. Why Store Configuration?
Suppose your ESP32 has:
Device Name Wi-Fi Settings Temperature Limit Light State Fan State
Instead of hardcoding everything:
const char* deviceName = "ESP32-Home";
you can store settings in flash.
/config.json
Then:
ESP32 starts ↓ Read config.json ↓ Load settings ↓ Start system
13. ESP32 Web Server + LittleFS
This is one of the most useful combinations.
Instead of putting HTML directly inside:
server.send(200, "text/html", "...");
you can store:
data/ ├── index.html ├── style.css └── script.js
inside the ESP32 filesystem.
Then:
Browser │ │ HTTP ▼ ESP32 Web Server │ ▼ LittleFS │ ├── index.html ├── style.css └── script.js
14. Example Web Files
index.html
<!DOCTYPE html> <html> <head> <title>ESP32</title> <link rel="stylesheet" href="/style.css"> </head> <body> <h1>ESP32 Smart Home</h1> <button>LIGHT ON</button> </body> </html>
style.css
body { font-family: Arial; text-align: center; } button { padding: 15px; }
Now your ESP32 serves actual files rather than a huge HTML string inside the .ino file.
15. Flash-Based Web Server
Architecture:
ESP32 ┌───────────────┐ │ Web Server │ │ │ Browser │ ↕ │ ─────┤ LittleFS │ │ │ │ │ index.html │ │ style.css │ │ script.js │ └───────────────┘
This is a very clean way to build an ESP32 web interface.
16. Preferences / NVS
ESP32 also provides a convenient way to store small persistent values.
A commonly used Arduino-ESP32 API is:
#include <Preferences.h> Preferences preferences;
Start a namespace:
preferences.begin("settings", false);
Save a value:
preferences.putInt("count", 25);
Read it:
int count = preferences.getInt("count", 0);
Close:
preferences.end();
17. What is NVS?
NVS = Non-Volatile Storage
It is designed for storing key/value data in flash.
Think of it like:
settings │ ├── count = 25 ├── light = true ├── fan = false └── name = "Home"
It is very convenient for small configuration values.
18. Preferences vs LittleFS
| Feature | Preferences/NVS | LittleFS |
|---|---|---|
| Small settings | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ |
| Key/value data | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ |
| Files | ❌ | ⭐⭐⭐⭐⭐ |
| HTML/CSS/JS | ❌ | ⭐⭐⭐⭐⭐ |
| Configuration | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| Large structured files | Not ideal | Better |
| Web server files | ❌ | ⭐⭐⭐⭐⭐ |
Simple rule:
Small settings → Preferences Files → LittleFS
19. EEPROM Concept
You may also hear about:
EEPROM
EEPROM traditionally refers to non-volatile memory that can store small amounts of data.
On ESP32, however, many projects use NVS/Preferences instead of treating EEPROM like on classic Arduino boards.
For ESP32:
Arduino EEPROM concept ↓ ESP32 ↓ Preferences / NVS
is often the better approach for small persistent values.
20. Example: Saving a Counter
Suppose your ESP32 counts people entering a room.
You have:
int peopleCount = 10;
If power is lost:
10 ↓ Power OFF ↓ Restart ↓ Count may reset
Instead:
preferences.putInt("people", peopleCount);
When ESP32 starts:
peopleCount = preferences.getInt("people", 0);
Now:
People = 10 ↓ Power OFF ↓ Power ON ↓ People = 10
This is very useful for your people-counting project.
21. Flash Wear
There is an important warning:
Don't continuously write to flash unnecessarily.
Bad idea:
void loop() { preferences.putInt("count", count); }
If loop() runs hundreds or thousands of times, you're repeatedly writing to flash.
Better:
Value changes ↓ Save only when necessary
For example:
People count changed ↓ Save new value
rather than:
Every 10 ms ↓ Write flash
Flash has a finite write endurance.
22. Sensor Data Logging
You could create:
/log.txt
and store:
10:00,29.2,60 10:01,29.3,61 10:02,29.5,61
Architecture:
DHT22 ↓ ESP32 ↓ LittleFS ↓ /log.txt
Later you could read the file through a web interface.
23. Local Data Logger
A complete local data logger could work like:
DHT22 │ ▼ ESP32 │ ┌─────┴─────┐ │ │ RAM LittleFS │ ▼ data.csv
The file could contain:
Time,Temperature,Humidity 10:00,29.2,60 10:01,29.3,61 10:02,29.5,61
Then your browser could download or visualize it.
24. LittleFS File Operations
Useful operations include:
Check whether file exists
if (LittleFS.exists("/data.txt")) { Serial.println("File exists"); }
Delete a file
LittleFS.remove("/data.txt");
Rename
LittleFS.rename("/old.txt", "/new.txt");
List files
You can iterate through a directory to inspect stored files.
25. Flash Partition
ESP32 flash isn't simply one big folder.
A partition table determines how flash is divided.
Conceptually:
ESP32 Flash ┌─────────────────────────────┐ │ Bootloader │ ├─────────────────────────────┤ │ Partition Table │ ├─────────────────────────────┤ │ Application / Firmware │ ├─────────────────────────────┤ │ Filesystem │ ├─────────────────────────────┤ │ NVS │ └─────────────────────────────┘
The actual layout depends on the selected board and partition scheme.
26. Why Partition Size Matters
Suppose you have:
index.html style.css script.js images
and the filesystem partition is too small.
You may not be able to upload all your files.
Therefore, for large web interfaces, you may need an appropriate partition scheme.
27. Uploading Filesystem Data
When using an ESP32 web server with LittleFS, you commonly have:
Project │ ├── project.ino │ └── data ├── index.html ├── style.css └── script.js
The data directory contains files intended for the filesystem.
The exact upload method depends on your Arduino IDE/core/tooling setup.
The important concept is:
data/ ↓ Filesystem image ↓ ESP32 Flash
28. Flash Storage + Wi-Fi Credentials
You can save configuration such as:
SSID Password Device Name
instead of hardcoding it permanently.
For example:
/config ssid password deviceName
Then:
ESP32 boot ↓ Read configuration ↓ Connect Wi-Fi
This is the foundation for configurable IoT devices.
Security note: storing credentials locally is useful, but the device should still protect them from unauthorized access and avoid exposing them through a web API or debug output.
29. Flash Storage + OTA
Later, when we learn OTA:
Web / Cloud │ ▼ Firmware Update │ ▼ ESP32 Flash
The ESP32's flash is used for firmware images and other persistent data according to its partition layout.
So understanding flash now will help you understand OTA later.
30. Complete Smart Home Example
Imagine your ESP32 stores:
LittleFS │ ├── index.html ├── style.css ├── script.js └── config.json Preferences/NVS │ ├── peopleCount ├── lightState ├── fanState └── securityState
Then:
ESP32 │ ┌────────────┴────────────┐ │ │ LittleFS NVS │ │ Web Files Settings │ │ └────────────┬────────────┘ │ ▼ Smart Home
This lets you build a much more professional standalone device.
31. Standalone ESP32 System
One of the biggest advantages is that you don't always need:
ESP32 + External Server + Database
You can create:
Phone │ │ Wi-Fi ▼ ESP32 │ ├── Web Server ├── LittleFS ├── Preferences └── Sensors
For a local smart-home controller, this can work completely inside your local network.
32. Local vs Cloud Storage
Local
ESP32 ↓ LittleFS / NVS
Advantages:
- Works without Internet
- Fast
- Simple
- Good for local configuration
Limitations:
- Limited storage
- Device can be lost/damaged
- Not automatically accessible from anywhere
Cloud
ESP32 ↓ Internet ↓ Cloud
Advantages:
- Remote access
- Centralized data
- Multiple devices
- Cloud dashboards
Limitations:
- Internet dependency
- Authentication/security required
- Service configuration required
33. Which Storage Should You Use?
Use this simple decision tree:
Need to store data? │ ▼ Is it a small key/value? │ YES ↓ Preferences / NVS NO │ ▼ Is it a file? │ YES ↓ LittleFS
Examples:
Wi-Fi settings → Preferences Light state → Preferences Counter → Preferences HTML → LittleFS CSS → LittleFS JavaScript → LittleFS JSON file → LittleFS CSV log → LittleFS
⚠️ Common Mistakes
1. Forgetting to initialize LittleFS
LittleFS.begin()
must succeed before using the filesystem.
2. Wrong file path
Use:
"/data.txt"
not:
"data.txt"
when working with ESP32 filesystem APIs.
3. Writing to flash continuously
Don't write every loop cycle.
4. Filesystem too small
Large web files may exceed the allocated filesystem partition.
5. Assuming flash is unlimited
Flash storage has finite capacity and write endurance.
6. Storing passwords carelessly
Don't expose stored credentials through a public webpage, API, or GitHub repository.
🧠 Quick Summary
RAM ↓ Temporary data
Flash ↓ Persistent data
For ESP32:
Small settings ↓ Preferences / NVS
Files ↓ LittleFS
HTML/CSS/JS ↓ LittleFS
Sensor logs ↓ LittleFS
And a powerful standalone system can look like:
ESP32 ┌──────────┼──────────┐ │ │ │ Sensors Web Server Storage │ / \ │ LittleFS NVS │ ▼ Browser