5inch_P4_Micropython_02_SD_Card: Mounting the SD Card, Reading Capacity, and Traversing Files¶
1. Course Introduction¶
This lesson uses the ESP32-P4's SDMMC Slot 0 to mount a FAT/FAT32-formatted microSD card to /sd in 1-bit SDIO mode. The program prints the CLK, CMD, D0, and power pins, lists the files in the root directory, and calculates the total capacity, used space, and free space, thereby verifying the card slot power supply, SDIO communication, file system, and file read path.
2. Learning Objectives¶
- Be able to correctly insert a FAT/FAT32 microSD card while the device is powered off, and flash the 5-inch dedicated firmware.
- Be able to explain the GPIO38 power supply, the CLK/CMD/D0 on GPIO43/44/39, and the 1-bit SDIO mounting process.
- Be able to run
sd_card.pyand determine the SD card status based on the mount, file list, and capacity logs. - Be able to troubleshoot, in order, unrecognized cards, missing files, and abnormal capacity.
3. Preparation¶
-
One CrowPanel Advanced 5-inch ESP32-P4 HMI AI Display, with a screen resolution of 800 × 480 and RGB565 16-bit.
-
One USB data cable that supports data transfer; a charging-only cable cannot be used for flashing or file transfer.
-
A Windows PC and the Thonny IDE; if the latest version has flashing or runtime issues, use Thonny 4.1.7, which has been verified in the source documentation.
-
The 5-inch dedicated firmware
lvgl_micropy_ESP32_GENERIC_P4-C6_WIFI-16_ELECROW_INCH5_V1_0.bin, which must not be replaced with the 7/9/10-inch firmware. -
Before flashing, close any software that occupies the serial port; power off the device before plugging or unplugging the SD card, speaker, or other hardware.
-
One FAT or FAT32-formatted microSD card; it is recommended to preload an easily recognizable test file.
-
sd_card.pyand the 5-inch dedicated firmware from this lesson's directory.
4. Software Operation Steps¶
Flash lvgl_micropy_ESP32_GENERIC_P4-C6_WIFI-16_ELECROW_INCH5_V1_0.bin following the procedure from Lesson 1. Upon success, a boot.py file should appear in the device area.
Flashing the firmware follows the same process as the first case.
You need to flash the "
lvgl_micropy_ESP32_GENERIC_P4-C6_WIFI-16_ELECROW_INCH5_V1_0.bin" firmware onto the device.Once the flashing is successful, a "boot.py" file will appear.
Upload and open sd_card.py, then click Run; alternatively, create a new blank file, paste the complete code, and run it. 
Check the Shell and confirm that /sd mounted successfully, along with the file count, total capacity, used space, and free space. 
5. Hardware Operation Steps¶
Power off first, then insert the microSD card flat in the correct orientation for the card slot, and power on after confirming it is locked in place. For the 5-inch model, follow the actual silkscreen on the device and the notch orientation of the slot.
Do not remove the card while the program is accessing the SD card. Before removing the card, stop the program, unmount the file system, and power off.
Connect the CrowPanel Advanced 5-inch ESP32-P4 HMI AI display to your computer using a USB cable.
6. Key Code Explanation¶
SD_CLK_PIN = 43
SD_CMD_PIN = 44
SD_D0_PIN = 39
SD_POWER_PIN = 38
SD_WIDTH = 1
SD_MOUNT_POINT = "/sd"
GPIO38 turns on the card slot power, while GPIO43, 44, and 39 serve as the clock, command, and D0, respectively. The program first attempts to access /sd to avoid repeated mounting; if it is not mounted, it creates machine.SDCard(slot=0, width=1, freq=20000000) and mounts it via vfs.mount(). os.listdir() lists the files, and os.statvfs() uses the file system block information to calculate the capacity.
7. Experimental Observations¶
The Shell first prints the ESP32-P4, Slot 0, GPIO, and 1-line SDIO parameters, then displays SD card mounted successfully at /sd.
The root directory should contain the preloaded test file, the total capacity should be greater than 0, and the sum of used and free space should be close to the total capacity. If initialization fails, check in sequence whether the card was inserted while powered off, whether the card is locked, whether the format is FAT/FAT32, and whether the firmware is INCH5.


