yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
ESP32 Core Board📖
1. ESP32 Introduction
  • 1. ESP32S3 Overview
  • 2. Environment Setup Introduction
  • 3. Dev Board Introduction
2. ESP-IDF Development Tutorial
1. Before Development
  • 1. Environment Setup (Windows)
  • 2. Environment Setup (Linux)
  • 3. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. DMA
  • 8. ADC
  • 9. PWM Breathing LED
  • 10. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. PSRAM External Memory Experiment
  • 5. Flash Read and Write Experiment
  • 6. USB Emulated Mass Storage Experiment
4. FreeRTOS operating system
  • 1.FreeRTOS Core Concepts Theory
  • 2.Task Creation and Management
  • 3.Queue
  • 4.Binary Semaphore
  • 5.Mutex
  • 6.Event Groups
  • 7.Software Timer
  • 8.Task Scheduler
  • 9.Low Power Mode
  • 10.Deadlock
  • 11.Priority Inversion
5. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. Socket Programming Basics
  • 9. HTTP Client Experiment
  • 10. HTTP Server Experiment
  • 11. MQTT IoT Communication Protocol
  • 12. WIFI OTA Upgrade
  • 13. Bluetooth Fundamentals
  • 14. Bluetooth Slave Mode
  • 15. Bluetooth Master Mode
6. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
7. Xiaozhi AI Voice
  • 1. Burn Xiaozhi Firmware
  • 2. Network and Configuration
  • 3. AI Agent Settings
  • 4. Program Analysis
  • 5. Free Chat
3. Arduino Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. ADC
  • 8. PWM Breathing LED
  • 9. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication
  • 2. I2C OLED Display
  • 3. SPI Bus Communication
  • 4. Flash Read and Write
  • 5. USB Emulated Mass Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Server Experiment
  • 5. UDP Client Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT IoT Communication Protocol
  • 11. WIFI OTA Upgrade
  • 12. Bluetooth Fundamentals
  • 13. Bluetooth Slave Mode
  • 14. Bluetooth Master Mode
5. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
4. MicroPython Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
  • 3. Thonny IDE
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. TIMER_IT
  • 5. Watchdog Timer
  • 6. ADC
  • 7. PWM Breathing LED
  • 8. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. Flash Read and Write Experiment
  • 5. Print Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT
  • 11. Bluetooth Fundamentals
  • 12. Bluetooth Slave Mode
  • 13. Bluetooth Master Mode
5. Audio Applications
  • 1. I2S Audio Playback Experiment
  • 2. Repeater Experiment
Download
  • Code
  • Hardware_Info
  • 3D_Model_File
  • Software_Tools
  • VMware
  • VM_File
Download ZIP

Welcome to ESP32 Core Board repository

4. Flash Read and Write Experiment

Flash Read and Write Experiment


Flash Read and Write Experiment1. Flash Filesystem Overview2. Core Concepts2.1 MicroPython Filesystem2.2 Data Storage Format2.3 Core API3. Hardware Dependencies3.1 ESP32-S3 Hardware Features 4. Project Architecture and Flow5. Source Code Details5.1 File Initialization5.2 Append Data (Command 1)5.3 Read All (Command 2)5.4 Delete a Single Entry (Command 3)5.5 Clear (Command 4)6. Operation Procedure6.1 Flashing and Running6.2 Expected Behavior6.3 Serial Output Example6.4 Verification Method7. Common Issues


1. Flash Filesystem Overview

A filesystem partition (FAT/LittleFS by default) is allocated in the internal Flash of the ESP32-S3. MicroPython can operate on files through the standard open() / read() / write() interfaces, and the data is retained after power-off. This experiment implements interactive Flash data management: append, read, delete, and clear.

Typical application scenarios:

ApplicationExample
Configuration storageWi-Fi SSID/password, device parameters
Data loggingHistorical sensor data, operation logs
Calibration dataADC offset, calibration coefficients
Offline cacheTemporarily store data when the network is down

2. Core Concepts

2.1 MicroPython Filesystem

MicroPython automatically mounts the Flash filesystem at startup. Under the root directory /, the standard Python file API (open / read / write) can be used for reading and writing.

2.2 Data Storage Format

In this experiment, the first line stores the total record count, and each subsequent line stores one user data entry. On the first run the file does not exist; the code automatically creates it and writes 0 on the first line. After the first append, the first line is updated to the actual record count:

0                    ← First created, count is 0
Hello ESP32-S3       ← 1st entry (the first line is updated to 1 after the append)
MicroPython Flash    ← 2nd entry
...

Each operation first reads all lines into a list, then writes everything back after adding/deleting — suitable for small data volumes (< tens of KB).

2.3 Core API

​x
# Open file
f = open(f_path, "w")    # Write mode (overwrite)
f = open(f_path, "r")    # Read mode
f = open(f_path, "a")    # Append mode
​
# Read & write
content = f.read()              # Read all content
lines = content.splitlines()   # Split into lines
f.write("hello\n")              # Write one line
f.close()                       # Close (must!)

3. Hardware Dependencies

This experiment only uses the internal Flash of the ESP32-S3, no external hardware required.

3.1 ESP32-S3 Hardware Features

FeatureDescription
Total Flash capacityUsually 8MB or 16MB (query with esp32.flash_size())
Filesystem partitionThe factory MicroPython firmware mounts the remaining Flash space as FAT/LittleFS at path /
Filesystem capacityTotal Flash minus firmware/partition-table usage, usually several MB (query with os.statvfs('/'))
Write enduranceNOR Flash typically endures 100,000+ erase/write cycles; suitable for configuration/log scenarios, not for high-frequency writes every second
Power-loss protectionopen/close syncs to Flash immediately; data is not lost on power-off. However, sudden power loss during writing may corrupt the file (FAT has no journal), so it is recommended to verify critical data after writing

4. Project Architecture and Flow

xxxxxxxxxx
Script execution (single-file .py)
  │
  ├─ Initialize: check whether the file exists; if not, create it and write the first-line count "0\n"
  ├─ Print the menu: [1 Append / 2 Read / 3 Delete / 4 Clear]
  │
  └─ while True:
       ├─ input()  wait for REPL command input
       │
       ├─ "1" Append:
       │    ├─ Read all lines → parse count (first line)
       │    ├─ count++ → append a new line
       │    └─ Write everything back to the file
       │
       ├─ "2" Read:
       │    ├─ Read all lines → skip count=0 or empty
       │    └─ Print line by line
       │
       ├─ "3" Delete a single entry:
       │    ├─ Enter the index → delete that line
       │    ├─ count-1, update the first line
       │    └─ Write back
       │
       └─ "4" Clear:
            └─ Write "0\n" (reset)

5. Source Code Details

Complete source code: 5.Source Collection → MicroPython → 3.Serial Communication and Storage System → 4. FLASH → flash_fs.py

5.1 File Initialization

xxxxxxxxxx
# Create the file if it does not exist
try:
    f = open(f_path, "r")
    f.close()
except:
    f = open(f_path, "w")
    f.write("0\n")       # First line = count
    f.close()

try/except is a reliable way to check whether a file exists — some MicroPython versions do not support os.path.exists().

5.2 Append Data (Command 1)

xxxxxxxxxx
# Read the current content
f = open(f_path, "r")
lines = f.read().splitlines()
f.close()
​
# Parse and update the counter
count = 0
if len(lines) > 0:
    count = int(lines[0])
count += 1
lines[0] = str(count)
​
# Append a new line
lines.append(input("Content: "))
​
# Write all lines back
f = open(f_path, "w")
for s in lines:
    f.write(s + "\n")
f.close()
print("Saved.")

5.3 Read All (Command 2)

xxxxxxxxxx
f = open(f_path, "r")
lines = f.read().splitlines()
f.close()
​
count = int(lines[0]) if len(lines) > 0 else 0
if count == 0 or len(lines) <= 1:
    print("No data.")
else:
    print("Flash data:")
    for i in range(1, len(lines)):
        print("%d: %s" % (i, lines[i]))

5.4 Delete a Single Entry (Command 3)

xxxxxxxxxx
num = int(input("Index: "))
f = open(f_path, "r")
lines = f.read().splitlines()
f.close()
​
if num < 1 or num >= len(lines):
    print("Invalid index.")
else:
    del lines[num]                  # Delete the line
    lines[0] = str(len(lines) - 1)  # Update the count
    with open(f_path, "w") as f:
        for s in lines:
            f.write(s + "\n")
    print("Deleted.")

5.5 Clear (Command 4)

xxxxxxxxxx
f = open(f_path, "w")
f.write("0\n")
f.close()
print("Cleared.")

6. Operation Procedure

6.1 Flashing and Running

Thonny IDE operation steps: See MicroPython → 1.Development Basics → 3.Thonny IDE.

  1. Open flash_fs.py with Thonny IDE and click Run
  2. Enter commands in the REPL/Shell area to interact (number 1~4 + Enter)

6.2 Expected Behavior

After startup, the program prints the menu and waits for user input. Enter the corresponding number to perform append/read/delete/clear operations. The data is saved in the Flash filesystem and remains after a soft reset or power-off.

Flash space is usually several MB; the first-line count method in this experiment is suitable for hundreds of entries of small data. For large amounts of data storage, it is recommended to write line by line in append mode.

6.3 Serial Output Example

xxxxxxxxxx
===== Internal Flash Test =====
1 Append 2 Read 3 Delete 4 Clear
1
Content: Temperature 25.5C
Saved.
1
Content: Humidity 60%
Saved.
1
Content: ESP-IDF
Saved.
2
Flash data:
1: Temperature 25.5C
2: Humidity 60%
3: ESP-IDF
3
Index: 1
Deleted.
2
Flash data:
1: Humidity 60%
2: ESP-IDF
4
Cleared.
2
No data.

Before power-off

Stored data before power-off:

xxxxxxxxxx
===== Internal Flash Test =====
1 Append 2 Read 3 Delete 4 Clear
1
Content: Temperature 25.5C
Saved.
1
Content: Humidity 60%
Saved.
1
Content: ESP-IDF
Saved.
2
Flash data:
1: Temperature 25.5C
2: Humidity 60%
3: ESP-IDF

Before power-off

Read after power-off and restart (data not lost):

xxxxxxxxxx
===== Internal Flash Test =====
1 Append 2 Read 3 Delete 4 Clear
2
Flash data:
1: Temperature 25.5C
2: Humidity 60%
3: ESP-IDF

After power-off

6.4 Verification Method

ActionExpected result
Append 3 entriesRead back 3 entries, indices 1~3
Delete index 2Entry #3 shifts to #2
Read after clearDisplays "No data."
Soft reset & readData persists (non-volatile)
Enter an invalid indexShows "Invalid index."

7. Common Issues

SymptomCauseSolution
Data lost after soft resetFile not close()dAlways f.close() after each read/write
OSError: [Errno 28] ENOSPCInsufficient Flash spaceDelete useless files, or use os.statvfs('/') to check the remaining space
ValueError: invalid literalThe first-line count is corruptedManually delete the data file and run again
Chinese garbled textEncoding issueMicroPython defaults to UTF-8; the Thonny IDE REPL also needs to be set to UTF-8
SPI Bus Communication Experiment
Print Storage

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文