ESP32 UNO - Mini Mp3 Player Module

Want your ESP32 Uno to play music, voice prompts or sound effects? Hook up the DIYables mini mp3 player to the ESP32 Uno form board, drop a few mp3 files on a micro SD card, and a small speaker plays them on command. The module uses the YX5200 decoder (the chip found on DFPlayer Mini clones), so the ESP32 Uno only sends short UART commands and never decodes audio itself.

By the end, you can:

  1. Connect a YX5200 mp3 board and speaker to the ESP32 Uno
  2. Lay out the micro SD card so tracks play in the right order
  3. Start, pause, skip and loop tracks with Arduino IDE code
  4. Change the volume and the track with push buttons
  5. Drive every player function from the Serial Monitor
ESP32 Uno mini mp3 player

Components You Need

1×ESP32 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×DIYables Mini Mp3 Player module
1×Micro SD Card
1×Speaker
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Mini Mp3 Player Module Overview

After this part, you know what the board does and which of its pins matter for this build.

The DIYables Mini Mp3 Player carries a YX5200-24SS mp3 decoder and a micro SD card slot. Its on-board amplifier pushes up to 3W into a speaker wired to the SPK pins. For louder setups, the DAC pins give a line-level stereo signal that you can feed into a separate amplifier.

Feature Value
Decoder chip YX5200-24SS
Control link UART, 9600 baud
Supply voltage 3.2V to 5.0V
Speaker output Mono, up to 3W
Line output DAC_L and DAC_R (stereo)
Volume steps 0 to 30
EQ choices 6 (Normal to Bass)
Play modes Single track, repeat track, repeat folder, repeat all, shuffle
Storage layout Root files or numbered folders on a micro SD card

Good to know: the module can also break into a playing song with an "advertisement" clip, and it answers status questions such as the current track number or volume.

Module Pinout

Only six pins are needed here. The rest are optional extras.

Pin Role
VCC Supply input, 3.2V to 5.0V
GND Ground
RX Command input from the board, wired through a 1K resistor
TX Reply output to the board RX pin
SPK_1 Speaker + (direct drive, 3W max)
SPK_2 Speaker − (direct drive, 3W max)
DAC_R Right line-level output for an external amplifier
DAC_L Left line-level output for an external amplifier
BUSY Goes LOW while a track plays, HIGH when idle
IO_1 Tap = previous track, hold = volume down
IO_2 Tap = next track, hold = volume up
Mini Mp3 Player Pinout

ESP32 Uno Pinout

Every header label on the ESP32 Uno form board, like D10 or D11, has its own GPIO number, and the sketch needs that number. Look up both pins in the picture before you plug in the jumper wires.

ESP32 Uno pinout diagram

How to Connect

When this part is done, the module, speaker and board are wired and ready for the first sketch.

The wiring diagram between ESP32 Uno Mini Mp3 Player

This image is created using Fritzing. Click to enlarge image

Mini Mp3 Player Pin ESP32 Uno Pin Note
VCC 5V
GND GND
RX D11 (GPIO23) Put a 1K resistor in series
TX D10 (GPIO5)
SPK_1 Speaker +
SPK_2 Speaker −

WARNING

D10 is GPIO5 on the ESP32 Uno, a boot strapping pin. The chip checks its level at power-up. The module TX line rests HIGH, which matches the default, so the board normally starts fine. If the board ever fails to boot or upload with the module plugged in, pull the TX wire off D10, upload, and plug it back in.

Good to know: ESP32 Uno pins run at 3.3V and are NOT 5V tolerant. The YX5200 uses 3.3V logic on its TX pin even when VCC is 5V, so the D10 (GPIO5) input is safe without a level shifter.

Leave the 1K resistor in series with the module RX pin all the same. It limits the current into the chip and removes most of the hiss this module is known for.

For the button sketches further down, add push buttons to D2 (GPIO26) and D3 (GPIO25). The second leg of each button goes to GND. No resistor is needed, because the code turns on the internal pull-up.

※ NOTE THAT:

SoftwareSerial is not available for the ESP32 Uno. The sketches use the hardware port Serial2 and route it to D10 and D11 with Serial2.begin(9600, SERIAL_8N1, 5, 23). Do not move the module to D0 (GPIO3) or D1 (GPIO1). Those two pins carry the USB serial link, so a module there would block uploads and garble the Serial Monitor.

Setting Up the Micro SD Card

This part makes sure the module finds every file and plays it in the order you expect.

Step 1 — Format the card

Give the micro SD card a fresh FAT32 or FAT16 format. Start from an empty card every time you change the track list.

Step 2 — Copy files to the root

Give each file a three-digit number:

/001.mp3 /002.mp3 /003.mp3

Step 3 — Optional numbered folders

To group sounds, make two-digit folders and put three-digit files inside:

/01/001.mp3 /01/002.mp3 /02/001.mp3
Rule Detail
First number Tracks count from 1, never 0
Folder names Two digits, 01 to 99
Files inside a folder Three digits, 001 to 255
Track order Set by copy order, not by file name

Good to know: the YX5200 reads files in the order they landed on the card. Copy them one at a time, in the order you want to hear them, right after a fresh format.

Add the DIYables_MiniMp3 Library

At the end of this part, Arduino IDE knows how to talk to the module.

  1. Plug in the board with the USB Type-C cable.
  2. Pick the board in Arduino IDE: choose ESP32 Dev Module and the CH340 COM port.
  3. Open the Libraries panel with the Libraries icon on the left bar.
  4. Search for "DIYables_MiniMp3" and find the entry by DIYables.
  5. Press Install and wait until it finishes.
  • Search for DIYables_MiniMp3 created by DIYables and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Library Manager
Type:
All
Topic:
All
DIYables_MiniMp3 by DIYables
Easy-to-use Arduino library for the DIYables Mini Mp3 Player module (YX5200-24SS). Communicates via UART. Works with all Arduino boards (Uno, Mega, Nano, Due, Giga), ESP32, ESP8266, and other compatible platforms. More info
1.0.0
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32 Dev Module on COM15
1

The library needs no other packages.

ESP32 Uno Code - First Sound Test with One Track

Use this first sketch as a wiring test: it sets the volume to 25 and plays 001.mp3 one time.

/* * ESP32 Uno + DIYables Mini Mp3 Player - Play One Track * * Module page: * https://diyables.io/products/mini-mp3-player-module * * Starts track 001 one time and then stays quiet. * * Connections on the ESP32 Uno: * Mp3 module RX <- D11 (GPIO23), with a 1K resistor in series * Mp3 module TX -> D10 (GPIO5) * Mp3 module VCC -> 5V * Mp3 module GND -> GND * Speaker wires -> SPK_1 and SPK_2 * * Micro SD card: mp3 files sit in the root folder as 001.mp3, 002.mp3 and so on. */ #include <DIYables_MiniMp3.h> // the ESP32 Uno has no SoftwareSerial, so the hardware UART Serial2 is used #define MP3_RX_PIN 5 // D10 (GPIO5) listens to the module TX line #define MP3_TX_PIN 23 // D11 (GPIO23) sends commands to the module RX line DIYables_MiniMp3 mp3; void setup() { Serial.begin(9600); Serial2.begin(9600, SERIAL_8N1, MP3_RX_PIN, MP3_TX_PIN); mp3.begin(Serial2); delay(1000); // give the module time to read the SD card mp3.setVolume(25); // loudness from 0 (mute) to 30 (max) Serial.println("Playing track 1..."); mp3.play(1); // file 001.mp3 } void loop() { // the module plays the track by itself, no work here }
Code Part What It Does
Serial2.begin(9600, SERIAL_8N1, 5, 23) Opens the hardware UART on D10 (RX) and D11 (TX)
mp3.begin(Serial2) Hands that port to the library
delay(1000) Waits while the module scans the SD card
mp3.setVolume(25) Sets the loudness out of 30
mp3.play(1) Starts 001.mp3

After upload, the first track plays once and the board sits idle.

Upload and Test

  1. First time with this board? Set it up with ESP32 Uno - Getting Started before you continue.
  2. Load the SD card with 001.mp3, 002.mp3 and so on, and push it into the module slot.
  3. Build the circuit by following the wiring table above.
  4. Plug in the USB Type-C cable between the ESP32 Uno and your computer.
  5. Select the board in Arduino IDE: pick ESP32 Dev Module and the COM port of the board (CH340).
  6. Paste the sketch into a new Arduino IDE window.
  7. Click Upload and wait for "Done uploading".
  8. Listen: 001.mp3 comes out of the speaker.
  9. Pro tip: a silent speaker with the message on screen usually means a missing SD card or a loose speaker wire, not a code fault. Re-seat the card first.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Playing track 1...
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Playback Commands

Function What It Does Usage
play(trackNum) Starts the given track mp3.play(1)
playNext() Jumps one track ahead mp3.playNext()
playPrevious() Moves back one track mp3.playPrevious()
pause() Holds the current track mp3.pause()
resume() Continues a held track mp3.resume()
stop() Ends playback mp3.stop()

ESP32 Uno Code - Three-Track Playlist

This sketch turns the first three files into a looping playlist with a set gap between starts.

/* * ESP32 Uno + DIYables Mini Mp3 Player - Play Multiple Tracks * * Module page: * https://diyables.io/products/mini-mp3-player-module * * Steps through a short playlist, with a fixed pause between starts. * * Connections on the ESP32 Uno: * Mp3 module RX <- D11 (GPIO23), with a 1K resistor in series * Mp3 module TX -> D10 (GPIO5) * Mp3 module VCC -> 5V * Mp3 module GND -> GND * Speaker wires -> SPK_1 and SPK_2 * * Micro SD card: the root folder holds 001.mp3, 002.mp3 and 003.mp3. */ #include <DIYables_MiniMp3.h> // the ESP32 Uno has no SoftwareSerial, so the hardware UART Serial2 is used #define MP3_RX_PIN 5 // D10 (GPIO5) listens to the module TX line #define MP3_TX_PIN 23 // D11 (GPIO23) sends commands to the module RX line DIYables_MiniMp3 mp3; int currentTrack = 1; int totalTracks = 3; // set this to the number of files on your card unsigned long lastTrackTime = 0; unsigned long trackDuration = 5000; // time in ms before the next track starts void setup() { Serial.begin(9600); Serial2.begin(9600, SERIAL_8N1, MP3_RX_PIN, MP3_TX_PIN); mp3.begin(Serial2); delay(1000); mp3.setVolume(20); Serial.println("Playing track 1..."); mp3.play(currentTrack); lastTrackTime = millis(); } void loop() { // when the wait time is over, move on to the next file if (millis() - lastTrackTime >= trackDuration) { currentTrack++; if (currentTrack > totalTracks) currentTrack = 1; // wrap around to the first file Serial.print("Playing track "); Serial.println(currentTrack); mp3.play(currentTrack); lastTrackTime = millis(); } }
Code Part What It Does
totalTracks = 3 Number of files in the playlist
trackDuration = 5000 Time in ms before the next start
millis() check Waits without blocking loop()
currentTrack > totalTracks Jumps back to track 1

The result is 1, 2, 3, 1, 2, 3 and so on, every 5 seconds.

Upload and Test

  1. Check the card holds at least 001.mp3, 002.mp3 and 003.mp3.
  2. Flash the sketch onto the board.
  3. Watch the Serial Monitor (9600 baud) for the track numbers.
  4. Pro tip: set trackDuration to the length of your longest clip in milliseconds, plus about 500 ms, so no song gets cut off.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Playing track 1... Playing track 2 Playing track 3 Playing track 1 Playing track 2
Ln 11, Col 1
ESP32 Dev Module on COM15
2

ESP32 Uno Code - Two-Button Volume Control

Here two buttons change the loudness while track 1 repeats. Wire UP to D2 (GPIO26) and DOWN to D3 (GPIO25), each with its other leg on GND.

/* * ESP32 Uno + DIYables Mini Mp3 Player - Volume Control * * Module page: * https://diyables.io/products/mini-mp3-player-module * * Two push buttons raise and lower the loudness while track 1 repeats. * * Connections on the ESP32 Uno: * Mp3 module RX <- D11 (GPIO23), with a 1K resistor in series * Mp3 module TX -> D10 (GPIO5) * Mp3 module VCC -> 5V * Mp3 module GND -> GND * Speaker wires -> SPK_1 and SPK_2 * UP button -> D2 (GPIO26), other leg to GND * DOWN button -> D3 (GPIO25), other leg to GND * * Micro SD card: mp3 files sit in the root folder as 001.mp3, 002.mp3 and so on. */ #include <DIYables_MiniMp3.h> // the ESP32 Uno has no SoftwareSerial, so the hardware UART Serial2 is used #define MP3_RX_PIN 5 // D10 (GPIO5) listens to the module TX line #define MP3_TX_PIN 23 // D11 (GPIO23) sends commands to the module RX line DIYables_MiniMp3 mp3; const int BUTTON_VOL_UP = 26; // D2 (GPIO26) volume up button const int BUTTON_VOL_DOWN = 25; // D3 (GPIO25) volume down button int volume = 15; // begin in the middle of the 0-30 range void setup() { Serial.begin(9600); Serial2.begin(9600, SERIAL_8N1, MP3_RX_PIN, MP3_TX_PIN); pinMode(BUTTON_VOL_UP, INPUT_PULLUP); pinMode(BUTTON_VOL_DOWN, INPUT_PULLUP); mp3.begin(Serial2); delay(1000); mp3.setVolume(volume); mp3.loopTrack(1); // keep track 1 repeating Serial.print("Volume: "); Serial.println(volume); } void loop() { // a pressed button reads LOW thanks to the internal pull-up if (digitalRead(BUTTON_VOL_UP) == LOW) { if (volume < 30) { volume++; mp3.setVolume(volume); Serial.print("Volume: "); Serial.println(volume); } delay(200); // short wait to skip contact bounce } // same check for the down button if (digitalRead(BUTTON_VOL_DOWN) == LOW) { if (volume > 0) { volume--; mp3.setVolume(volume); Serial.print("Volume: "); Serial.println(volume); } delay(200); // short wait to skip contact bounce } }
Code Part What It Does
INPUT_PULLUP Keeps each button HIGH until pressed
digitalRead(...) == LOW Detects a press
volume < 30 / volume > 0 Keeps the level inside 0 to 30
mp3.loopTrack(1) Repeats track 1 so you can hear each change
delay(200) Simple debounce

Each press moves the volume one step and prints the new level.

Upload and Test

  1. Fit both buttons (D2 = GPIO26, D3 = GPIO25) and click Upload.
  2. Set the Serial Monitor to 9600 baud.
  3. Press UP and DOWN while the music repeats.
  4. Pro tip: start testing at a low level. A small speaker near step 30 can distort and pull a lot of current.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Volume: 15 Volume: 14 Volume: 13 Volume: 14 Volume: 15
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Volume Commands

Function What It Does Usage
setVolume(level) Jumps straight to a level (0–30) mp3.setVolume(25)
volumeUp() One step louder mp3.volumeUp()
volumeDown() One step quieter mp3.volumeDown()
getVolume() Reads the present level mp3.getVolume()

ESP32 Uno Code - Skip Forward and Back Buttons

This sketch gives you skip-forward and skip-back buttons: NEXT on D2 (GPIO26), PREV on D3 (GPIO25).

/* * ESP32 Uno + DIYables Mini Mp3 Player - Next and Previous Buttons * * Module page: * https://diyables.io/products/mini-mp3-player-module * * Two push buttons jump forward or back in the playlist. * Each jump is printed to the Serial Monitor. * * Connections on the ESP32 Uno: * Mp3 module RX <- D11 (GPIO23), with a 1K resistor in series * Mp3 module TX -> D10 (GPIO5) * Mp3 module VCC -> 5V * Mp3 module GND -> GND * Speaker wires -> SPK_1 and SPK_2 * NEXT button -> D2 (GPIO26), other leg to GND * PREV button -> D3 (GPIO25), other leg to GND * * Micro SD card: mp3 files sit in the root folder as 001.mp3, 002.mp3 and so on. */ #include <DIYables_MiniMp3.h> // the ESP32 Uno has no SoftwareSerial, so the hardware UART Serial2 is used #define MP3_RX_PIN 5 // D10 (GPIO5) listens to the module TX line #define MP3_TX_PIN 23 // D11 (GPIO23) sends commands to the module RX line DIYables_MiniMp3 mp3; const int BUTTON_NEXT = 26; // D2 (GPIO26) next track button const int BUTTON_PREV = 25; // D3 (GPIO25) previous track button void setup() { Serial.begin(9600); Serial2.begin(9600, SERIAL_8N1, MP3_RX_PIN, MP3_TX_PIN); pinMode(BUTTON_NEXT, INPUT_PULLUP); pinMode(BUTTON_PREV, INPUT_PULLUP); mp3.begin(Serial2); delay(1000); mp3.setVolume(20); mp3.play(1); // begin at the first file Serial.println("Press NEXT or PREV button to change track"); } void loop() { if (digitalRead(BUTTON_NEXT) == LOW) { Serial.println("Next track"); mp3.playNext(); delay(300); // short wait to skip contact bounce } if (digitalRead(BUTTON_PREV) == LOW) { Serial.println("Previous track"); mp3.playPrevious(); delay(300); // short wait to skip contact bounce } }
Code Part What It Does
mp3.play(1) Starts at the first file
mp3.playNext() Runs when NEXT reads LOW
mp3.playPrevious() Runs when PREV reads LOW
delay(300) Stops one press from counting twice

Every press changes the song and logs the action.

Upload and Test

  1. Connect NEXT and PREV to D2 (GPIO26) and D3 (GPIO25).
  2. Click Upload in Arduino IDE.
  3. Press the buttons and listen to the track change.
  4. Pro tip: put a different short voice clip on each file ("one", "two", "three") to check the order quickly.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Press NEXT or PREV button to change track Next track Previous track Next track Next track
Ln 11, Col 1
ESP32 Dev Module on COM15
2

ESP32 Uno Code - One-Button Pause and Play

A single button on D2 (GPIO26) holds and restarts the music.

/* * ESP32 Uno + DIYables Mini Mp3 Player - Pause and Resume * * Module page: * https://diyables.io/products/mini-mp3-player-module * * One push button switches between pause and play. * Every press flips the state. * * Connections on the ESP32 Uno: * Mp3 module RX <- D11 (GPIO23), with a 1K resistor in series * Mp3 module TX -> D10 (GPIO5) * Mp3 module VCC -> 5V * Mp3 module GND -> GND * Speaker wires -> SPK_1 and SPK_2 * Button -> D2 (GPIO26), other leg to GND * * Micro SD card: mp3 files sit in the root folder as 001.mp3, 002.mp3 and so on. */ #include <DIYables_MiniMp3.h> // the ESP32 Uno has no SoftwareSerial, so the hardware UART Serial2 is used #define MP3_RX_PIN 5 // D10 (GPIO5) listens to the module TX line #define MP3_TX_PIN 23 // D11 (GPIO23) sends commands to the module RX line DIYables_MiniMp3 mp3; const int BUTTON_PIN = 26; // D2 (GPIO26) pause/resume button bool paused = false; void setup() { Serial.begin(9600); Serial2.begin(9600, SERIAL_8N1, MP3_RX_PIN, MP3_TX_PIN); pinMode(BUTTON_PIN, INPUT_PULLUP); mp3.begin(Serial2); delay(1000); mp3.setVolume(20); mp3.play(1); Serial.println("Playing. Press button to pause/resume."); } void loop() { if (digitalRead(BUTTON_PIN) == LOW) { if (paused) { mp3.resume(); Serial.println("Resumed"); } else { mp3.pause(); Serial.println("Paused"); } paused = !paused; delay(300); // short wait to skip contact bounce } }
Code Part What It Does
bool paused Stores the present state
mp3.pause() Runs when the music is playing
mp3.resume() Runs when the music is on hold
paused = !paused Swaps the state after each press

The track picks up from the exact spot where it stopped.

Upload and Test

  1. Wire one button to D2 (GPIO26) and GND, then upload.
  2. Press once to hold the song.
  3. Press again to continue it.
  4. Pro tip: if the player flips back and forth on one press, your button bounces a lot. Try 400 ms in place of 300 ms.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Playing. Press button to pause/resume. Paused Resumed Paused
Ln 11, Col 1
ESP32 Dev Module on COM15
2

ESP32 Uno Code - Repeat One Track Forever

Use this one for background music or an alarm: it repeats 001.mp3 with the flat EQ preset.

/* * ESP32 Uno + DIYables Mini Mp3 Player - Loop Track * * Module page: * https://diyables.io/products/mini-mp3-player-module * * Repeats one track without end, with the flat equalizer preset. * * Connections on the ESP32 Uno: * Mp3 module RX <- D11 (GPIO23), with a 1K resistor in series * Mp3 module TX -> D10 (GPIO5) * Mp3 module VCC -> 5V * Mp3 module GND -> GND * Speaker wires -> SPK_1 and SPK_2 * * Micro SD card layout: * /001.mp3 * /002.mp3 * ... */ #include <DIYables_MiniMp3.h> // the ESP32 Uno has no SoftwareSerial, so the hardware UART Serial2 is used #define MP3_RX_PIN 5 // D10 (GPIO5) listens to the module TX line #define MP3_TX_PIN 23 // D11 (GPIO23) sends commands to the module RX line DIYables_MiniMp3 mp3; void setup() { Serial.begin(9600); Serial2.begin(9600, SERIAL_8N1, MP3_RX_PIN, MP3_TX_PIN); mp3.begin(Serial2); delay(1000); // give the module time to read the SD card mp3.setVolume(25); // loudness from 0 to 30 mp3.setEQ(DIYables_MiniMp3::EQ_NORMAL); Serial.println("Playing track 1 on loop..."); mp3.loopTrack(1); } void loop() { // free for your own code, the module repeats the track alone }
Code Part What It Does
mp3.setEQ(EQ_NORMAL) Picks the flat sound profile
mp3.loopTrack(1) Tells the module to repeat 001.mp3
Empty loop() Free for your own code

The module handles the repeat by itself, so the ESP32 Uno stays free for other jobs.

Upload and Test

  1. Flash this code onto the board.
  2. Listen: 001.mp3 restarts every time it ends.
  3. Pro tip: for a seamless loop, trim the silence at the start and end of the mp3 file in an audio editor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Playing track 1 on loop...
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Repeat and Shuffle Commands

Function What It Does Usage
loopTrack(trackNum) Repeats one track with no end mp3.loopTrack(1)
loopFolder(folder) Repeats every file in one folder mp3.loopFolder(1)
loopAll() Repeats the whole card mp3.loopAll()
stopLoop() Ends any repeat mode mp3.stopLoop()
shuffle() Plays the whole card in random order mp3.shuffle()

ESP32 Uno Code - Numbered Folder Playback

This sketch pulls clips from folder 01 and folder 02, which is handy when you keep music and voice prompts apart.

/* * ESP32 Uno + DIYables Mini Mp3 Player - Play from Folder * * Module page: * https://diyables.io/products/mini-mp3-player-module * * Picks tracks from numbered folders on the SD card. * * Connections on the ESP32 Uno: * Mp3 module RX <- D11 (GPIO23), with a 1K resistor in series * Mp3 module TX -> D10 (GPIO5) * Mp3 module VCC -> 5V * Mp3 module GND -> GND * Speaker wires -> SPK_1 and SPK_2 * * Micro SD card layout: * /01/001.mp3 <- playFolder(1, 1) * /01/002.mp3 <- playFolder(1, 2) * /02/001.mp3 <- playFolder(2, 1) * /02/002.mp3 <- playFolder(2, 2) * * Keep in mind: * - counting begins at 1, never 0 * - folder names have 2 digits (01-99) * - file names have 3 digits (001-255) * - format the card as FAT32 first, then copy the files one at a time * - the module sorts files by copy order, not by name, * so copy them in the order you want to hear them */ #include <DIYables_MiniMp3.h> // the ESP32 Uno has no SoftwareSerial, so the hardware UART Serial2 is used #define MP3_RX_PIN 5 // D10 (GPIO5) listens to the module TX line #define MP3_TX_PIN 23 // D11 (GPIO23) sends commands to the module RX line DIYables_MiniMp3 mp3; void setup() { Serial.begin(9600); Serial2.begin(9600, SERIAL_8N1, MP3_RX_PIN, MP3_TX_PIN); mp3.begin(Serial2); delay(1000); mp3.setVolume(20); // folder 01, first file Serial.println("Playing folder 01, track 001..."); mp3.playFolder(1, 1); delay(5000); // folder 01, second file Serial.println("Playing folder 01, track 002..."); mp3.playFolder(1, 2); delay(5000); // folder 02, first file Serial.println("Playing folder 02, track 001..."); mp3.playFolder(2, 1); } void loop() { // the demo runs only once, so loop() stays empty }
Code Part What It Does
mp3.playFolder(1, 1) Plays /01/001.mp3
mp3.playFolder(1, 2) Plays /01/002.mp3
mp3.playFolder(2, 1) Plays /02/001.mp3
delay(5000) Gap between the clips

Three clips play once, then the sketch stops.

Upload and Test

  1. Make folders 01 and 02 on the card, each with 001.mp3 and 002.mp3.
  2. Upload this code.
  3. Listen to the three clips in order.
  4. Pro tip: name your folders by theme in a text note on your PC (01 = music, 02 = alarms) so you remember what each number holds.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Playing folder 01, track 001... Playing folder 01, track 002... Playing folder 02, track 001...
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Folder Commands

Function What It Does Usage
playFolder(folder, track) Plays a file from a folder (99 folders, 255 files each) mp3.playFolder(1, 1)
playLargeFolder(folder, track) Plays from a big folder (15 folders, 3000 files each) mp3.playLargeFolder(1, 1500)
playFromMP3Folder(trackNum) Picks a numbered file inside /mp3 mp3.playFromMP3Folder(1)

ESP32 Uno Code - Typed Commands in the Serial Monitor

No buttons needed here: type one-character commands in the Serial Monitor to test every feature.

/* * ESP32 Uno + DIYables Mini Mp3 Player - Serial Command Control * * Module page: * https://diyables.io/products/mini-mp3-player-module * * Type short commands in the Serial Monitor to drive the player. * Handy to try every feature with no buttons at all. * * Command list (type one, then press Enter): * 1-9 start track 1 to 9 * + louder * - quieter * p pause * r resume * s stop * n next track * b previous track (back) * ? print the player status * * Connections on the ESP32 Uno: * Mp3 module RX <- D11 (GPIO23), with a 1K resistor in series * Mp3 module TX -> D10 (GPIO5) * Mp3 module VCC -> 5V * Mp3 module GND -> GND * Speaker wires -> SPK_1 and SPK_2 * * Micro SD card: mp3 files sit in the root folder as 001.mp3, 002.mp3 and so on. */ #include <DIYables_MiniMp3.h> // the ESP32 Uno has no SoftwareSerial, so the hardware UART Serial2 is used #define MP3_RX_PIN 5 // D10 (GPIO5) listens to the module TX line #define MP3_TX_PIN 23 // D11 (GPIO23) sends commands to the module RX line DIYables_MiniMp3 mp3; void setup() { Serial.begin(9600); Serial2.begin(9600, SERIAL_8N1, MP3_RX_PIN, MP3_TX_PIN); mp3.begin(Serial2); delay(1000); mp3.setVolume(20); Serial.println("=== DIYables Mini Mp3 Player ==="); Serial.println("Commands:"); Serial.println(" 1-9 Play track number"); Serial.println(" + Volume up"); Serial.println(" - Volume down"); Serial.println(" p Pause"); Serial.println(" r Resume"); Serial.println(" s Stop"); Serial.println(" n Next track"); Serial.println(" b Previous track"); Serial.println(" ? Show status"); Serial.println("================================"); } void loop() { if (Serial.available()) { char cmd = Serial.read(); switch (cmd) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': Serial.print("Playing track "); Serial.println(cmd - '0'); mp3.play(cmd - '0'); break; case '+': Serial.println("Volume up"); mp3.volumeUp(); break; case '-': Serial.println("Volume down"); mp3.volumeDown(); break; case 'p': Serial.println("Paused"); mp3.pause(); break; case 'r': Serial.println("Resumed"); mp3.resume(); break; case 's': Serial.println("Stopped"); mp3.stop(); break; case 'n': Serial.println("Next track"); mp3.playNext(); break; case 'b': Serial.println("Previous track"); mp3.playPrevious(); break; case '?': { Serial.println("--- Status ---"); int16_t vol = mp3.getVolume(); Serial.print("Volume: "); Serial.println(vol); int16_t track = mp3.getCurrentTrack(); Serial.print("Current track: "); Serial.println(track); bool playing = mp3.isPlaying(); Serial.print("Playing: "); Serial.println(playing ? "Yes" : "No"); int16_t total = mp3.getTrackCount(); Serial.print("Total tracks: "); Serial.println(total); Serial.println("--------------"); break; } default: break; } } }
Code Part What It Does
Serial.available() Checks for a typed character
switch (cmd) Picks the action for that character
cmd - '0' Turns the digit into a track number
case '?' Asks the module for volume, track, state and count

Each command runs at once and prints a short confirmation.

Upload and Test

  1. Upload the sketch, then open the Serial Monitor at 9600 baud.
  2. Send a command from this list:
Key Action
1–9 Start track 1 to 9
+ Louder
− Quieter
p Pause
r Resume
s Stop
n Next track
b Previous track
? Show the status
  1. Pro tip: you can type several keys in one line, like "3++?", and the sketch runs them one after another.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
=== DIYables Mini Mp3 Player === Playing track 4 Volume up Volume up --- Status --- Volume: 22 Current track: 4 Playing: Yes Total tracks: 7 --------------
Ln 11, Col 1
ESP32 Dev Module on COM15
2

ESP32 Uno vs UNO R4 for This Project

This table shows what changes when you move the mp3 player build from the UNO R4 to the ESP32 Uno.

Item Arduino UNO R4 ESP32 Uno
Logic voltage 5V 3.3V
Serial link to the module SoftwareSerial on pins 10 and 11 Hardware Serial2 on GPIO5 and GPIO23
Module TX wire D10 D10 (GPIO5)
Module RX wire D11 D11 (GPIO23)
Button pins D2 and D3 D2 (GPIO26) and D3 (GPIO25)
Arduino IDE board Arduino UNO R4 WiFi or Minima ESP32 Dev Module
WiFi and Bluetooth WiFi only on the R4 WiFi WiFi and Bluetooth Classic and BLE

The wires stay on the same header positions, so the same breadboard layout works. Only the code changes: drop SoftwareSerial and use Serial2 with GPIO5 and GPIO23.

Equalizer Presets

Use this part to change the tone of the sound with one line of code.

Constant ID Sound Profile
DIYables_MiniMp3::EQ_NORMAL 0 Flat
DIYables_MiniMp3::EQ_POP 1 Pop
DIYables_MiniMp3::EQ_ROCK 2 Rock
DIYables_MiniMp3::EQ_JAZZ 3 Jazz
DIYables_MiniMp3::EQ_CLASSIC 4 Classical
DIYables_MiniMp3::EQ_BASS 5 Extra low end
mp3.setEQ(DIYables_MiniMp3::EQ_ROCK);

Reading the Player Status

With these calls, your ESP32 Uno code can react to the player, for example start a new song only when the old one ends.

A query can block for as long as 100 ms. A result of −1 means the module did not reply in time or sent an error.

Function Return Type Result
isPlaying() bool Returns true during playback
getVolume() int16_t Present volume (0–30)
getEQ() int16_t Present EQ preset (0–5)
getTrackCount() int16_t Files on the SD card
getCurrentTrack() int16_t Number of the running track
getFolderCount() int16_t Folders on the SD card
getTrackCountInFolder(folder) int16_t Files in one folder

Go Wireless: A Phone Remote for Your Mp3 Player

This idea turns the ESP32 Uno into a small music box you control from a phone.

The ESP32 Uno has WiFi and Bluetooth built in, so the buttons can move to a screen. A slider in a web page or the Bluetooth app can set the volume from 0 to 30, and a text box can send the same one-letter commands as the Serial Monitor sketch. You can also show the current track and volume on the phone. Keep the module on Serial2 (GPIO5 and GPIO23) as in the main code.

Troubleshooting

Problem Possible Cause Solution
No COM port shows up in Arduino IDE CH340 driver missing or charge-only cable Install the CH340 driver and use a USB cable that carries data
Upload stops at "Waiting for download" Board did not enter flash mode Hold the BOOT button while the upload starts, then let go
Board does not boot or upload with the module connected D10 (GPIO5) is a boot strapping pin and was pulled the wrong way Unplug the TX wire from D10 while uploading, then reconnect it
Speaker is silent and nothing is printed Wrong board or port, or the sketch did not upload Pick ESP32 Dev Module and the CH340 port, then upload again
Message prints but no sound Card missing, card in exFAT, or speaker loose Format the card as FAT32, re-seat it and check SPK_1 and SPK_2
Module ignores every command TX and RX lines crossed the wrong way Module TX goes to D10 (GPIO5), module RX goes to D11 (GPIO23) through 1K
Strong buzz or hiss No series resistor on RX or a weak supply Fit the 1K resistor and feed VCC from the 5V pin
ESP32 Uno restarts at high volume Speaker draws too much current from USB Lower the volume, use a 3W or smaller speaker, or a stronger 5V supply
Compile error SoftwareSerial.h not found Sketch written for another board Use the Serial2 sketches from this page
Status calls give −1 Query sent while the module was busy Wait a moment after a command and check the D10 wire

FAQ

Is the YX5200 module the same as a DFPlayer Mini?

They use the same YX5200 decoder chip and the same command set, so most DFPlayer Mini guides apply. The DIYables board has its own pin layout, so follow the pinout table on this page when you wire it.

How loud can this mp3 module get with the ESP32 Uno?

The on-board amplifier gives up to 3W into a small speaker, which fills a room. For more power, take the line signal from DAC_L and DAC_R and feed it into an external amplifier.

Which audio files does the module play?

It plays mp3 files (and WAV on many units) from a micro SD card formatted as FAT16 or FAT32. Give the files numbered names like 001.mp3, and keep folder names to two digits.

Do I need a level shifter between the module and the ESP32 Uno?

No. The YX5200 sends 3.3V logic on its TX pin even when VCC is 5V, so it is safe for the ESP32 Uno 3.3V input. The 1K series resistor on module RX still helps against noise.

Why does the ESP32 Uno code use Serial2 and GPIO5 and GPIO23?

The ESP32 Uno core has no SoftwareSerial, but the chip has spare hardware UARTs that can use almost any pin. Serial2 is set to GPIO5 (D10) for RX and GPIO23 (D11) for TX, so the wires stay on the same header spots as the wiring diagram.

Can I run an UNO R4 mp3 sketch on the ESP32 Uno without changes?

No. An UNO R4 sketch uses SoftwareSerial with pin numbers 10 and 11, which fails to compile on the ESP32 Uno. Remove SoftwareSerial, start Serial2 with GPIO5 and GPIO23, and pass Serial2 to mp3.begin(). The library calls stay the same.

Is it a problem that D10 is a strapping pin on the ESP32 Uno?

Usually not. GPIO5 must be HIGH at boot, and the module TX line rests HIGH. If a board still fails to start or upload, unplug the D10 wire during the upload and plug it back in afterward.

Can I wire the push buttons to A2 to A5 on the ESP32 Uno?

You can, but A2 to A5 (GPIO35, GPIO34, GPIO36, GPIO39) are input only and have no internal pull-up. INPUT_PULLUP does nothing there, so each button needs an external 10kΩ resistor to 3.3V. D2 and D3 are simpler because their pull-up is built in.

What if my project needs more pins than the Uno headers give?

The ESP32 Uno has extra holes near the power header: GPIO32 and GPIO33 (I/O, PWM, analog on ADC1, so they also work while WiFi is on) and GPIO15 (I/O, PWM, analog on ADC2). Solder pin headers there and write the GPIO number in code. For more buttons or a BUSY pin input in this project, use GPIO32 and GPIO33 first. Two warnings: GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC spot) are boot strapping pins, so a wrong connection can stop the board from booting or uploading. And never use the six holes under the digital header (SD0 to SD3, CMD, CLK = GPIO6 to GPIO11), because they connect to the flash chip and using them crashes the board.

How can I tell when a song has finished?

Call isPlaying() in loop() and start the next track when it returns false. Another way is to wire the module BUSY pin to a free GPIO, because BUSY goes HIGH when playback stops.

Can I control the mp3 player from my phone with the ESP32 Uno?

Yes. The ESP32 Uno has WiFi and Bluetooth, so a web page or the DIYables Bluetooth app can send volume and track commands. See the Go Wireless section above for matching tutorials.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!