Arduino UNO R4 - MP3 Player

This tutorial instructs you how to create an MP3 player using an Arduino UNO R4, an MP3 player module, a Micro SD Card, and a speaker. The MP3 player loads music or audio recordings from the Micro SD Card. The Arduino UNO R4 controls the MP3 player module to pick and play a song from the card, turn it into an audio signal, and send this signal to the speaker. We will cover these steps in detail.

Then, you can change the code to include a potentiometer or rotary encoder to adjust the volume.

Arduino UNO R4 mp3 player

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Serial MP3 Player Module
1×Micro SD Card
1×3.5mm Aux Speaker
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4

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 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.

Overview of Serial MP3 Player Module and Speaker

Serial MP3 Player Module Pinout

A serial MP3 player module includes three interfaces:

  • The interface to the Arduino UNO R4: has four pins:
    • RX pin: This is for data and should connect to the TX pin on the Arduino UNO R4, using either Hardware or Software Serial.
    • TX pin: This is also for data and should connect to the RX pin on the Arduino UNO R4, through Hardware or Software Serial.
    • VCC pin: This is for power and should be connected to VCC (5V).
    • GND pin: This is a ground pin and should be connected to GND (0V).
  • The interface to the speaker: a 3.5mm Aux output female jack.
  • The interface to the Micro SD Card: The Micro SD Card socket located at the back of the module.
Serial MP3 Player Module Pinout
image source: diyables.io

Speaker Pinout

A speaker generally has two connection points:

  • Audio connection: It uses a 3.5mm Aux male connector to connect to the MP3 player.
  • Power connection: It can use a USB, a 5V power adapter, or other types of power connections.

How It Works

What we need to get ready:

  • Save a list of songs or recordings on a micro SD card.
  • Place the micro SD card into the MP3 player module.
  • Connect the MP3 player module to the Arduino UNO R4.
  • Connect the MP3 player module to a speaker.
  • Connect the speaker to a power source.

Every MP3 file on the Micro SD Card has a number starting from 0 that shows the order of the songs.

We can then set up the Arduino UNO R4 to send commands to the MP3 player module. It can handle these commands:

  • Start Playing
  • Stop
  • Play Next
  • Play Previous
  • Adjust Volume

The MP3 player module plays the MP3 file stored on the micro SD card, transforms it into an audio signal, and sends this signal to the speaker through the 3.5mm Aux interface.

Wiring Diagram

The wiring diagram between Arduino UNO R4 MP3 player module

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code - Play Music

The code below starts playing the first song saved on the Micro SD Card.

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-mp3-player */ #include <SoftwareSerial.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 #define ARDUINO_RX 7 // The Arduino UNO R4 pin connected to the TX of the Serial MP3 Player module #define ARDUINO_TX 6 // The Arduino UNO R4 pin connected to the RX of the Serial MP3 Player module SoftwareSerial mp3(ARDUINO_RX, ARDUINO_TX); void setup() { Serial.begin(9600); mp3.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++) { mp3.write(frame[i]); } }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Follow the steps in the section How It Works
  • Copy the code provided and use it in Arduino IDE
  • Press the Upload button in Arduino IDE to send the code to Arduino UNO R4
  • Have fun with the music

Arduino UNO R4 Code - Play Music with control buttons

The code below is an improved version of the earlier code. It includes four buttons that allow you to control the MP3 player.

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-mp3-player */ #include <SoftwareSerial.h> #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 #define ARDUINO_RX 7 // The Arduino UNO R4 pin connected to the TX of the Serial MP3 Player module #define ARDUINO_TX 6 // The Arduino UNO R4 pin connected to the RX of the Serial MP3 Player module SoftwareSerial mp3(ARDUINO_RX, ARDUINO_TX); ezButton button_play(2); // create ezButton object that attach to pin 2 ezButton button_pause(3); // create ezButton object that attach to pin 3 ezButton button_next(4); // create ezButton object that attach to pin 4 ezButton button_prev(5); // create ezButton object that attach to pin 5 void setup() { Serial.begin(9600); mp3.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++) { mp3.write(frame[i]); } }

The wiring connections for the mentioned code:

The wiring diagram between Arduino UNO R4 MP3 player speaker

This image is created using Fritzing. Click to enlarge image

Now, you can change the projects to include more features, for example:

Video Tutorial

Function References

※ 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!