Skip to content

Lesson 08: SD Card File Read and Write

1. Course Introduction

Mount the SD card file system, create hello.txt, write hello world! to it, then read it back and print the result.

2. Learning Objectives

After completing this lesson, you should be able to:

  • Understand the centralized configuration of SD_MMC/SPI pins in board_config.h.
  • Use the functions for file creation, string writing, and string reading.
  • Understand the looping verification approach of the FreeRTOS sd_task().

3. What You Need

  • Hardware: One CrowPanel Advanced 5-inch ESP32-P4 HMI AI Display development board; one FAT32-formatted MicroSD card; one USB Type-C data cable that supports data transfer.
  • The SD card must be formatted as a FAT file system; otherwise it may fail to be recognized correctly, and files on the card may fail to be read.
  • Compatibility note: The hardware and software code for the 5-inch development boards are fully interchangeable, differing only in board size; please select the appropriate model based on display size and use case.

Code reference link: https://github.com/Elecrow-RD/-CrowPanel-Advanced-5inch-ESP32-P4-HMI-AI-Display-800x480-IPS-Touch-Screen/tree/master/example/V1.0

4. Software Operation Steps

Double-click to open the Lesson 8 code (the .ino file).

Open the SD card lesson project

The SD card pin descriptions are available in board_config.h for reference.

Configure the following options accordingly.

  • Board: ESP32P4 Dev Module
  • Core Debug Level: Info
  • Flash Frequency / Mode / Size: 80MHz / QIO / 16MB (128Mb)
  • Partition Scheme: 16M Flash (3MB APP/9.9MB FATFS)
  • PSRAM: Enabled
  • USB Mode: Hardware CDC and JTAG
  • Port: After connecting the USB data cable to the board's UART0, select the newly appeared COM port under "Tools → Port".

Standard upload configuration screenshot for Lesson 1

Import the library files required by this project into the development environment by following the library import steps explained in detail in Lesson 1, to ensure that the code can correctly locate the relevant dependencies during compilation, thereby guaranteeing normal program operation.

P4_Arduino_01_Images_15

5. Hardware Operation Steps

With the power off, insert the MicroSD card into the board's SD card slot. Make sure the card is oriented correctly, with the gold contacts facing the slot's contact points; do not forcibly plug or unplug it while powered on.

Insert the MicroSD card into the board

Connect the USB-C cable that supports data transfer to the board's UART0 port and the computer, and confirm that the power indicator lights up.

Connect the board to the computer via UART0

In the Arduino IDE, select the correct board and COM port, then click Upload. After the upload completes, open the serial monitor and set the baud rate to 115200.

For instructions on how to open the serial port monitor, please refer to Lesson 1.

The serial monitor should display the SD card name, type, capacity, and speed, and sequentially output the file write and read results.

Verification of hello.txt and read/write log

Confirm that the text read back is hello world!. If initialization fails, check the card format, insertion orientation, and SDMMC pin configuration.

6. Key Code Explanation

6.1 File Read/Write Interface

write_string_file(const char *filename, const char *data)
read_string_file(const char *filename)

write_string_file() opens the requested path with the standard VFS file API, writes the supplied string, and closes the handle. read_string_file() opens the file for reading, obtains its contents, and closes it. Both paths check the file handle and report failure instead of dereferencing a null pointer.

6.2 FreeRTOS Test Task

get_sd_card_info();
write_string_file(file_hello, data);
vTaskDelay(200 / portTICK_PERIOD_MS);
read_string_file(file_hello);

sd_task() performs the sequence "card info → write → wait → read" in a fixed order. The vTaskDelay(200 / portTICK_PERIOD_MS) gives the filesystem time to finish the write before reading the same file. A longer delay at the end of the task controls the repeat rate and keeps the serial output readable.

7. Experimental Results

A /hello.txt file appears on the SD card, containing hello world!; the serial output shows the successful write, the read-back content, and test completion.

(For how to open the serial monitor, please refer to Lesson 1, where we provide a detailed explanation.)

Verification of hello.txt and read/write log

8. Frequently Asked Questions and Troubleshooting

  • If mounting fails, power off, re-insert the SD card, and check the format.
  • If the write succeeds but the read returns empty, check the path, the timing of closing the file, and the card's write-protection status.