LittleFS is available in several of the most popular Arduino cores, including those for the ESP32, the ESP8266 and the Raspberry Pi Pico family through the arduino-pico core. Each core wraps the same upstream library in a familiar File interface, so once you know the patterns on one board they carry over to the others. This article collects those patterns: mounting, reading and writing safely, working with directories, storing structured data and uploading files from your computer.
LittleFS examples in this article use the shared API found in these cores; small differences between them are noted where they matter. Facts were checked against the ESP8266 core documentation, the upstream littlefs repository and the arduino-littlefs-upload project on 23 November 2026.
Mounting the filesystem
LittleFS must be mounted before any file can be opened. Every core offers a begin() method; on the ESP32 it accepts a flag that formats the partition automatically if mounting fails, while on the ESP8266 the core formats automatically by default.
#include <LittleFS.h>
void setup() {
Serial.begin(115200);
if (!LittleFS.begin()) {
Serial.println("LittleFS mount failed");
return;
}
Serial.println("LittleFS mounted");
}
LittleFS mount failures on a new board almost always mean the area has never been formatted, or that the board's flash layout reserves no space for a filesystem. Check the Flash Size or Partition Scheme menu before assuming a code problem.
Writing a file safely
LittleFS commits data only when a file is closed or flushed. That is the heart of its power-loss guarantee: until the commit succeeds, the previous version of the file remains intact. In practice it means you should always close files when you finish with them.
File f = LittleFS.open("/settings.txt", "w"); // "w" truncates
if (!f) {
Serial.println("open failed");
return;
}
f.println("brightness=70");
f.println("mode=auto");
f.close(); // commit
LittleFS supports the usual mode strings: "r" to read, "w" to create or truncate, and "a" to append. Opening with "w" replaces the whole file, which is exactly what you want for settings: if power fails mid-write, the old settings survive.
Appending to logs
LittleFS appends efficiently, because its file structure is designed for appending without rewriting earlier blocks. Open with "a", write one record and close.
File log = LittleFS.open("/log.csv", "a");
log.printf("%lu,%.2f\n", millis(), temperature);
log.close();
LittleFS writes at every close, so logging hundreds of times per second this way wears flash and wastes time. Buffer readings in RAM and write them in batches, or keep the file open and call flush() at intervals that reflect how much data you can afford to lose.
Reading files
LittleFS files are read with the stream functions you already know from Serial. Reading line by line is common for simple text formats.
File f = LittleFS.open("/settings.txt", "r");
while (f && f.available()) {
String line = f.readStringUntil('\n');
Serial.println(line);
}
f.close();
LittleFS also lets you check whether a path exists with LittleFS.exists(), which avoids errors when a file has not yet been created on first boot.
Working with directories
LittleFS has real directories, unlike SPIFFS. You can create them with mkdir(), remove empty ones with rmdir() and list their contents by opening the directory and iterating.
LittleFS.mkdir("/logs");
File dir = LittleFS.open("/");
File entry = dir.openNextFile();
while (entry) {
Serial.printf("%s %s %u\n", entry.isDirectory() ? "DIR " : "FILE",
entry.name(), entry.size());
entry = dir.openNextFile();
}
LittleFS directory listing on the ESP8266 core can also use the Dir object returned by LittleFS.openDir(), a style inherited from the SPIFFS API. Both approaches reach the same data.
Storing structured data
LittleFS stores bytes, so structured data needs a format. Two approaches are common.
LittleFS works well with JSON when combined with a library such as ArduinoJson, because a human-readable settings file is easy to inspect and edit. The cost is parsing time and some RAM.
LittleFS also handles binary structures, which are compact and quick. Write a struct with f.write() and read it back with f.read(). Include a version number in the struct so that future firmware can recognise and convert older layouts.
struct Settings { uint8_t version; uint8_t brightness; uint16_t interval; };
Settings s = {1, 70, 300};
File f = LittleFS.open("/settings.bin", "w");
f.write((uint8_t *)&s, sizeof(s));
f.close();
Uploading files from your computer
LittleFS images can be built from a data folder and flashed to the board, which is how web pages, certificates and default settings are usually deployed. For Arduino IDE 2, the arduino-littlefs-upload plug-in by Earle Philhower adds a command palette action that builds the image and uploads it to supported boards. For the classic IDE, separate uploader plug-ins exist for the ESP8266 and ESP32. PlatformIO builds and uploads images with its Upload Filesystem Image task once board_build.filesystem = littlefs is set.
LittleFS uploads overwrite the entire filesystem area. Any files the board created itself, such as logs, are lost when you upload a new image, so download them first if you need them.
Checking free space
LittleFS space is reported in whole blocks, typically 4 KB on SPI flash. The ESP8266 core exposes LittleFS.info(), while the ESP32 core offers LittleFS.totalBytes() and LittleFS.usedBytes(). Logging free space at boot helps you spot a filesystem that is filling with forgotten logs.
Core-by-core notes
| Core | How to start | Notes |
|---|---|---|
| ESP8266 | LittleFS.begin() |
SPIFFS is deprecated; formats automatically if no filesystem is found |
| ESP32 | LittleFS.begin(true) |
true formats on failure; partition comes from the scheme |
| RP2040 / RP2350 (arduino-pico) | LittleFS.begin() |
Filesystem size chosen in the Flash Size menu |
| Teensy | Class per memory type | See our Teensy article |
LittleFS for other Arduino architectures, such as Mbed-based boards, is available through third-party libraries listed in the Arduino Library Manager; check each library's documentation for supported boards.
Common mistakes and fixes
| Mistake | Effect | Fix |
|---|---|---|
Forgetting close() |
Data missing after reset | Close or flush every file |
Writing on every loop() |
Slow sketch, extra flash wear | Batch writes |
| Paths without a leading slash | Files not found on some cores | Use absolute paths such as /data.txt |
| Uploading an image over field data | Logs erased | Download files before uploading |
| Many tiny files | Space runs out early | Combine data into fewer files |
A robust settings pattern
LittleFS already protects the filesystem from power loss, but your application logic can still make poor choices. A simple, dependable pattern for settings is worth adopting in every sketch that stores configuration.
LittleFS settings code should start by attempting to read the file. If it is missing or fails validation, load defaults in RAM and write them out. Validation can be as simple as checking a version byte and a length, or as thorough as a checksum stored with the data. Because the filesystem only commits on close, a write interrupted by a power cut leaves the previous file in place, so the device always boots with either the old or the new settings, never a mixture.
LittleFS also makes it easy to keep a backup copy. Writing /settings.bak before replacing /settings.json costs one extra block and gives you a fallback if a firmware bug ever writes nonsense. On startup, try the main file first and the backup second.
Serving web pages from flash
LittleFS is the usual home for the HTML, CSS and JavaScript of on-device configuration pages on Wi-Fi boards. Web server libraries for the ESP32 and ESP8266 can serve files straight from the filesystem, so the page is uploaded once as part of the data image and the sketch only handles form submissions.
LittleFS works best for web assets when files are pre-compressed. Many servers will deliver a .gz version of a file with the correct content encoding, which saves both flash space and transfer time. Keep the number of files small, combine scripts where you can, and remember that each file occupies at least one block on flash.
LittleFS-hosted pages should never contain secrets, because anyone who can reach the device on the network can usually request them. Store credentials in separate files that the server does not expose.
Choosing between LittleFS and an SD card
LittleFS on internal flash suits settings, web assets and modest logs, while an SD card suits large datasets that must be read on a computer. Flash is always present and needs no extra wiring, but its capacity is limited by the partition. An SD card offers gigabytes and native PC compatibility through FAT, yet FAT lacks LittleFS's power-loss resilience. Many projects use both: LittleFS for configuration that must never be corrupted, and an SD card for bulk data that is periodically removed.
Planning an Arduino storage workflow
Before adding a filesystem to a sketch, list the files the device needs and decide which ones are supplied with the firmware and which ones are created by the device. A configuration file, a web page and a growing sensor log have different update patterns. Keep a copy of the original data folder on your computer so that a replacement board can be prepared without recovering files from a damaged device.
When a sketch starts, check the result of the mount operation before opening a file. If mounting fails, report the failure clearly over the serial console or another diagnostic channel. Automatically formatting on every failure can remove data that might otherwise be recoverable, so make formatting an explicit setup or maintenance action when the stored information matters.
For settings, validate the contents after reading them. A file may exist but contain an incomplete value, an older format or a value outside the range your application accepts. Keep sensible defaults in the firmware, include a format version when practical and make changes in a way that the application can recover from an interrupted update. Check the return value of every write and close the file after the update completes.
Finally, test the upload process separately from normal operation. Confirm the selected board, flash layout and data folder, then restart the device and read the uploaded files back. Try a missing file, a full filesystem and an unexpected reset. These checks make it easier to distinguish a filesystem problem from a mistake in the sketch or its board configuration.
Frequently Asked Questions
Which Arduino boards support LittleFS?
LittleFS is built into the ESP32, ESP8266 and arduino-pico cores and is available for Teensy through Teensyduino. Other architectures rely on third-party libraries.
Do I need to format LittleFS before use?
LittleFS needs formatting once on a blank area. The ESP8266 core does this automatically, and on the ESP32 you pass true to begin().
How do I upload a data folder in Arduino IDE 2?
LittleFS images can be uploaded in Arduino IDE 2 with the arduino-littlefs-upload plug-in, which adds an upload command to the command palette.
Is LittleFS safe if power fails while writing?
LittleFS keeps the filesystem consistent and falls back to the last committed state. Data written since the last close or flush may be lost, but existing files remain intact.
Can I use JSON files with LittleFS?
LittleFS stores any bytes, so JSON files work well, typically parsed with a library such as ArduinoJson.
Why does my LittleFS file read back empty?
LittleFS returns an empty file if the data was never committed, so check that the writing code calls close() or flush() and that the file was opened with the right mode.
Conclusion
LittleFS gives Arduino developers a dependable, directory-aware filesystem with an API they already know. Mount once, close files promptly, batch frequent writes and keep an eye on free space, and the same code will serve you across ESP32, ESP8266 and RP2040 boards. When you are ready to look under the hood, our home page explains how LittleFS stays safe during power cuts, and the ESP32 article covers partitions in depth.