SunFounder ESP32 Starter Kit
Note: If you see the following information.
E (528) vfs_fat_sdmmc: mount_to_vfs failed (0xffffffff).
Failed to mount SD card
First, check if your SD card is properly inserted into the expansion board.
If it is inserted correctly, there might be an issue with your SD card. You can try using an eraser to
clean the metal contacts.
If the problem persists, it is recommended to format the SD card, please refer to How to format the
SD card?.
How it works?
The purpose of this project is to demonstrate the usage of the SD card with the ESP32 board. The ESP32’s built-in
SDMMC host peripheral is used to connect with the SD card.
The project begins by initializing the serial communication and then attempts to mount the SD card. If the SD card
fails to mount successfully, the program will print an error message and exit the setup function.
Once the SD card is mounted successfully, the program proceeds to create a file named “test.txt” in the root directory
of the SD card. If the file is successfully opened in write mode, the program writes a line of text - “Hello, world!” to
the file. The program will print a success message if the write operation is successful, otherwise, an error message will
be printed.
After the writing operation is complete, the program closes the file and then opens the root directory of the SD card. It
then begins to loop through all the files in the root directory, printing the filename and filesize of each file found.
In the main loop function, there are no operations. This project focuses on SD card operations such as mounting the
card, creating a file, writing to a file, and reading the file directory, all of which are executed in the setup function.
This project serves as a useful introduction to handling SD cards with the ESP32, which can be crucial in applications
that require data logging or storage.
Here’s an analysis of the code:
1. Include the SD_MMC library, which is needed to work with SD cards using ESP32’s built-in SDMMC host pe-
ripheral.
#include "SD_MMC.h"
2. Inside the setup() function, the following tasks are performed.
• Initialize the SD card
Initialize and mount the SD card. If the SD card fails to mount, it will print “Failed to mount SD card”
to the serial monitor and stop the execution.
if(!SD_MMC.begin()) { // Attempt to mount the SD card
Serial.println("Failed to mount card"); // If mount fails, print to␣
˓→serial and exit setup
return;
}
• Open the file
Open a file named "test.txt" located in the root directory of the SD card in write mode. If the file
fails to open, it prints “Failed to open file for writing” and returns.
1.41. 7.4 SD Card Write and Read 153