How to initialize LittleFS in Arduino on the ESP32 (PlatformIO)
In order to use the LittleFS library that comes with Arduino on the ESP32, use the following function
#include <LittleFS.h>
bool filesystemOK = false;
void InitFilesystem() {
// Initialize LittleFS
if (!LittleFS.begin(false /* false: Do not format if mount failed */)) {
Serial.println("Failed to mount LittleFS");
if (!LittleFS.begin(true /* true: format */)) {
Serial.println("Failed to format LittleFS");
} else {
Serial.println("LittleFS formatted successfully");
filesystemOK = true;
}
} else { // Initial mount success
filesystemOK = true;
}
}
Additionally, you need to configure PlatformIO to use LittleFS:
board_build.filesystem = littlefs
Now, in setup()
, call
InitFilesystem();
If you have errors including LittleFS.h
, you might need to explicitly use a recent arduino-esp32
version in platformio.ini
:
platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.5
In case you absolutely need to use an older arduino-esp32
version that doesn’t support LittleFS out of the box, you can use the lorol/LittleFS
library as outlined in our previous post: How to initialize LittleFS in PlatformIO on the ESP32 using the lorol/LittleFS library
Full platformio.ini
example:
[env:esp32dev]
platform = espressif32
platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.5
board = esp32dev
framework = arduino
board_build.filesystem = littlefs
monitor_speed = 115200