.. _arduino_play_sdmp3: ================================= 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 ``Audio`` library * Streaming 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 :ref:`preparation` before continuing. .. note:: Use the recommended Arduino IDE settings in :ref:`arduino_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.ino`` in Arduino IDE. #. In the **Tools** menu, select ``ESP32S3 Dev Module`` as the board and choose the correct serial port. #. Click **Upload**. #. Open **Serial Monitor** at ``115200`` baud. 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. .. code-block:: cpp #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. .. code-block:: cpp #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. .. code-block:: cpp 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. .. code-block:: cpp 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.mp3`` with your own MP3 files while keeping the same file path * Change ``audio.setVolume(15)`` to a different value * Combine this example with :ref:`arduino_sdmmc` to browse and play different tracks