MP3 player using Arduino and DFPlayer mini
Hi guys, welcome to this tutorial. Today, we will build an mp3 player using an Arduino and the DFPlayer mini MP3 module.
The DFplayer mini is a small, low-cost mp3 module with a simplified audio output that can be connected directly to a speaker or an earphone jack. The module can be used as a stand-alone module with attached battery, speaker, and push buttons or used in combination with a microcontroller or development board like the Arduino, enabled for RX/TX (Serial) communication, thus through simple serial commands we can play music and perform other functions like playing the next and previous song, shuffle, pause the song currently being played etc. The module comes with an SDcard slot and supports both FAT16, FAT32 file system.
Some of the features of the DF player mini include:
- Support of sampling rate of 8KHz, 11.025KHz, 12KHz, 16KHz, 22.05KHz, up to 48KHz
- 24-bit DAC output, dynamic range support 90dB, SNR supports 85dB
- Supports FAT16, FAT32 file system, maximum support 32GB TF card
- A variety of control modes, serial mode, AD key control mode
- The broadcast language spots feature, you can pause the background music being played
- Built-in 3W amplifier
- The audio data is sorted by folder; supports up to 100 folders, each folder can be assigned to close to 1000 songs
- 30 levels of volume adjustable, 10 levels EQ adjustable.
The goal of this tutorial is to demonstrate the use of this module with Arduino, using the push buttons to instruct the Arduino to send serial commands to the module and control mp3. We will use three buttons to achieve this. The first button will serve as the “previous” button that will enable us to play the previous song. The second button will be to “play/pause” the file currently being played and the third button will be used to play the next file.
The following components are required to build this project;
As usual, each of this components can be bought via the link attached to them in the list above.
The schematics for this project is fairly easy
As seen above the connection between the Arduino and the DFplayer mini is very simple as we only need to connect two pins aside VCC and GND. It should be noted that the 1k resistor added in between the Rx pin of the module and the Arduino was added to reduce noise but it’s not necessary if your module setup is not accompanied with noise on the Rx line. The connection is described below for clarity.
DFplayer Mini - Arduino Rx - D11 Tx - D10 VCC - 5v Gnd - Gnd
Go over the connections once again to ensure everything is as it should be. Our switches are intentionally connected without pull up (or down) resistors because we will enable the Arduino internal pull up resistors.
To send commands from the Arduino to the DFplayer mini, based on the button pressed, we will use the Arduino software serial library. While we could have used the hardware serial to send commands from the Arduino to the DFplayermini, the Arduino hardware serial pins (0 and 1) are the same pins used by the Arduino to communicate with the computer and may prevent code to uploaded smoothly if connected to any other device. So to communicate in a stress-free manner we will use the software serial library with pins 10 and 11.
The code for this tutorial is simple, while the DFPlayer mini has a library which contains different functions for controlling the mp3 player, we will write our own functions for the fun of it and to help show how the module really works.
The first thing we do in the code, as usual, is to include the libraries that we will use which in this case is the software serial library, creating an object of the library while declaring the Rx and Tx pins (10 and 11 respectively).
Next, we define some of the commands that we will use specifying the hex values.
Next, we declare the pins of the Arduino to which the push buttons are connected.
Next, we move to the void setup function where we set the pin mode of the pins to which the buttons are connected and set those pins “high”. With this done we start the software serial communication with a 9600 baud rate.
Next, we play the first song on the storage device using the playfirst() function and set the isPlaying boolean variable to true to indicate that one of the files is currently playing.
Moving to the void loop function, this function basically checks for the button press and sends the corresponding command to the DFPlayer mini which then either plays (depending on the value of isplaying variable) or pause the current song, or go to the next or previous song.
The complete code for the project is available below and can be downloaded from the download section of this tutorial.
Load an SD card with songs and insert into the DFplayer mini, then upload the code to your Arduino and connect the wires from speaker to the speaker pins of the DFPlayer mini. You should hear songs start streaming out from the connected speaker. Your final setup should look like the image below.
Some of the applications of this tutorials are listed below and I hope it gives you the inspiration to build something really cool.
- Fire alarm voice prompts
- Toll stations voice prompts
- Electricity, communications, financial business hall voice prompts
- Multi-channel voice alarm or equipment operating guide voice
and so on.
That’s it for this tutorial guys, thanks for following. If you get stuck anywhere feel free to send your questions via the comment section.
Till next time!
The video tutorial for this tutorial can be watched on youtube here.