MP3 Player using DFPlayer and Arduino.

dfplayer with arduino mp3 player

The DFPlayer mini module can be used where you want to include sound or music in your projects and in this tutorial i will show you how to interface this module with Arduino.

DFPlayer mini module description.

The DFPlayer mini is a small low cost mp3 module that can directly connected to a speaker. It can be used as a stand alone module with attached battery, speaker and push buttons or with a microcontroller like in our case, the Arduino.

dfplayer mini module pin out

Specifications.

The DFPlayer mini module has an in-build amplifier which can drive up to 3 watt loudspeakers in stereo or mono. It has 24-bit digital to analog converter (DAC)
  • Has a micro SD card slot and can support FAT16 and FAT32 file system with a maximum of 32GB for a TF card, 32GB for a U disk and 64Mbs for NORFLASH
  • It supports MP3 and WMV hardware decoding and a sampling rate of 8KHz,11.025KHz, 12KHz,1 6KHz, 22.05KHz, 24KHz, 32KHz, 44.1KHz, 48KHz.
  • Has a 30 level adjustable volume and 6 -level EQ adjustable

There are many other specifications for this module which you can look up in the DFPlayer mini module data sheet .From this data sheet you will also find the different commands for serial communication.

Interfacing the DFPlayer mini module with Arduino.

This mp3 player module can be connected to the Arduino as illustrated below. We connect a speaker to pins SPK_1 AND SPK_2 which will output the sound fromthe micro SD card or TFcard inserted into the DFPlayer module.

i). Don’t forget connecting a 1k resistor between Arduino Tx and the MP3 module RX otherwise you will have poor sound output.
ii). When storing audio files in the SD card, the file names should begin with a four-digit number, for example, the first audio file should be named as 0001.mp3. The name can even have other characters but must begin with the four numbers. The second audio can be 0002_A dusty road.mp3.

Code for playing audio files on the memory card.

For the simple control of the DFPlayer using Arduino you will need to install the DFPlayer_Mini_Mp3.h library and for this library to work properly you also need to install the DFRobot_utility.h library.

#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
 
void setup () {
 Serial.begin (9600);
 mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module 
 delay(1);
 mp3_set_volume (10);  // set volume of speaker (0~30)
}
 
void loop () {        
 
 mp3_play (1); //play 0001.mp3
 delay (10000); //10 sec, time delay to allow 0001.mp3 to finish playing
 
 mp3_play (2);
 delay (5000);
 
 mp3_play (3);
 delay (5000);
 
 mp3_play (4); //play 0004.mp3
 delay (9000);
 
 mp3_play (5);
 delay (6000);
  
}

The main function is mp3_play () which plays a specified audio file. For example mp3_play(1) plays the audio file 0001***.mp3 which is the first file.

MP3 Player using DFPlayer mini with Arduino.

Controlling mp3 player with buttons.

After learning the basics of how the DFPlayer mini module works and how to use it with Arduino we can be able to do a more intersting project. We can build a simple mp3 player using this module.

The mp3 player is set up as shown in the schematic below. I have included five buttons to control the player. These buttons are for play or pause, next song, previous song, volume increase and volume decrease.

mp3 player using dfplayer mini and arduino schematic

Code for mp3 player with buttons.

Since the DFPlayer mini and Arduino communicate via serial, we need to use the SoftwareSerial.h library which comes installed in the Arduino IDE. This library enable us to use software serial communication because the hardware serial (using pins D0 and D1) can produce errors when uploading code.

#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;
int volumeUP = 5;
int volumeDOWN = 6;
int volume = 15;
boolean isPlaying = false;



void setup () {

pinMode(buttonPause, INPUT);
digitalWrite(buttonPause,HIGH);
pinMode(buttonNext, INPUT);
digitalWrite(buttonNext,HIGH);
pinMode(buttonPrevious, INPUT);
digitalWrite(buttonPrevious,HIGH);
pinMode(volumeUP, INPUT);
pinMode(volumeDOWN, INPUT);
digitalWrite(volumeUP, HIGH);
digitalWrite(volumeDOWN, 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();
    }
  }

  if(digitalRead(volumeUP) == ACTIVATED)
{
volumeINC();
}
if(digitalRead(volumeDOWN) == ACTIVATED)
{
volumeDEC();
}
}

void playFirst()
{
execute_CMD(0x3F, 0, 0);
delay(500);
execute_CMD(0x06, 0, volume);
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 volumeINC()
{
volume = volume+1;
if(volume==31)
{
volume=30;
}
execute_CMD(0x06, 0, volume);
delay(500);
}
void volumeDEC()
{
volume = volume-1;
if(volume==-1)
{
volume=0;
}
execute_CMD(0x06, 0, volume);
delay(500);
}

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

Controlling the mp3 player using IR remote.

In case you want to be able to control the MP3 player from a distance then you may need another way other than using push buttons. For example we can an IR remote control whose set up will be done as shown below.

mp3 player ir remote control

From the schematic, we remove the push buttons and instead include a TSOP1838 IR receiver whose signal pin is attached to pin 7 of the Arduino.

If you do not know how the IR receiver and remote control work you can make reference to my other tutorial :

  • Interfacing an IR remote with Arduino.
  • From the above post you can be able to decode any remote control device you want to use. Then decide which buttons you will use for controlling the mp3 player.

    Code for IR remote controlled mp3 player.

    This code is for enabling control of the player using a remote control. There are six functions namely; pause and play, next song, previous song, volume increase , volume decrease and sound equalizer (Normal/Pop/Rock/Jazz/Classic/Base).

    #include <SoftwareSerial.h>
    #include <IRremote.h>
    SoftwareSerial mySerial(10,11);
    # define Start_Byte 0x7E
    # define Version_Byte 0xFF
    # define Command_Length 0x06
    # define End_Byte 0xEF
    # define Acknowledge 0x00
    //--------------------------------------------------------//
    # define pause_play 16724175  //code received from button no. 1
    # define next_song 16718055   //code received from button no. 2
    # define prev_song 16743045   //code received from button no. 3
    # define vol_inc 16716015     //code received from button no. 4
    # define vol_dec 16726215    //code received from button no. 5
    # define sound_equalizer 16734885 //code received from button no. 6
    //-------------------------------------------------------//
    const int receive = 7;
    IRrecv irrecv(receive);
    decode_results dec;
    int volume = 15;
    int eqset = 0;
    boolean Playing = false;
    
    void setup ()
    {
    irrecv.enableIRIn();
    mySerial.begin(9600);
    delay(1000);
    playFirst();
    Playing = true;
    }
    
    void loop ()
    {
    if(irrecv.decode(&dec))
    {
    if (dec.value==pause_play)
    {
    if(Playing)
    {
    pause();
    Playing = false;
    }
    else
    {
    Playing = true;
    play();
    }
    }
    if (dec.value==next_song)
    {
    if(Playing)
    {
    next();
    }
    }
    if (dec.value==prev_song)
    {
    if(Playing)
    {
    previous();
    }
    }
    if(dec.value==vol_inc)
    {
    volumeINC();
    }
    if(dec.value==vol_dec)
    {
    volumeDEC();
    }
    if(dec.value==sound_equalizer)
    {
    equalizer();
    }
    irrecv.resume();
    }
    }
    
    void playFirst()
    {
    exe_cmd(0x3F, 0, 0);
    delay(100);
    exe_cmd(0x06, 0, volume);
    delay(100);
    exe_cmd(0x11,0,1);
    delay(100);
    }
    
    void pause()
    {
    exe_cmd(0x0E,0,0);
    delay(100);
    }
    
    void play()
    {
    exe_cmd(0x0D,0,1);
    delay(100);
    }
    
    void next()
    {
    exe_cmd(0x01,0,1);
    delay(100);
    }
    
    void previous()
    {
    exe_cmd(0x02,0,1);
    delay(100);
    }
    void volumeINC()
    {
    volume = volume+1;
    if(volume == 31)
    {
    volume = 30;
    }
    exe_cmd(0x06, 0, volume);
    delay(100);
    }
    
    void volumeDEC()
    {
    volume = volume-1;
    if(volume == -1)
    {
    volume = 0;
    }
    exe_cmd(0x06, 0, volume);
    delay(100);
    }
    
    void equalizer()
    {
    eqset = eqset+1;
    if(eqset == 6)
    {
    eqset = 0;
    }
    exe_cmd(0x07, 0 ,eqset);
    delay(100);
    }
    
    void exe_cmd(byte CMD, byte Par1, byte Par2)
    {
    word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
    byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge, Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
    for (byte x=0; x<10; x++)
    {
    mySerial.write(Command_line[x]);
    }
    }
    

    You may also like...