Using LittleFS for File Operations on ESP32 with PlatformIO

Ibrahim Bin Mansur
4 min readOct 17, 2024

--

LittleFS (Little File System) is a lightweight filesystem designed for microcontrollers with a small amount of flash memory. In this guide, we’ll explore how to use LittleFS on an ESP32 microcontroller for basic file operations such as creating files, appending data, and deleting files. We’ll be using PlatformIO as our development environment.

Link

Setting up the Project

First, create a new PlatformIO project for ESP32. In your platformio.ini file, add the following configuration:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
littlefs

build_flags =
-DCONFIG_LITTLEFS_FOR_IDF_3_2

This configuration sets up the project for an ESP32 board, includes the LittleFS library, and adds a necessary build flag for compatibility.

Code Example

Here’s a complete example that demonstrates creating a file, appending to it, reading its contents, and then deleting it:

#include <Arduino.h>
#include <LittleFS.h>

void setup() {
Serial.begin(115200);

// Initialize LittleFS
if (!LittleFS.begin(true)) {
Serial.println("An error occurred while mounting LittleFS");
return;
}

// Create and open a file for writing
File file = LittleFS.open("/example.txt", "w");
if (!file) {
Serial.println("Failed to open file for writing");
return;
}

// Write to the file
if (file.println("Hello, ESP32!")) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();

// Append to the file
file = LittleFS.open("/example.txt", "a");
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
if (file.println("This is an appended line.")) {
Serial.println("File appended");
} else {
Serial.println("Append failed");
}
file.close();

// Read the file
file = LittleFS.open("/example.txt", "r");
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
Serial.println("File content:");
while (file.available()) {
Serial.write(file.read());
}
file.close();

// Delete the file
if (LittleFS.remove("/example.txt")) {
Serial.println("File deleted");
} else {
Serial.println("Delete failed");
}
}

void loop() {
// Nothing to do here
}

After a little modification, to see if the file exists:

#include <Arduino.h>
#include <LittleFS.h>

void setup() {
Serial.begin(115200);

// Initialize LittleFS
if (!LittleFS.begin(true)) {
Serial.println("An error occurred while mounting LittleFS");
return;
}

// Create and open a file for writing
File file = LittleFS.open("/example.txt", "w");
if (!file) {
Serial.println("Failed to open file for writing");
return;
}

// Write to the file
if (file.println("Hello, ESP32!")) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();

// Append to the file
file = LittleFS.open("/example.txt", "a");
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
if (file.println("This is an appended line.")) {
Serial.println("File appended");
} else {
Serial.println("Append failed");
}
file.close();

// Read the file
file = LittleFS.open("/example.txt", "r");
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
Serial.println("File content:");
while (file.available()) {
Serial.write(file.read());
}

file.close();
if (LittleFS.exists("/example.txt")){
Serial.println("File exists");
} else {
Serial.println("File does not exist");
}

// Delete the file
if (LittleFS.remove("/example.txt")) {
Serial.println("File deleted");
} else {
Serial.println("Delete failed");
}

if (LittleFS.exists("/example.txt")){
Serial.println("File exists");
} else {
Serial.println("File does not exist");
}
}

void loop() {
// Nothing to do here
}

Explanation of the Code

  1. Initialization: We start by initializing the serial communication and mounting the LittleFS filesystem.
  2. Creating a File: We use LittleFS.open() with the "w" mode to create a new file or open an existing one for writing. We then write a line to the file using file.println().
  3. Appending to a File: To append to the file, we open it again using the “a” mode. This allows us to add content to the end of the file without overwriting existing content.
  4. Reading a File: We open the file in “r” mode for reading. We then use a while loop to read the file contents byte by byte and print them to the serial monitor.
  5. Deleting a File: Finally, we use LittleFS.remove() to delete the file from the filesystem.

Best Practices

  • Always check if file operations are successful before proceeding.
  • Close files after you’re done with them to free up system resources.
  • Use appropriate error handling to make your code more robust.
  • Be mindful of the limited flash wear cycles; avoid excessive writes to the same file.

Conclusion

LittleFS provides a simple and efficient way to work with files on microcontrollers like the ESP32. This example demonstrates basic file operations, but LittleFS is capable of more advanced features as well, such as directory support and wear leveling to extend the life of your flash memory.

Remember to properly initialize LittleFS in your projects and handle potential errors to ensure your file operations are reliable and your ESP32 application runs smoothly.

References

--

--

No responses yet