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

Flash Read and Write Experiment


Flash Read and Write Experiment1. Flash Read and Write Overview2. Core Concepts2.1 NVS and Preferences2.2 NVS Supported Data Types2.3 Namespaces and Key-Value Pairs2.4 Delete with Shift-Forward Algorithm3. Hardware Dependencies4. Project Architecture and Flow5. Source Code5.1 Initialization and Boot Read5.2 Append Write5.3 Delete with Shift-Forward Algorithm5.4 Traverse Read5.5 Clear All Data6. Operation Flow6.1 Compile and Flash6.2 Normal Phenomena6.3 Verification Methods7. Common Issues


1. Flash Read and Write Overview

ESP32's internal SPI Flash can not only store firmware but also be partitioned for user data storage. This experiment uses Arduino's Preferences library (a high-level wrapper for ESP-IDF NVS) to implement CRUD operations on key-value pairs: append write, traverse read, delete by index (with shift-forward algorithm), and one-click clear.

NVS (Non-Volatile Storage) is a key-value storage library provided by ESP-IDF, based on a dedicated partition of SPI Flash. NVS encapsulates wear-leveling and power-failure protection on Flash, allowing developers to read and write persistent data like using a "dictionary" without worrying about the underlying erase/write details.

Typical Application Scenarios:

Application TypeExamples
Configuration StorageWi-Fi SSID/password, device name, calibration parameters
Status RecordingDevice running time, last shutdown state, error log count
User DataPreset parameter tables, historical records, offline cache
Firmware ManagementOTA upgrade flags, firmware version number

2. Core Concepts

2.1 NVS and Preferences

NVS (Non-Volatile Storage) is a lightweight key-value storage library implemented by ESP-IDF on SPI Flash. Preferences is Arduino's wrapper for it:

xxxxxxxxxx
Arduino Preferences → ESP-IDF NVS → SPI Flash partition → Wear-Leveling (WL) protection

2.2 NVS Supported Data Types

TypeFunctionsSize
Integernvs_set_i8() / nvs_set_i16() / nvs_set_i32() / nvs_set_i64()1~8 bytes
Stringnvs_set_str()Variable, ≤ 1984 bytes
Binary Blobnvs_set_blob()Variable, ≤ 1984 bytes

2.3 Namespaces and Key-Value Pairs

  • Namespace: Equivalent to a folder; data in different namespaces does not interfere with each other. This experiment uses "data_store"

  • Key-Value Pair: Each storage item consists of a key (string) and a value (int/float/string/bytes)

  • begin(namespace, readonly) mount → operate → end() commit and release

2.4 Delete with Shift-Forward Algorithm

NVS itself only supports remove(key) deletion by key and cannot automatically "fill gaps". This experiment implements a shift-forward strategy: suppose dat3 is deleted, dat4→dat3, dat5→dat4... finally delete the extra datN, decrement counter by 1, ensuring key names are always consecutively numbered from 1.


3. Hardware Dependencies

Pure software experiment, no external hardware required. Only USB cable needed to view serial interaction menu.

ESP32-S3 NVS Hardware Features:

FeatureParameter
Storage mediumInternal SPI Flash (partitioned via partition table)
Maximum key countLimited by Flash partition size (typically hundreds to thousands)
Key name lengthUp to 15 characters
Value typesint8/16/32/64, float/double, string, blob (binary)
Maximum string length4000 bytes (limited by NVS page size)
Erase/write cyclesAbout 100,000 times/sector (wear-leveling algorithm distributes writes evenly)
Power-failure protectionYes (end() calls nvs_commit for atomic write, power failure won't corrupt existing data)
Write granularityCached per namespace, end() commits all at once

4. Project Architecture and Flow

xxxxxxxxxx
setup()
  ├─ Serial.begin(115200)
  ├─ Read saved dataCount in read-only mode
  └─ Print interaction menu (1=Write 2=Read 3=Delete 4=Clear)

loop()
  └─ if Serial.available():
       ├─ cmd=1 → Wait for user input → putString("datN", content) → dataCount++
       ├─ cmd=2 → Iterate i=1..count → getString("datN") → Print
       ├─ cmd=3 → Shift-forward algorithm → remove(datN) → dataCount--
       └─ cmd=4 → clear() → dataCount=0

5. Source Code

Source Code Location: Source code → Arduino → 2.Serial communication → 4.FLASH → 4.FLASH.ino

5.1 Initialization and Boot Read

Read-only mode quickly reads the saved entry count without holding a write lock.

xxxxxxxxxx
#include <Preferences.h>
Preferences prefs;
#define STORAGE_NAMESPACE "data_store"
int dataCount = 0;

void setup() {
  Serial.begin(115200);

  prefs.begin(STORAGE_NAMESPACE, true);      // Read-only mode
  dataCount = prefs.getInt("count", 0);    // Returns default 0 if key doesn't exist
  prefs.end();

  Serial.println("1=Write 2=Read 3=Delete 4=Clear");
}

The second parameter 0 in getInt("count", 0) is the default value — returns 0 instead of error when the key doesn't exist.

5.2 Append Write

Open in read-write mode, generate key name datN, write content and update counter.

xxxxxxxxxx
if (cmd == "1") {
  // Wait for user input content...
  prefs.begin(STORAGE_NAMESPACE, false);     // Read-write mode
  dataCount++;
  char key[16];
  sprintf(key, "dat%d", dataCount);          // Key name: "dat1", "dat2"...
  prefs.putString(key, content);
  prefs.putInt("count", dataCount);          // Update counter
  prefs.end();                               // Commit write
}

prefs.end() actually commits the write to Flash. Power failure before end() loses this write but doesn't damage existing data.

Important note for append write:

Serial monitor send settings must select No line ending.

If newline or carriage return is used as message terminator, pressing enter automatically appends \r\n characters;

If only enter is pressed without valid text input, the program reads empty content containing only newline characters, ultimately writing invalid empty data to Preferences.

Do not select line ending

5.3 Delete with Shift-Forward Algorithm

After deleting datDelNum, shift subsequent data forward to maintain consecutive key names.

xxxxxxxxxx
for (int i = delNum; i < total; i++) {
  char srcKey[16], dstKey[16];
  sprintf(srcKey, "dat%d", i + 1);           // Source key (next one)
  sprintf(dstKey, "dat%d", i);               // Destination key (current position)
  String nextData = prefs.getString(srcKey, "");
  prefs.putString(dstKey, nextData);         // Shift forward
}
sprintf(key, "dat%d", total);
prefs.remove(key);                           // Delete the extra last entry
dataCount = total - 1;
prefs.putInt("count", dataCount);

Why is the shift-forward algorithm needed? prefs.remove(key) only deletes a single entry. After deletion, key names will have gaps (e.g., dat1→dat2→dat4). If subsequent append writes only rely on dataCount auto-increment, dat3's position will always be empty. The shift-forward algorithm ensures consecutive key names and, combined with append write logic, prevents "ghost holes".

5.4 Traverse Read

Open in read-only mode, loop through all datN keys and print. The second parameter "" in getString(key, "") is the default value when the key doesn't exist.

xxxxxxxxxx
else if (cmd == "2") {
  prefs.begin(STORAGE_NAMESPACE, true);         // Read-only mode
  int total = prefs.getInt("count", 0);

  if (total == 0) {
    Serial.println("No data");
  } else {
    for (int i = 1; i <= total; i++) {
      char key[16];
      sprintf(key, "dat%d", i);                  // Construct key name "dat1", "dat2"...
      String data = prefs.getString(key, "");    // Returns empty string "" if not exists
      Serial.printf("Entry %d: %s\r\n", i, data.c_str());
    }
  }
  prefs.end();
}

Call prefs.end() for each read operation as well. Although read-only mode doesn't hold a write lock, timely resource release is good programming practice.

5.5 Clear All Data

prefs.clear() clears all key-value pairs in the current namespace at once, much faster than remove() one by one.

xxxxxxxxxx
else if (cmd == "4") {
  prefs.begin(STORAGE_NAMESPACE, false);         // Read-write mode
  prefs.clear();                                 // Clear all key-value pairs in this namespace
  dataCount = 0;                                 // Reset counter to zero
  prefs.end();
  Serial.println("All data cleared!");
}

clear() only clears the current namespace "data_store" and does not affect other namespaces. The underlying operation marks old NVS pages as erased and allocates new pages — the entire namespace's data is one batch operation, more than 10 times faster than remove() one by one. Note don't forget to synchronously reset dataCount in memory, otherwise the counter will be inconsistent with actual storage.


6. Operation Flow

6.1 Compile and Flash

Arduino Flash Process: See 1. Before Development → 2. Build and Flash.

  1. Open 4.FLASH.ino

  2. Open Serial Monitor (115200 bps), set line ending to "Newline"

6.2 Normal Phenomena

Interact via commands in the serial monitor:

x
==================================
   ESP32-S3 Flash Storage 
 1 = Append Write  (追加写入)
 2 = Read All Data (读取全部)
 3 = Delete Specified Data (删除指定)
 4 = Clear All Data (清空全部)
==================================
Enter content to save:
Entry 1 saved successfully
Enter content to save:
Entry 2 saved successfully
All data:
Entry 1: Hello World!
Entry 2: ESP32-S3 NVS Demo
Enter entry number to delete:
Entry 1 deleted
All data:
Entry 1: ESP32-S3 NVS Demo
All data cleared!
No data

Serial print

Before power off:

Before power off

After power cycle reconnect:

After reconnect

6.3 Verification Methods

OperationExpected Result
Write, power cycle, restart, readData still present (power-failure safe)
Write 5 entries, delete #3, readConsecutive key names: dat1~dat4
Input 4, restart, readShows "No data"

7. Common Issues

PhenomenonCauseSolution
Data lost after restartprefs.end() not calledMust call end() after each write
Write fails/hangsOpened for write in read-only modeSecond parameter of begin(ns, false) must be false
Key not found but data existsCounter inconsistent with actual storageCheck if delete logic correctly updates count
Compilation error Preferences.h not foundESP32 board package not installedInstall ESP32 Arduino Core in IDE
SPI Bus Communication
USB Emulated Mass 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
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文