Example Code for ESP32 Play Music from SD Card
Connect to an SD card module to play music files from an SD card using the I2S amplifier and ESP32. Users can learn how to interface the SD card module with the ESP32, scan for music files on the SD card, and control audio playback using the I2S amplifier.
Hardware Preparation
- Firebeetle ESP32-E(DFR0654) x 1
- Speaker x1
- MicroSD Module (DFR0229) x 1
- SD/MicroSD Memory Card (SKU: FIT0394) ×1
- Card Reader ×1
- M-M/F-M/F-F Jumper wires x1 set
Software Preparation
- Arduino IDE
- Click the FireBeetle 2 ESP32-E Wikipage to find SDK installtion tutorial
- Download and install the I2S Ampilifer Library. (About how to install the library?)
Wiring Diagram

Description:
I2S Module VCC to ESP32-E 3V3
I2S Module GND to ESP32-E GND
I2S Module LRC to ESP32-E 26/D3
I2S Module BCLK to ESP32-E 25/D2
I2S Module DIN to ESP32-E 14/D6
SD card module 5V to ESP32-E VCC
SD card module GND to ESP32-E GND
SD card module MOSI to ESP32-E 23/MOSI
SD card module SS to ESP32-E 4/D12
SD card module SCL to ESP32-E 18/SCK
SD card module MISO to ESP32-E 19/MISO
I2S Module SPK+ to Speaker power +
I2S Module SPK- to Speaker power -
Other Preparation Work
- Open Arduino IDE.
- Select the board as "FireBeetle2 ESP32-E".
- Set the upload rate to 921600.
- Format the SD card to FAT32 and copy WAV format music files (with English path names) to it.
Sample Code
#include <DFRobot_MAX98357A.h>
DFRobot_MAX98357A amplifier;
String musicList[100]; // SD card music playlist
void setup(void)
{
Serial.begin(115200);
while ( !amplifier.initI2S(/*_bclk=*/GPIO_NUM_25, /*_lrclk=*/GPIO_NUM_26, /*_din=*/GPIO_NUM_14) ){
Serial.println("Initialize I2S failed !");
delay(3000);
}
while (!amplifier.initSDCard(/*csPin=*/GPIO_NUM_4)){
Serial.println("Initialize SD card failed !");
delay(3000);
}
Serial.println("Initialize succeed!");
amplifier.scanSDMusic(musicList);
printMusicList();
amplifier.setVolume(5);
amplifier.closeFilter();
amplifier.openFilter(bq_type_highpass, 500);
amplifier.SDPlayerControl(SD_AMPLIFIER_PLAY);
delay(5000);
if(musicList[1].length()){
Serial.println("Changing Music...\n");
amplifier.playSDMusic(musicList[1].c_str());
}else{
Serial.println("The currently selected music file is incorrect!\n");
}
}
void loop()
{
parseSerialCommand();
delay(500);
}
void printMusicList(void)
{
uint8_t i = 0;
if(musicList[i].length()){
Serial.println("\nMusic List: ");
}else{
Serial.println("The SD card audio file scan is empty, please check whether there are audio files in the SD card that meet the format!");
}
while(musicList[i].length()){
Serial.print("\t");
Serial.print(i);
Serial.print(" - ");
Serial.println(musicList[i]);
i++;
}
}
void parseSerialCommand(void)
{
String cmd;
float value;
if(Serial.available()){
cmd = Serial.readStringUntil('-');
if(cmd.equals("hp")){
Serial.println("Setting a High-Pass filter...\n");
value =Serial.parseFloat();
amplifier.openFilter(bq_type_highpass, value);
}else if(cmd.equals("lp")){
Serial.println("Setting a Low-Pass filter...\n");
value =Serial.parseFloat();
amplifier.openFilter(bq_type_lowpass, value);
}else if(cmd.equals("closeFilter")){
Serial.println("Closing filter...\n");
amplifier.closeFilter();
}else if(cmd.equals("vol")){
Serial.println("Setting volume...\n");
value =Serial.parseFloat();
amplifier.setVolume(value);
}else if(cmd.equals("start")){
Serial.println("starting amplifier...\n");
amplifier.SDPlayerControl(SD_AMPLIFIER_PLAY);
}else if(cmd.equals("pause")){
Serial.println("Pause amplifier...\n");
amplifier.SDPlayerControl(SD_AMPLIFIER_PAUSE);
}else if(cmd.equals("stop")){
Serial.println("Stopping amplifier...\n");
amplifier.SDPlayerControl(SD_AMPLIFIER_STOP);
}else if(cmd.equals("musicList")){
Serial.println("Scanning music list...\n");
amplifier.scanSDMusic(musicList);
printMusicList();
}else if(cmd.equals("changeMusic")){
cmd = musicList[Serial.parseInt()];
if(cmd.length()){
Serial.println("Changing Music...\n");
amplifier.playSDMusic(cmd.c_str());
}else{
Serial.println("The currently selected music file is incorrect!\n");
}
}else{
Serial.println("Help : \n \
Currently available commands (format: cmd-value):\n \
Start playback: e.g. start-\n \
Pause playback: e.g. pause-\n \
Stop playback: e.g. stop-\n \
Print music list: e.g. musicList-\n \
Change songs according to the music list: e.g. changeMusic-1\n \
Set and open high-pass filter: e.g. hp-500\n \
Set and open low-pass filter: e.g. lp-15000\n \
Close filter: e.g. closeFilter-\n \
Set volume: e.g. vol-5.0\n \
For the detailed meaning, please refer to the code comments of this demo.\n");
}
while(Serial.read() >= 0);
}
}
Result
- After uploading the code, open the Arduino IDE Serial Monitor (baud rate 115200) to check if the initialization is successful.
- The ESP32 will scan the SD card for WAV files and print the music list to the Serial Monitor.
- The speaker will automatically play the first music file from the SD card.
Screenshots:
- Select board and upload rate:

- Play music from SD card:

Additional Information
- Only WAV format music files with English path names are supported.
- The SD card must be formatted to FAT32 for compatibility with the ESP32.
Was this article helpful?
