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.

Required Components

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.

Schematics

The schematics for this project is fairly easy

Schematics

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.

Code

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

///              MP3 PLAYER PROJECT
/// http://educ8s.tv/arduino-mp3-player/
//////////////////////////////////////////


#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);

Next, we define some of the commands that we will use specifying the hex values.

# 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]

Next, we declare the pins of the Arduino to which the push buttons are connected.

int buttonNext = 2;
int buttonPause = 3;
int buttonPrevious = 4;

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.

void setup () {

pinMode(buttonPause, INPUT);
digitalWrite(buttonPause,HIGH);
pinMode(buttonNext, INPUT);
digitalWrite(buttonNext,HIGH);
pinMode(buttonPrevious, INPUT);
digitalWrite(buttonPrevious,HIGH);

mySerial.begin (9600);

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.

delay(1000);
playFirst();
isPlaying = true;

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.

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();
    }
  }
}

The complete code for the project is available below and can be downloaded from the download section of this tutorial.

///              MP3 PLAYER PROJECT
/// http://educ8s.tv/arduino-mp3-player/
//////////////////////////////////////////


#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]);
}
}

Demo

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.

Hardware Setup

Some of the applications of this tutorials are listed below and I hope it gives you the inspiration to build something really cool.

Applications

  1. Fire alarm voice prompts
  2. Toll stations voice prompts
  3. Electricity, communications, financial business hall voice prompts
  4. 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.

Downloads

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

RELATED PROJECTS