Skip to main content

How to use DFPlayer Pro to play Music

What is a DFPlayer?

 11/2024 Update
 A new library added below.
For people trying to avoid delay(), please use the DFPlayerMini_Fast library instead.

The DFPlayer Mini MP3 Player For ArduinoPro is a smallmini, simple but powerful MP3 Player. It supports four controlling modes: Arduino, AT command, on-board buttons, and low-pricedADKEY. MP3You modulecan withdirectly press the on-board button to play or switch music without using a simplifiedcontroller. outputBy directlyusing toa theUSB speaker.cable, The moduleyou can beeasily usedcopy your audio files into this module, or use it as a stand-alonesound modulecard withfor anyour attached battery, speaker and push buttonsPC or usedRaspberry inPi combinationafter withconnecting anthem Arduino UNO or any other with RX/TX capabilities.together. Know More

dfplayer.jpg

We have a tutorial about how to use DFplayer Mini here.

Upgrade from DFPlayer Mini

FeatureDFPlayer MiniDFPlayer ProUpgrade Benefit
Audio Output QualityMono, lower-quality (distortion at high volume)Stereo output, higher-quality audio✅ Cleaner, richer sound; suitable for higher-end projects
DAC (Digital-Analog Converter)Basic DACHigh-quality DAC✅ Better audio fidelity
Storage SupportMicroSD only (up to 32GB FAT32)MicroSD (up to 32GB FAT32) + 128MB internal flash✅ Internal storage removes reliance on SD card
Flash Memory Support❌ None✅ Yes, built-in 128MB flash✅ More durable, faster access, no SD card needed
Serial Communication9600 baud fixed115200 baud default (configurable)✅ Faster, more responsive communication
Command SetBasic, sometimes buggyMore advanced, stable command set✅ More reliable control and feedback
Speaker ConnectionDirect to mono speakerStereo line out (needs amp)➖ Needs external amp for speakers
Volume Control0–30 steps0–100 steps✅ Finer volume control
Power Supply3.2V–5V3.3V–5.5V➕ Slightly more flexible
SizeSmaller (20mm x 22mm)Slightly larger (24mm x 24mm)➖ Slightly bigger, but compact enough
Ease of UseSimple, but unstable at timesMore stable firmware✅ More professional and dependable
Audio ChannelsMonoDual-channel (stereo)✅ True stereo separation for improved sound effects
Advanced Playback ControlsLimited to play/pause/next/prevFast-forward, fast-rewind, play from specific time✅ More control and interactivity for audio playback

Wiring

Wiring up the sensor is quite complex, the pins are not labelled so you will have to refer to the pinout. dfplayerpinout.png

DFplayer Mini Wiring
  1. VCC to 5V (Power)
  2. GND to GND (Ground)
  3. RX to D2 via 1K resistorD3
  4. TX to D3D2
  5. SPK_1R+ to Speaker(+) red wire
  6. GND to GND (Ground)
  7. SPK_2R- to Speaker(-) black wire
  • Second
    potentiometer Wiring
    1. right pin to 5Vspeaker (Power)
    2. L+
    3. middle& pinL-) tois A0 (Signal)
    4. left pin to GND (Ground) dfplayermini_bb.png optional

    File handling

    The order you copy the mp3 onto the micro SD card will affect the order mp3 played, which means the play(1) function will play the first mp3 copied into the micro SD card.

     MAC User Attention!
     If you are using Mac OS X to copy the mp3, the file system will automatically add hidden files like: "._0001.mp3" for index, which this module will handle as valid mp3 files.

    It is really annoying. To remove them, follow the below steps:

    1. Finder - Go to your USB drive
    2. Press Shift + Command + . to reveal all hidden files
    3. Select all .XXXXXX files and directories and delete
    4. Empty Bin
    5. Eject your USB drive

    Library

    DFRobotDFPlayerMiniDFRobot_DF1201S library will be used for this module. We have a tutorial on how to install a library here.

    Get Started

    In this example, we are using the potentiometer to control two audios. It will play the first audio when the potentiometer turns to the right and play the second when it turns to the left.

     DF layer will not initiate!
     If you didn't put in the SD card, or have no MP3 files in the SD card, the module will not work. Make sure you are using .mp3, not .wav or any other audio formats.

    #include "SoftwareSerial.h"
    #include "DFRobotDFPlayerMini.h"
    
    // Use pins 2 and 3 to communicate with DFPlayer Mini
    static const uint8_t PIN_MP3_TX = 2; // Connects to module's RX
    static const uint8_t PIN_MP3_RX = 3; // Connects to module's TX
    SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
    
    const int pot = A0;
    int potValue = 0;
    
    // Create the Player object
    DFRobotDFPlayerMini player;
    
    void setup() {
    
      pinMode(pot, INPUT);
    
      // Init USB serial port for debugging
      Serial.begin(9600);
      // Init serial port for DFPlayer Mini
      softwareSerial.begin(9600);
    
      // Start communication with DFPlayer Mini
      if (player.begin(softwareSerial)) {
        Serial.println("OK");
    
        // Set volume to maximum (0 to 30).
        player.volume(30);
      } else {
        Serial.println("Connecting to DFPlayer Mini failed!");
      }      
    }
    
    void loop() {
    
    	potValue = analogRead(pot);
    
    	if(potValue > 500 ){ 
    
    	 static unsigned long timer = millis();
     	 if (millis() - timer > 2000) { //2000 is the duration of the audio(1)
      		timer = millis();
    
       		//(2) is the 2rd file in the sd card, the order = the order you copied the file to it
       		player.play(2);  
      }
      
    	}else {
      	  static unsigned long timer = millis();
      
     	 if (millis() - timer > 3000) { //3000 is the duration of the audio(2)
      	  	timer = millis();
       		player.play(1); 
      		}
    	}
    }
    

    Better Library

    Since the official library uses delay() in the code, it can be problematic when the code is used with other components or sensors.

    DFRobotDFPlayerMini_Fast library will be used for this module. You will need to do a manual install for this library. FireTimer library is also needed.

    We have a tutorial on how to install a library here.

    Example code

    The wiring will be the same as above. For further details of this library API, please visit their github page.

    #include <DFPlayerMini_Fast.DFRobot_DF1201S.h>
    #include <SoftwareSerial.h>
    
    SoftwareSerial mySerial(3,DF1201SSerial(2, 2)3);  // RX,RX  TX
    
    DFPlayerMini_FastDFRobot_DF1201S myMP3;DF1201S;
    void setup()
    void){
      Serial.begin(115200);
      mySerial.DF1201SSerial.begin(9600)115200);
      myMP3.while(!DF1201S.begin(mySerial,DF1201SSerial)){
        true)Serial.println("Init failed, please check the wire connection!");
        delay(1000);
      Serial.println("Setting}
      /*Set volume to max"20*/
      DF1201S.setVol(/*VOL = */20);
      Serial.print("VOL:");
      myMP3.volume(30)/*Get volume*/
      Serial.println(DF1201S.getVol());
      /*Enter music mode*/
      DF1201S.switchFunction(DF1201S.MUSIC);
      /*Wait for the end of the prompt tone */
      delay(2000);
      /*Set playback mode to "repeat all"*/
      DF1201S.setPlayMode(DF1201S.ALLCYCLE);
      Serial.println(print("Looping track 1"PlayMode:");
      myMP3.loop(1)/*Get playback mode*/
      Serial.println(DF1201S.getPlayMode());
     
    }
    
    void loop(){
      {Serial.println("Start playing");
      /*Start playing*/
      DF1201S.start();
      delay(3000);
      Serial.println("Pause");
      /*Pause*/
      DF1201S.pause();
      delay(3000);
      Serial.println("Next");
      /*Play the next song*/
      DF1201S.next();
      delay(3000);
      Serial.println("Previous");
      /*Play the previous song*/
      DF1201S.last();
      delay(3000);
      Serial.println("Start playing");
      //doFast nothingforward 10S
      DF1201S.fastForward(/*FF = */10);
      
      Serial.print("File number:");
      //Get file number
      Serial.println(DF1201S.getCurFileNumber());
      
      Serial.print("The number of files available to play:");
      //The number of files available to play
      Serial.println(DF1201S.getTotalFile());
      
      Serial.print("The time length the current song has played:");
      //Get the time length the current song has played 
      Serial.println(DF1201S.getCurTime());
      
      Serial.print("The total length of the currently-playing song: ");
      //Get the total length of the currently-playing song 
      Serial.println(DF1201S.getTotalTime());
      Serial.print("The name of the currently-playing file: ");
      //Get the name of the playing file 
      Serial.println(DF1201S.getFileName());
      delay(3000);
      //Play the file No.1, the numbers are arranged according to the sequence of the files copied into the U-disk 
      DF1201S.playFileNum(/*File Number = */1);
      //Play the test.mp3 file in test folder 
      DF1201S.playSpecFile("TRACK007.MP3");
      
      while(1);
      /*Delete the currently-playing file */
      //DF1201S.delCurFile();
    }