1.5.1 MP3 Playback from SD Card
Play MP3 audio files stored on a Micro SD card through the I2S audio amplifier.
Overview
This example combines the SD card and the I2S audio output to play MP3 music files. You’ll mount the SD card, find an MP3 file, and stream its decoded audio to the amplifier that drives the onboard speaker.
Note
This example is based on the open-source I2Saudio_SD_MMC example from the ESP32-audioI2S library. For the LAFVIN ESP32-S3 Multimedia Kit, we adapted the SD_MMC and I2S pin configuration, matched the board’s 1-bit SD card wiring, and simplified the local MP3 playback flow for this tutorial.
What You’ll Learn
Configuring I2S audio output pins with the
AudiolibraryStreaming MP3 data from an SD card to an I2S DAC
Using
audio.loop()to keep playback running
Before You Begin
Complete the setup steps in Preparation before continuing.
Note
Use the recommended Arduino IDE settings in Recommended Board Settings unless this lesson says otherwise.
Open the example sketch from the code package:
LAFVIN-ESP32-S3-Multimedia-Kit-main/1.Arduino/5.1_Play_SDMMC/5.1_Play_SDMMC.ino
Note
Before uploading, copy test1.mp3 from 1.Arduino/5.1_Play_SDMMC/music to the /music/ folder on your SD card.
Signal |
ESP32-S3 |
|---|---|
SD CLK |
GPIO 39 |
SD CMD |
GPIO 38 |
SD D0 |
GPIO 40 |
I2S BCLK |
GPIO 42 |
I2S LRC |
GPIO 14 |
I2S DOUT |
GPIO 41 |
Upload and Test
Prepare a Micro SD card with an MP3 file at
/music/test1.mp3.Insert the SD card, then connect the board with a USB Type-C data cable.
Open
5.1_Play_SDMMC.inoin Arduino IDE.In the Tools menu, select
ESP32S3 Dev Moduleas the board and choose the correct serial port.Click Upload.
Open Serial Monitor at
115200baud. The audio track starts playing through the onboard speaker automatically. Metadata, if embedded in the MP3 file, is printed to the Serial Monitor.
How the Code Works
SD Card Mount
The sketch mounts the SD card first so the audio library can open the MP3 file from the filesystem.
#include "SD_MMC.h"
SD_MMC.setPins(SD_MMC_CLK, SD_MMC_CMD, SD_MMC_D0);
SD_MMC.begin("/sdcard", true);
Audio Output Setup
The Audio object is configured to use the board’s fixed I2S output pins.
#include "Audio.h"
Audio audio;
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(15);
Play MP3 from SD
After the SD card and audio output are ready, the sketch starts playback from the file stored on the SD card.
audio.connecttoFS(SD_MMC, "music/test1.mp3");
Audio Loop
The audio.loop() call must run continuously to decode MP3 data and feed audio samples to the I2S output.
void loop() {
audio.loop();
vTaskDelay(1);
}
In the provided sketch:
The SD card is mounted in 1-bit SDMMC mode, matching the board’s pinout
The I2S pins are mapped to the onboard audio path
Volume 15 is used as a comfortable default level
Try This Next
Replace
test1.mp3with your own MP3 files while keeping the same file pathChange
audio.setVolume(15)to a different valueCombine this example with 1.4 SD Card (SDMMC) to browse and play different tracks