ESP32 MP3 Audio Playback from SD Card

by Pratik (A PCBArtist)

This article demonstrates ESP32 MP3 audio playback capability using the PCB Artists ES8388 audio codec module.

There are numerous examples present in the ESP-ADF repository and several of them demonstrate how to play back an MP3 file retrieved from various sources (internal memory, SD card, network location, etc). In this case, the MP3 file is fetched from an SD card. This is one of the best ways to evaluate the audio quality that the ESP32 is capable of producing using a good quality audio codec.

We noticed that the audio playback performance is extremely good with almost no noticeable deterioration in the audio quality compared to a standard smartphone. The codec used here is commonly used in budget smartphones and therefore you can expect the same audio quality in most ESP32 applications. Also, Espressif has definitely done a great job with the MP3 decoding APIs!

ESP32 MP3 Player Demo Components

The MP3 player demo consists of 3 main components:
  • ESP32 module (ESP32-PICO-KIT in our case)
  • Audio codec (ES8388 module used here)
  • SD card (we used a micro SD card adapter)
The microSD card holds the MP3 audio file that the ESP32 reads, decodes, and plays back via the audio codec. Decoding the MP3 file consists of fetching the metadata and extracting the bit depth and sample rate, and then decoding the entire audio data stream accordingly. This decoded stream is then clocked into the audio codec module, which has been configured to a matching audio sampling rate and depth.

Interfacing the ESP32 to Audio Codec

The ESP32 communicates with the audio codec via 2 different links:
  • Control link, I2C interface
  • Data link, I2S interface
The power to the ES8388 module must be provided by the ESP32 board or module being used. Both AVDD and DVDD need 3.3V to function. It is recommended that you keep the interconnect cable short and also use dedicated, separate wires for the AVDD and DVDD lines to prevent digital noise in the audio stream.
If you do not know what the codec module pins are for, you can read more about interfacing an audio codec with ESP32.
The connections between ESP32 pins and ES8388 module are shown below:
esp32 es8388 audio codec module interface connection
ESP32 ES8388 Audio Codec Module Connections

Interfacing the ESP32 to SD Card

For this example to work, you need an SD card that stores the MP3 file. To maintain compatibility with the ESP32 Lyra-T example present in the Espressif ESP-ADF examples directory, we used a certain pinout that is shown below.
You can connect the SD card to the ESP32 module as shown here, using an SD breakout board like this.

esp32 mp3 player sd card pinout
ESP32 and microSD Breakout Connection

Modify the ESP32 MP3 Playback Example from ESP-ADF

A minor modification is required to make the SD card to audio output pipeline example work with this hardware setup.

Instead of the default 1-line SDIO mode used by the ESP-ADF example project, we will be using the good old SPI mode. Because MP3 files are compressed, SPI mode speed is good enough for playing back MP3 files.
To make the ESP-ADF example use SPI mode, you can follow these steps:
  • Open ESP-ADF example folder at
    {ADF_PATH}/examples/player/pipeline_sdcard_mp3
  • Inside the main folder, edit file play_sdcard_mp3_example.c to use SPI mode. Locate this line:
    // Initialize SD Card peripheral
    audio_board_sdcard_init(set, SD_MODE_1_LINE);

Change the above line to use SPI mode as follows:

    // Initialize SD Card peripheral
    audio_board_sdcard_init(set, SD_MODE_SPI);

Build the MP3 Pipeline Example

You can build the example by switching into that example directory and executing idf.py build.

The build and flash process is similar to the one followed by the ESP32 Bluetooth Audio Player Demo.

Running the Example

Once the example has been flashed, you should be able to copy any MP3 file over to the SD card and it should play back via the ES8388 audio codec module into your headphones that you have plugged in.
Note that the example only plays back MP3 files named as test.mp3.
Don’t forget to rename your MP3 file!
Change Log
  • Initial Release: 30 May 2021
References

None

You may also like

Leave a Comment

6 + one =