Introduction: MP3 Player With Arduino Using DF Player 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.

Step 1: Things You Need

The following components are required to build this project;

DFPlayer Mini


Arduino Uno

Breadboard

Push Buttons

Speaker

Resistor


Jumper wires

Step 2: Features of DF Player Mini

Some of the features of the DF player mini include:Support of sampling rate of 8KHz, 11.025KHz, 12KHz, 16KHz, 22.05KHz, up to 48KHz24-bit DAC output, dynamic range support 90dB, SNR supports 85dBSupports FAT16, FAT32 file system, maximum support 32GB TF cardA variety of control modes, serial mode, AD key control modeThe broadcast language spots feature, you can pause the background music being playedBuilt-in 3W amplifierThe audio data is sorted by folder; supports up to 100 folders, each folder can be assigned to close to 1000 songs30 levels of volume adjustable, 10 levels EQ adjustable.

Step 3: Schmatics

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

Step 4: Code

Please copy the following code and upload it to your arduino Board :

//////////////////////////////////////////
#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
# define ACTIVATED LOW
int buttonNext = 2;
int buttonPause = 3;
int buttonPrevious = 4;
boolean isPlaying = false;
void setup () {
pinMode(buttonPause, INPUT);
digitalWrite(buttonPause,HIGH);
pinMode(buttonNext, INPUT);
digitalWrite(buttonNext,HIGH);
pinMode(buttonPrevious, INPUT);
digitalWrite(buttonPrevious,HIGH);
mySerial.begin (9600);
delay(1000);
playFirst();
isPlaying = true;
}
void loop () {
if (digitalRead(buttonPause) == ACTIVATED)
{
if(isPlaying)
{
pause();
isPlaying = false;
}else
{
isPlaying = true;
play();
}
}
if (digitalRead(buttonNext) == ACTIVATED)
{
if(isPlaying)
{
playNext();
}
}
if (digitalRead(buttonPrevious) == ACTIVATED)
{
if(isPlaying)
{
playPrevious();
}
}
}
void playFirst()
{
execute_CMD(0x3F, 0, 0);
delay(500);
setVolume(20);
delay(500);
execute_CMD(0x11,0,1);
delay(500);
}
void pause()
{
execute_CMD(0x0E,0,0);
delay(500);
}
void play()
{
execute_CMD(0x0D,0,1);
delay(500);
}
void playNext()
{
execute_CMD(0x01,0,1);
delay(500);
}
void playPrevious()
{
execute_CMD(0x02,0,1);
delay(500);
}
void setVolume(int volume)
{
execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
delay(2000);
}
void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}

Step 5: Playing Mp3

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