Skip to content

5inch_P4_IDF_12_SD_Music_Playback: ESP32-P4 SD Card Music Playback

1. Course Introduction

This lesson plays WAV audio from the SD card through the development board. Init() first completes the I2C, STC8, and SD card initialization, then initializes the amplifier control and the I2S audio system; app_main finally calls Audio_play_wav_sd("/sdcard/huahai.wav") to play the specified file.

2. Learning Objectives

  • Understand that SD card mounting must precede WAV file access.
  • Master the initialization sequence of audio control, amplifier shutdown, and I2S initialization.
  • Be able to verify the WAV file location based on the absolute mount path.
  • Be able to use serial logs to locate SD card, audio control, or audio playback initialization errors.

3. Prerequisites

  • Applicable development board: CrowPanel Advanced 5-inch ESP32-P4 HMI AI Display Development Board.
  • Software: VS Code, ESP-IDF Extension (ESP-IDF v5.5.4 or later).
  • Project dependencies: Keep the main/main.c, peripheral/bsp_sd, and peripheral/bsp_audio components, as well as the sdmmc_cmd and esp_vfs_fat managed components.

Code download link:

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

The audio files we provide:

-CrowPanel-Advanced-5inch-ESP32-P4-HMI-AI-Display-800x480-IPS-Touch-Screen/example/V1.0/idf-code/convert_wav/Output at master · Elecrow-RD/-CrowPanel-Advanced-5inch-ESP32-P4-HMI-AI-Display-800x480-IPS-Touch-Screen

4. Software Operation Steps

Open the ESP-IDF Extension panel in VS Code, click Open ESP-IDF Project, and select the Lesson12-Playing_Loca_Music_from_SD_Card folder.

You can also drag this project folder directly into VS Code.

Open project

First select the code runtime environment ESP-IDF v5.5.4, set the flashing method to UART, and then select the serial port corresponding to the actual development board. Next, in the ESP-IDF Extension panel, click Set Espressif Device Target and select esp32p4. After the configuration is complete, the status bar should display ESP-IDF v5.5.4, UART, the required COM port, and ESP32-P4.

Click SDK Configuration Editor

Click SDK Configuration Editor in the VS Code bottom status bar or the ESP-IDF extension panel, and wait for the configuration page to fully load before modifying parameters. If the page is still loading, do not run Build immediately.

Click SDK Configuration Editor

Wait for the SDK Configuration Editor to finish loading

Enter flash in the search box and ensure Flash SPI mode: QIO; Flash Sampling Mode: STR Mode; Flash SPI speed: 80 MHz; Flash size: 16 MB. These parameters should match the onboard Flash of the Advance-P4.

Configure Flash parameters

Then refer to "4. Software Operation Steps" in Lesson07_Turn_on_the_Screen to complete the detailed SDK configuration; the related configuration methods have been explained in Lesson 7.

After verifying the configuration is correct, click Save in the upper-right corner; confirm that the changes have been saved, then run Build to compile.

Click Full Clean to clear the cache left over from the previous compilation. Perform this operation after the first compilation, when switching project configurations, or after modifying SDK parameters, to prevent old configurations from affecting new build results.

Run Full Clean

Click Build to compile the project.

Compile the project

Confirm that the huahai.wav file has been placed on the SD card, insert the SD card into the development board, and connect the USB; click Select Port to Use to choose the serial port, then click Flash to flash the firmware.

Select the serial port and flash

After flashing is complete, click Monitor to open the serial monitor; you should see the card information and playback logs; press Ctrl + ] to exit the monitor.

Open the monitor

After flashing, the program plays automatically; observe whether the speaker outputs music.

image-20260729122331790

Finally, you can use the one-click operation button on the ESP-IDF status bar to sequentially run compilation, flashing, and opening the serial monitor. Use this only after the project configuration, serial port, and code have all been confirmed correct; if you need to locate a problem, you should still follow the steps above one by one.

One-click compile, flash, and open monitor

5. Hardware Operation Steps

With the power off, insert the MicroSD card containing huahai.wav into the development board's SD card slot. Pay attention to the card orientation—the gold contacts should face down.

image-20260729122407145

Connect the ESP32-P4 development board to the computer using a USB data cable; the development board's power indicator lights up.

image-20260729114452613

After flashing is complete and a reset is performed, observe whether the serial port prints the SD card information and WAV File Info to confirm that the file is correctly recognized.

Observe file information

Observe whether the speaker plays the WAV music; after playback ends, the serial port should print Audio playback completed.

image-20260729122331790

6. Key Code Explanation

err = i2c_init();
err = stc8_i2c_init();
err = sd_init();
if (err != ESP_OK) init_fail("sd", err);
vTaskDelay(500 / portTICK_PERIOD_MS);

Init() prepares I2C and the STC8 controller before mounting the SD card. After a successful mount it waits 500 ms before configuring audio; any initialization failure enters the common halt loop.

set_Audio_ctrl(false);
err = audio_init();
...
Audio_play_wav_sd("/sdcard/huahai.wav");

audio_ctrl_init() prepares amplifier control, set_Audio_ctrl(false) keeps the amplifier off during setup, and audio_init() prepares playback. After another 500 ms stabilization delay, Audio_play_wav_sd() opens /sdcard/huahai.wav from the mounted card and starts WAV playback.

7. Experimental Observations

After successful initialization, the development board's speaker plays the huahai.wav file from the SD card. If the file does not exist, the SD card is not mounted, or the WAV format is incompatible, the corresponding error will appear on the serial port; initialization failures are continuously reported by init_fail().