ESP32 with YX5300/YX6300 MP3 Player (Arduino IDE)

This is a guide on how to interface the YX5300 / YX6300 Serial MP3 Music Player Module with the ESP32 programmed with Arduino IDE. You’ll learn how to wire the MP3 player to the ESP32 and play sounds and music tracks from.mp3/.wav files that are stored in a MicroSD card. As a project example, we’ll also show you how to build an MP3 audio player with button controls and a volume knob.

ESP32 Guide YX5300 YX6300 MP3 Player Arduino IDE

Table of Contents

In this tutorial, we’ll cover the following topics:

Introducing the YX5300 / YX6300 Serial MP3 Music Player Module

The YX5300 / YX6300 is a low-cost Serial (UART) MP3 music player module that can be interfaced with microcontrollers (such as the ESP32, ESP8266, or Arduino) to play .mp3 or .wav files stored on a MicroSD card by sending serial commands.

YX5300 YX6300 MP3 Player Serial Module Front Pinout Headphone Jack

Where to Buy?

You can check our Maker Advisor Tools page to compare the YX5300 / YX6300 module price in different stores:

Wiring the YX5300 / YX6300 Serial MP3 Music Player to the ESP32

YX5300 YX6300 MP3 Player Serial Module Front Pinout Highlight

Wiring the MP3 player to the ESP32 is quite simple, as it only requires two serial pins: RX and TX.

YX5300 / YX6300ESP32
TXGPIO 16 (UART 2 RX)
RXGPIO 17 (UART 2 TX)
VCCVIN (5V)
GNDGND

According to the product specifications in the manual, the VCC works with voltages between 3.2V-5.2V. According to our testing, it works most reliably when connected to 5V (VIN).

* you can use any other RX/TX GPIOs. Check the ESP32 Pinout Guide:

You may also like: ESP32 UART Communication (Serial): Set Pins, Interfaces, Send and Receive Data (Arduino IDE).

We’ll connect the RX pin to GPIO 17, and the TX pin to GPIO 16.

ESP32 YX5300 YX6300 MP3 Player Play Sounds Wiring Circuit

Important: in all projects that use serial commands, you need to connect the pins TX-RX and the RX-TX. However, if the code is not running properly (not sending/receiving serial commands), you might need to swap the wiring and connect RX-RX and TX-TX. One of our Serial MP3 players came with the wrong labels on the module, and the only way it worked was connected with TX-TX and RX-RX.

Preparing the MicroSD Card

The YX5300 MP3 player has a microSD card slot where you can insert a microSD card to store your .mp3 or .wav files. Here are the main specifications that the module supports.

YX5300Value
Supported MicroSD CardsMicroSD <= 32GB
Card FormatFAT16 / FAT32
Track Formats.mp3 / .wav
Frequencies8-48 kHz

Before proceeding with the tutorial, make sure you format your microSD card as FAT32. Follow the next instructions to format your microSD card or use a software tool like SD Card Formater (compatible with Windows and Mac OS).

1. Insert the microSD card into your computer. Go to My Computer and right-click on the SD card. Select Format as shown in the figure below.

format microSD card

2. A new window pops up. Select FAT32, press Start to initialize the formatting process and follow the onscreen instructions.

format microsd card fat32 windows

3. In order to play music tracks, you must have folders and your files named correctly, so the MP3 module can access them. Here’s an example of the folder and filename structure:

01/
001xxx.mp3
002xxx.mp3
003xxx.mp3
02/
004xxx.mp3
005xxx.mp3
03/
006xxx.mp3
007xxx.mp3
008xxx.mp3

Your folders must have 2-digits and each tracks needs to start with 3-digit. Each track name must be unique. Note: there is a maximum of 99 folders with 255 songs in each folder.

Example, start by creating 2 folders in your microSD card (name your folders: 01 and 02):

YX5300 YX6300 MP3 Player Serial Module Folder Structure

4. Copy .mp3 or .wav files, starting with 001, 002, and 003. The file names must be sequencial and start with 3-digits.

YX5300 YX6300 MP3 Player Serial Module Folder and Filenames

5. Finally, remove the microSD card from you computer and attach it to the YX5300 module.

YX5300 YX6300 MP3 Player Module Back with MicroSD Card

You can click here to download a .zip folder with the royalty free music files used for this project.

Connecting Headphones or Speakers to the YX5300 Module

The YX5300 doesn’t come with any headphones or speakers, but you can use some heaphones similar to the ones illustrated in the image below and plug them into the module headphone jack.

Headphone speaker examples

Installing the YX5300 for ESP32 Library (YX6300 compatible)

There are several libraries to interface the YX5300 serial module with the ESP32. We’ll use the YX5300_ESP32 library by bluejunimo (even though the library is called YX5300 for ESP32, it’s also fully compatible with the YX6300 MP3 module without any additional changes).

You can install the library via the Arduino IDE library Manager. Search for YX5300 for ESP32 and install the library by bluejunimo.

Installing YX5300 YX6300 for ESP32 MP3 Player Serial Arduino IDE

Preparing Arduino IDE

We’ll program the ESP32 board using Arduino IDE. So, make sure you have the ESP32 add-on installed. Follow the next tutorial:

If you prefer using VSCode + PlatformIO, follow the next tutorial instead:

Testing the YX5300 / YX6300 MP3 Player (Play Sound)

The following code is the one provided by the library to test the MP3 player module. You’ll test if the module is working if you can hear the audio files being played in the headphone or speaker.

/*********
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/esp32-yx5300-yx6300-mp3-player-arduino/
  Example based on the library example by bluejunimo https://github.com/bluejunimo/YX5300_ESP32/
*********/
#include <YX5300_ESP32.h>

// Connect to YX5300 Serial pins (RX and TX)
#define RX 16
#define TX 17

// mp3 object
YX5300_ESP32 mp3;

void setup() {
  // Initialize connection with the YX5300/YX6300 module
  mp3 = YX5300_ESP32(Serial2, RX, TX);
  
  // Shows the hex commands being sent to the mp3 device (includes helpful errors)
  Serial.begin(115200);
  mp3.enableDebugging();

  // Plays the first track stored on the microSD Card (Track Formats .mp3/.wav | Frequencies 8-48 kHz)
  // MicroSD Card folder and file structure, example folder name 01 and file name 001xxx.mp3:
  // 01/001xxx.mp3   01/002xxx.mp3   01/003xxx.mp3   02/004xxx.mp3
  mp3.playTrack(1);
}

void loop() {
  // put your main code here, to run repeatedly:
}

View raw code

How the Code Works

Let’s break down how the code works.

Include YX5300_ESP32 library

First, you need to include the YX5300_ESP32.h library created by bluejunimo that will make it very easy to interface with the MP3 player module

#include <YX5300_ESP32.h>

Initialize the YX5300

Set the YX5300 RX and TX pins and create an object for the module called mp3.

#define RX 16
#define TX 17

YX5300_ESP32 mp3;

setup()

In the setup(), start by initializing the module with the a specific Serial channel.

mp3 = YX5300_ESP32(Serial2, RX, TX);

Recommended reading: ESP32 UART Communication (Serial): Set Pins, Interfaces, Send and Receive Data (Arduino IDE).

Enable the Serial communication and MP3 debugging, so you can see the hex serial commands being sent to the device. With debugging enabled, you will also see helpful errors.

Serial.begin(115200);
mp3.enableDebugging();

You can quickly play the first track stored on the microSD Card using the following command:

mp3.playTrack(1);

Demonstration

Upload the previous code to your ESP32 board. It will play the first track stored in the microSD card. Your serial monitor should be printing a similar message:

ESP32 YX5300 YX6300 MP3 Player playback track Arduino IDE Serial Monitor Demonstration

If you have the headphones or speaker plugged into the headphone jack, you should be hearing the first track stored in your microSD card. If you are experiencing any issues, follow the next troubleshooting section.

YX5300 YX6300 MP3 Player Serial Module Connected to Headphone Speaker Jack

Troubleshooting the YX5300/YX6300 MP3 Player Module

If you uploaded the previous code, but you can’t hear any sound with the YX5300/YX6300 MP3 Player Module it can be one of these following issues.

  • Wiring error: in all projects that use serial commands, you need to connect the pins TX-RX and the RX-TX. However, if the code is not running properly (not sending/receiving serial commands), you might need to swap the wiring and connect RX-RX and TX-TX. One of our Serial MP3 players came with the wrong labels on the module, and the only way it worked was connected with TX-TX and RX-RX;
  • Not enough power: according to the product specifications in the manual, the VCC is works with voltages between 3.2V-5.2V. According to our testing, it works most reliably when connected to 5V (VIN);
  • MicroSD card issue: your microSD must be equal or less than 32GB and formated with FAT16 / FAT32. The track formats must be in an .mp3 / .wav files with 8-48 kHz frequencies;
  • Wrong folder structure / filenames: your microSD card folder and file structure is incorrect, example folder and filenames: 01/001xxx.mp3 01/002xxx.mp3 01/003xxx.mp3 02/004xxx.mp3;
  • Issue headphones/speaker: we recommend testing your headphones/speakers in another device to make sure they are working.

YX5300 / YX6300 MP3 Player with Button Controls and Volume Knob

As a project example, we’ll show you how to build an MP3 player with button controls and a volume knob. Having the Arduino IDE prepared with the library installed and the microSD card inserted in the YX5300 module with the correct folders and files described earlier, you can continue following this guide.

Parts Required

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematic Diagram

Wiring the MP3 player to the ESP32 is quite simple, as it only requires two serial pins: RX and TX.

YX5300 / YX6300ESP32
TXGPIO 16 (UART 2 RX)
RXGPIO 17 (UART 2 TX)
VCCVIN (5V)
GNDGND

According to the product specifications in the manual, the VCC is works with voltages between 3.2V-5.2V. According to our testing, it works most reliably when connected to 5V (VIN).

Potentiometer/PushbuttonsESP32
PotentiometerGPIO 4
Play/Pause PushuttonGPIO 5
Previous Track PushbuttonGPIO 18
Next Track PushbuttonGPIO 19

Wire all the components to your ESP32 using the previous tables or by following the schematic diagram below.

ESP32 YX5300 YX6300 MP3 Player with Button Controls Wiring Circuit

MP3 Player Code

Upload the following code to turn your ESP32 into an MP3 audio player device.

/*********
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/esp32-yx5300-yx6300-mp3-player-arduino/
*********/
#include <YX5300_ESP32.h>

// Connect to YX5300 Serial pins (RX and TX)
#define RX 16
#define TX 17

// Global variables for the pushbuttons
const uint8_t pinBtnPlay = 5;
const uint8_t pinBtnPrev = 18;
const uint8_t pinBtnNext = 19;

// Bool variable to track if the audio is playing or paused
volatile bool isPlaying = true;

// Variables for debouncing the pushbuttons
const unsigned long DEBOUNCE_DELAY = 500;  // in milliseconds
volatile unsigned long lastPressTime = 0;

// Analog input pin the potentiometer is attached to
const uint8_t potVolume = 4; 

// Auxiliary volume variables
#define MIN_VOLUME 0
#define MAX_VOLUME 30
int current_volume = 0;

// mp3 object
YX5300_ESP32 mp3; 

// Play/Pause Button Interrupt Service Routine (ISR)
void ARDUINO_ISR_ATTR buttonPlay() {
  unsigned long now = millis();
  if (now - lastPressTime > DEBOUNCE_DELAY) {    
    if(isPlaying) {
      mp3.pause();
      isPlaying = false;
    }
    else {
      mp3.resume();
      isPlaying = true;
    }
  }
  lastPressTime = now;
}

// Previous Track Button Interrupt Service Routine (ISR)
void ARDUINO_ISR_ATTR buttonPrev() {
  unsigned long now = millis();
  if (now - lastPressTime > DEBOUNCE_DELAY) {    
    mp3.prev();
    isPlaying = true;
  }
  lastPressTime = now;
}

// Next Track Button Interrupt Service Routine (ISR)
void ARDUINO_ISR_ATTR buttonNext() {
  unsigned long now = millis();
  if (now - lastPressTime > DEBOUNCE_DELAY) {    
    mp3.next();
    isPlaying = true;
  }
  lastPressTime = now;
}

void setup() {
  // Init pushbuttons with internal pullup resistor
  pinMode(pinBtnPlay, INPUT_PULLUP);
  pinMode(pinBtnPrev, INPUT_PULLUP);
  pinMode(pinBtnNext, INPUT_PULLUP);

  // Set interrupts ISR functions
  attachInterrupt(pinBtnPlay, buttonPlay, HIGH);
  attachInterrupt(pinBtnPrev, buttonPrev, HIGH);
  attachInterrupt(pinBtnNext, buttonNext, HIGH);

  // Initialize connection with the YX5300/YX6300 module
  mp3 = YX5300_ESP32(Serial2, RX, TX);
  
  // Shows the hex commands being sent to the mp3 device (includes helpful errors)
  Serial.begin(115200);
  mp3.enableDebugging();
  
  // Plays the first track stored on the SD Card (Track Formats .mp3/.wav | Frequencies 8-48 kHz)
  // SD Card folder and file structure, example folder name 01 and file name 001xxx.mp3:
  // 01/001xxx.mp3   01/002xxx.mp3   01/003xxx.mp3   02/004xxx.mp3
  mp3.playTrack(1);
}

void loop() {  
  // Read the potentiometer's value
  int raw_value = analogRead(potVolume);
  
  // Map the value to the min/max of the mp3 device's volume values 
  int new_volume = map(raw_value, 0, 4095, MIN_VOLUME, MAX_VOLUME);
  
  // Change the volume if the pot position has changed
  if(new_volume != current_volume) {
    // Update the mp3 device's volume
    mp3.setVolume(new_volume);
    // Update the current volume with the new volume
    current_volume = new_volume;
  }
}

View raw code

How the Code Works

Let’s take a look at how the code works so that you can understand how to use the YX5300 serial commands, interrupts to trigger the button commands and potentiometer to adjust the volume.

Include YX5300_ESP32 library

First, you need to include the YX5300_ESP32.h library created by bluejunimo that will make it very easy to interface with the MP3 player module

#include <YX5300_ESP32.h>

Initialize the YX5300

Set the YX5300 RX and TX pins and create an object for the module called mp3.

#define RX 16
#define TX 17

YX5300_ESP32 mp3;

Variables

Create some global variables for the pushbuttons GPIOs and potentiometer.

const uint8_t pinBtnPlay = 5;
const uint8_t pinBtnPrev = 18;
const uint8_t pinBtnNext = 19;

const uint8_t potVolume = 4; 

Define a bool variable to track if the audio is playing or paused.

volatile bool isPlaying = true;

These next two variables are used for debouncing the pushbuttons.

const unsigned long DEBOUNCE_DELAY = 500;  // in milliseconds
volatile unsigned long lastPressTime = 0;

Finally, you need these variables to check the min/max volume and store the courrent volume according potentiometer position.

#define MIN_VOLUME 0
#define MAX_VOLUME 30
int current_volume = 0;

setup()

In the setup(), initialize the pushbuttons as inputs and set them as interrupts to detect changes on their states.

pinMode(pinBtnPlay, INPUT_PULLUP);
pinMode(pinBtnPrev, INPUT_PULLUP);
pinMode(pinBtnNext, INPUT_PULLUP);

attachInterrupt(pinBtnPlay, buttonPlay, HIGH);
attachInterrupt(pinBtnPrev, buttonPrev, HIGH);
attachInterrupt(pinBtnNext, buttonNext, HIGH);

Learn more about ESP32 interrupts: ESP32 GPIO Interrupts with Arduino IDE.

Then, initialize the module with a specific Serial channel.

mp3 = YX5300_ESP32(Serial2, RX, TX);

Enable the Serial communication and MP3 debugging, so you can see the hex serial commands being sent to the device. With debugging enabled, you will also see helpful errors.

Serial.begin(115200);
mp3.enableDebugging();

You can quickly play the first track stored on the microSD Card using the following command:

mp3.playTrack(1);

Button Interrupts

Create 3 interrupt functions that are instantly called when the play/pause, previous track, and next track pushbuttons are pressed.

void ARDUINO_ISR_ATTR buttonPlay() {
  unsigned long now = millis();
  if (now - lastPressTime > DEBOUNCE_DELAY) {    
    if(isPlaying) {
      mp3.pause();
      isPlaying = false;
    }
    else {
      mp3.resume();
      isPlaying = true;
    }
  }
  lastPressTime = now;
}

Depending on the pushbutton that was pressed, a specific function will run. These are the 4 main methods that control the MP3 player, and their names are pretty self-explanatory:

mp3.resume();
mp3.pause();
mp3.prev();
mp3.next();

loop()

In the loop(), you constantly read the current potentiometer volume.

int raw_value = analogRead(potVolume);

Then, if the volume has changed (increased or decreased), you adjust the volume.

int new_volume = map(raw_value, 0, 4095, MIN_VOLUME, MAX_VOLUME);

if(new_volume != current_volume) {
  mp3.setVolume(new_volume);
  current_volume = new_volume;
}

MP3 Player Demonstration

Upload the previous code to your ESP32 board with all the components wired. It will play the first track stored in the microSD card (you can click here to download a .zip folder with the royalty free music files used for this project).

In your Arduino IDE serial monitor, you should see similar messages when you press the pushbuttons.

ESP32 YX5300 YX6300 MP3 Player with Button Controls Arduino IDE Serial Monitor Demonstration

Check if everything is working:

  1. First pushbutton plays the track or pauses the track;
  2. The second pushbutton returns to the previous track;
  3. The third pushubtton skpis to the next track;
  4. Finally, you can move the potentiometer knob to adjust the volume.
ESP32 YX5300 YX6300 MP3 Player with Button Controls Arduino IDE Circuit Demonstration

Watch the next video for a project live demonstration.


Wrapping Up

This was a comprehensive guide on how to interface the YX5300/YX6300 MP3 Player with the ESP32. It can be quite useful to play sounds or music with your ESP32 board. Additionally, you can add any sensor to your project so that the ESP32 starts playing audio when a certain condition is met.

We hope you’ve found this guide useful. We have other guides that might be helpful:

To learn more about the ESP32, make sure to check out our resources:



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.