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: if (!SPIFFS.begin())
{
Serial.println("Failed to mount file system");
return;
}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: 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
}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: 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");
}File.size
Returns file size, in bytes.Syntax:file.size()Code snippet: 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());
}File.close
Closes the file. No other operation will be performed on File object after the close function called.Syntax:file.close()Code Snippet: 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();
}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
/****************************************************************
* NodeMCU SPIFFS Tutorial Example
*
* Example program to read file from SPIFFS in ESP8266
* by https://nerdyelectronics.com
*
* The ESP8266 SPIFFS File System – Complete Beginner Guide:
* https://nerdyelectronics.com/iot/esp8266/esp8266-spiffs-file-system/
*
* Upload Files to ESP8266 SPIFFS:
* https://nerdyelectronics.com/iot/esp8266/upload-files-to-esp8266-spiffs/
*
***************************************************************/
#include "FS.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if (!SPIFFS.begin()) //mount the file system
{
Serial.println("Failed to mount file system");
return;
}
}
void loop() {
// put your main code here, to run repeatedly:
File configurations = SPIFFS.open("/config.txt", "r"); //open the config file
if (!configurations)
{
Serial.println("Failed to open config file");
}
else
{
Serial.println("Successfully opened config file");
Serial.print("File size = ");
Serial.println(configurations.size()); //read the file size here
while (configurations.available())
{
Serial.write(configurations.read());
}
Serial.println("");
}
configurations.close();
delay(5000);
}You can download the same program and the “config.txt” file from this link.Now, upload this program to the NodeMCU and open the Serial Monitor. You should see an output like this:

📖 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.







