ESP32 - MP3 Player

In this guide, we'll discover how to create an MP3 player using an ESP32, an MP3 player module, a Micro SD Card, and a speaker. The MP3 files, which can be music or recorded audio, are kept on the micro SD Card. The ESP32 will be programmed to send commands to control the MP3 player module which song to pick from the SD Card, convert it into sound, and then send that sound to the speaker. We'll dive into the following aspects:

Afterward, you can take the code a step further by incorporating a potentiometer or rotary encoder to adjust the volume.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Serial MP3 Player Module
1×Micro SD Card
1×3.5mm Aux Speaker
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables.

Introduction to Serial MP3 Player Module and Speaker

Serial MP3 Player Module Pinout

A serial MP3 player module has three interfaces:

  • The interface to ESP32 includes 4 pins:
    • RX pin: data pin, needs to be connected a TX pin of ESP32
    • TX pin: data pin, needs to be connected a RX pin of ESP32
    • VCC pin: power pin, needs to be connected to VCC (3.3V or 5V)
    • GND pin: power pin, needs to be connected to GND (0V)
  • The interface to the speaker is a 3.5mm Aux output female jack.
  • The interface to the Micro SD Card is a Micro SD Card Socket in the back of the module.
Serial MP3 Player Module Pinout

Speaker Pinout

A speaker usually has two interfaces:

  • Audio signal interface: it is 3.5mm Aux male connector that connects to the MP3 player module
  • Poweer interface: it can be USB, 5V power adapter or any other power interface

How It Works

To get started, make sure you have the following:

  • Gather a collection of songs or recorded audio that you want to play and store them on a micro SD Card.
  • Insert the micro SD Card into the MP3 player module.
  • Connect the MP3 player module to the ESP32 and connect the speaker to the MP3 player module. Also, ensure the speaker is connected to a power source.

Each MP3 file on the micro SD Card gets a ID, starting from 0. Then, you can tell the ESP32 to do various things with the MP3 player module, such as:

  • Play: Start playing the selected song.
  • Pause: Pause the song.
  • Play Next: Move to the next song.
  • Play Previous: Go back to the previous song.
  • Change Volume: Adjust how loud the music is.

When the MP3 player module gets a command, it reads the MP3 file from the micro SD Card, turns it into an audio signal, and sends that signal to the speaker using a 3.5mm Aux connection.

Wiring Diagram

  • How to connect ESP32 and mp3 player using breadboard
ESP32 MP3 player module Wiring Diagram

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and mp3 player

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code - Play Music

The below code plays the first song stored on the Micro SD Card.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-mp3-player */ #define CMD_PLAY_NEXT 0x01 #define CMD_PLAY_PREV 0x02 #define CMD_PLAY_W_INDEX 0x03 #define CMD_SET_VOLUME 0x06 #define CMD_SEL_DEV 0x09 #define CMD_PLAY_W_VOL 0x22 #define CMD_PLAY 0x0D #define CMD_PAUSE 0x0E #define CMD_SINGLE_CYCLE 0x19 #define DEV_TF 0x02 #define SINGLE_CYCLE_ON 0x00 #define SINGLE_CYCLE_OFF 0x01 void setup() { Serial.begin(9600); Serial2.begin(9600); delay(500); // wait chip initialization is complete mp3_command(CMD_SEL_DEV, DEV_TF); // select the TF card delay(200); // wait for 200ms mp3_command(CMD_PLAY, 0x0000); // Play mp3 //mp3_command(CMD_PAUSE, 0x0000); // Pause mp3 //mp3_command(CMD_PLAY_NEXT, 0x0000); // Play next mp3 //mp3_command(CMD_PLAY_PREV, 0x0000); // Play previous mp3 //mp3_command(CMD_SET_VOLUME, 30); // Change volume to 30 } void loop() { } void mp3_command(int8_t command, int16_t dat) { int8_t frame[8] = { 0 }; frame[0] = 0x7e; // starting byte frame[1] = 0xff; // version frame[2] = 0x06; // the number of bytes of the command without starting byte and ending byte frame[3] = command; // frame[4] = 0x00; // 0x00 = no feedback, 0x01 = feedback frame[5] = (int8_t)(dat >> 8); // data high byte frame[6] = (int8_t)(dat); // data low byte frame[7] = 0xef; // ending byte for (uint8_t i = 0; i < 8; i++) { Serial2.write(frame[i]); } }

Quick Instructions

ESP32 Code - Play Music with control buttons

The below code is an upgrade of the previous code. It adds foyr buttons to let you interact with MP3 player.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-mp3-player */ #include <ezButton.h> #define CMD_PLAY_NEXT 0x01 #define CMD_PLAY_PREV 0x02 #define CMD_PLAY_W_INDEX 0x03 #define CMD_SET_VOLUME 0x06 #define CMD_SEL_DEV 0x09 #define CMD_PLAY_W_VOL 0x22 #define CMD_PLAY 0x0D #define CMD_PAUSE 0x0E #define CMD_SINGLE_CYCLE 0x19 #define DEV_TF 0x02 #define SINGLE_CYCLE_ON 0x00 #define SINGLE_CYCLE_OFF 0x01 ezButton button_play(32); // create ezButton object that attach to ESP32's pin GPIO32 ezButton button_pause(33); // create ezButton object that attach to ESP32's pin GPIO33 ezButton button_next(25); // create ezButton object that attach to ESP32's pin GPIO25 ezButton button_prev(26); // create ezButton object that attach to ESP32's pin GPIO26 void setup() { Serial.begin(9600); Serial2.begin(9600); delay(500); // wait chip initialization is complete mp3_command(CMD_SEL_DEV, DEV_TF); // select the TF card delay(200); // wait for 200ms button_play.setDebounceTime(50); // set debounce time to 50 milliseconds button_pause.setDebounceTime(50); // set debounce time to 50 milliseconds button_next.setDebounceTime(50); // set debounce time to 50 milliseconds button_prev.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button_play.loop(); // MUST call the loop() function first button_pause.loop(); // MUST call the loop() function first button_next.loop(); // MUST call the loop() function first button_prev.loop(); // MUST call the loop() function first if (button_play.isPressed()) { Serial.println("Play mp3"); mp3_command(CMD_PLAY, 0x0000); } if (button_pause.isPressed()) { Serial.println("Pause mp3"); mp3_command(CMD_PAUSE, 0x0000); } if (button_next.isPressed()) { Serial.println("Play next mp3"); mp3_command(CMD_PLAY_NEXT, 0x0000); } if (button_prev.isPressed()) { Serial.println("Play previous mp3"); mp3_command(CMD_PLAY_PREV, 0x0000); } } void mp3_command(int8_t command, int16_t dat) { int8_t frame[8] = {0}; frame[0] = 0x7e; // starting byte frame[1] = 0xff; // version frame[2] = 0x06; // the number of bytes of the command without starting byte and ending byte frame[3] = command; // frame[4] = 0x00; // 0x00 = no feedback, 0x01 = feedback frame[5] = (int8_t)(dat >> 8); // data high byte frame[6] = (int8_t)(dat); // data low byte frame[7] = 0xef; // ending byte for (uint8_t i = 0; i < 8; i++) { Serial2.write(frame[i]); } }

The wiring diagram for the above code:

ESP32 MP3 player speaker Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Now, you can modify the projects to add more functions, for example:

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

Function References

※ OUR MESSAGES