Table of Contents
KEY TAKEAWAYS
- SPIFFS provides a flat file system (no directories) on the ESP8266 flash memory
- Flash memory has limited write cycles — avoid frequent small writes to extend flash life
- Use
SPIFFS.begin()to initialize, then read/write files with standard file I/O functions - SPIFFS size is configured in the board settings and shares flash space with the program
- Internal EEPROM – 512 Bytes. You can write data 1 million times (but it has no file system).
- Flash memory – connected to ESP through SPI. In this flash memory, ESP stores the program, files, WiFi, and configurations. Limitation of this memory is it has only 10000 write cycles. Even though the file system is stored in the same flash chip as the program, programming new sketch will not modify file system contents. This allows using the file system to store sketch data, configuration files, or content for the Web server, etc..
ESP8266 SPIFFS available Functions
Let us first have a look at the various functions available for use.SPIFFS.begin
This method initializes or mounts the SPIFFS file system. We call this function before using other functions from this package. Returns true if the file system was mounted successfully, false otherwise.Syntax:SPIFFS.begin()Returns:1 – Mounted Successfully
0 – Mount unsuccessful
Code Snippet:[c] if (!SPIFFS.begin()) { Serial.println("Failed to mount file system"); return; } [/c]So, we call this function and check for the return value.SPIFFS.open
This function opens the desired file. The path should be specified starting with a slash.(eg: /data.txt).Mode is a string which indicates the file privilege. It can be:- “r”-reads the file,
- “w”-Writes to the file,
- “a”-adds or appends data to file,
- Few other modes like “r+”, “w+”, “a+”
SPIFFS.open(path,mode)Returns: File Object – If Open Successful
0 (zero) – If Open unsuccessful
Code snippet:[c] File configurations = SPIFFS.open("/config.txt", "r"); //Enter the file name if (!configurations) { Serial.println("Failed to config file"); } else { //File successfully opened //enter your logic here } [/c]SPIFFS.remove
Deletes the file specified using path. Returns true if the file is deleted Successfully.Syntax:SPIFFS.remove(path)Returns: 1 (True) – If delete Successful
0 (False) – If delete unsuccessful
Code snippet:[c] uint8_t removeFile = SPIFFS.remove("/config.txt", "r"); //Enter the file name if (!removeFile) { Serial.println("Failed to remove config file"); } else { Serial.println("config file removed successfully"); } [/c]File.size
Returns file size, in bytes.Syntax:file.size()Code snippet:[c]
File configurations= SPIFFS.open("/config.txt", "r"); //Enter the file name
if (!configurations)
{
Serial.println("Failed to open config file");
}
else
{
Serial.print("Size of config file = ");
Serial.println(configurations.size());
}
[/c]File.close
Closes the file. No other operation will be performed on File object after the close function called.Syntax:file.close()Code Snippet:[c]
File configurations= SPIFFS.open("/config.txt", "r"); //Enter the file name
if (!configurations)
{
Serial.println("Failed to open config file");
}
else
{
//Write your logic here
//Now close the file
configurations.close();
}
[/c]Now you have understood the methods available in SPIFFS and how to use them in program. In the program, we will read a file from the ESP8266 SPIFFS file system and display the content on the serial monitor.For this tutorial, I will use a text file called “config.txt“. Inside this file, I have written the following:
ESP8266 NodeMCU SPIFFS Program:
This program shows the use of the SPIFFS function calls and how to read and display the information from the config file:The working of the program is:- Mount the SPIFFS File System
- Open the config file
- Read the config file and print the information
- Close the file

📖 Related: ESP8266 – Flash The nodeMCU firmware • ESP8266 – Flash The AT commands firmware

Vivek Bhageria — Lead Firmware R&D Engineer, 12+ years. Ex-Bosch (automotive powertrain), MusicTribe (real-time audio), medical devices. M.Tech BITS Pilani. I write at NerdyElectronics — practical, register-level embedded systems for engineers who want to understand what’s actually happening under the hood.







