How to Upload Images or Files to the ESP32 Using LittleFS

The ESP32 is a versatile microcontroller that allows storing and accessing files in its flash memory, such as images or data files. To achieve this, it is necessary to use a compatible file system, such as SPIFFS or LittleFS. Below, I will document the exact process I followed to properly configure my ESP32 and avoid issues in the future.

ESP32 and Partition Configuration

In the Arduino IDE
Version: 2.3.4-nightly-20241114 Date: 2024-11-14T03:17:02.899Z CLI Version: 1.0.4 Copyright © 2024 Arduino SA

The available options in Tools > Partition Scheme are the following:

  • Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS)
  • Default 4MB with ffat (1.2MB APP/1.5MB FATFS)
  • 8M with spiffs (3MB APP/1.5MB SPIFFS)
  • Minimal (1.3MB APP/700KB SPIFFS)
  • No OTA (2MB APP/2MB SPIFFS)
  • No OTA (1MB APP/3MB SPIFFS)
  • No OTA (2MB APP/2MB FATFS)
  • No OTA (1MB APP/3MB FATFS)
  • Huge APP (3MB No OTA/1MB SPIFFS)
  • Minimal SPIFFS (1.9MB APP with OTA/190KB SPIFFS)
  • 16M Flash (2MB APP/12.5MB FATFS)
  • RainMaker 4MB
  • RainMaker 4MB No OTA
  • Custom

Today, November 19, 2024, I selected:

Configuration Used:

  • Partition option: Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS)

Executed Commands

1.- Create the file system file (LittleFS):

mklittlefs -c data -p 256 -b 4096 -s 1441792 littlefs.bin
  • -c data: Folder containing the files (e.g., image.jpg, logo.jpg).
  • -p 256: Page size.
  • -b 4096: Block size.
  • -s 1441792: Total file system size.

2.- Verify the ESP32 chip and flash memory:

esptool.py --chip esp32 --port /dev/cu.usbserial-0001 flash_id

This command allowed me to confirm that the flash memory was properly configured.

3.- Upload the LittleFS file to the ESP32:

esptool.py --chip esp32 --port /dev/cu.usbserial-0001 write_flash 0x290000 littlefs.bin

Partition File Used

The file that worked with these settings was default.csv, and here is its content:

# Name,   Type, SubType, Offset,  Size, Flags
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x140000,
app1, app, ota_1, 0x150000,0x140000,
spiffs, data, spiffs, 0x290000,0x160000,
coredump, data, coredump,0x3F0000,0x10000,
  • spiffs: Allocated space for the file system, starts at address 0x290000 and has a size of 1.5MB (0x160000).
  • This configuration is preset in the Default 4MB with spiffs option.

Mounting LittleFS in Code

Instead of using SPIFFS, I chose LittleFS. Here is the code snippet used to mount the file system and handle errors:

if (!LittleFS.begin()) {
    Serial.println("LittleFS mount failed. Formatting...");
    if (LittleFS.format()) {
        Serial.println("LittleFS successfully formatted.");
    } else {
        Serial.println("Error formatting LittleFS.");
    }
    if (!LittleFS.begin()) {
        Serial.println("LittleFS could not be mounted after formatting.");
    } else {
        Serial.println("LittleFS successfully mounted after formatting.");
    }
} else {
    Serial.println("LittleFS successfully mounted.");
}

Important Considerations

  1. Avoid using SPIFFS: Although SPIFFS is still supported, LittleFS is more efficient and modern, especially if you’re handling larger files or performing many write and read operations.
  2. Compatibility with the partition scheme: Make sure the selected partition scheme meets your storage needs. In this case, 1.5MB for LittleFS was enough for my images (logo.jpg, image.jpg).
  3. Steps to upload files:
    • Always recreate the file system (e.g., littlefs.bin) after making changes to the data folder files.
    • Use the commands described above to upload the file system to the ESP32.
  4. Serial Monitor for Debugging: Use the Serial Monitor to confirm that LittleFS is mounted correctly and that the files are available:
LittleFS Mounted Successfully Files in LittleFS: File: /logo.jpg File: /image.jpg

Conclusion

With this configuration and the detailed steps, I successfully got the ESP32 to load images from LittleFS in my project. This blog serves as a reference to avoid common mistakes in the future and as a guide for other developers facing similar challenges.