Skip to main content

How to use DFPlayer Pro to play MP3

What is a DFPlayer?DFPlayer Pro?

11/2024Replacing UpdateDFplayer Mini
ASince the old model is not the most stable and many random problems are encountered, we are going to use this new library added below.
For people trying to avoid delay(), please use the DFPlayerMini_Fast librarymodel instead.

The DFPlayer Pro is an upgrade of the DFPlayer Mini MP3 Player Forfor ArduinoArduino. It is a small and low-priced MP3 module with aan simplifiedinternal outputstorage directlyof to the speaker.128MB. The module can be used as a stand-alone module with an attached battery, speaker and push buttons or used in combination with an Arduino UNO or any other with RX/TX capabilities. Know More

This tutorial is based on using Arduino UNO.

dfplayer.jpg

Wiring

Wiring up the sensor is quiteless complex,complex than the pinsprevious areversion notwith the labelled so you will have to refer to the pinout.pins. dfplayerpinout.png

DFplayer Mini Wiring
  1. VCCVIN to 5V (Power)
  2. GND to GND (Ground)
  3. RX to D2 via 1K resistor
  4. TX to D3
  5. SPK_1R+ to Speaker(+) red wire
  6. GND to GND (Ground)
  7. SPK_2R- to Speaker(-) black wire
  • Connect
    potentiometerL+ Wiring
    &
      L-
    1. rightif pinyou towant 5Vstereo (Power)
    2. middle pin to A0 (Signal)
    3. left pin to GND (Ground) dfplayermini_bb.pngaudio

    File handling

    The

      order
    1. You youcan copydownload our example-sounds.zip.
    2. Connect the module to a computer with a USBC cable.
    3. Create a folder called mp3.
    4. Place the mp3 onto the micro SD card will affect the order mp3 played, which means the play(1) function will play the first mp3 copiedfiles into thethat 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 drivefolder.

      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 <DFRobot_DF1201S.h>
      #include "SoftwareSerial.h"
      
      #includeSoftwareSerial "DFRobotDFPlayerMini.h"DF1201SSerial(2, 3);  // 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
      
      SoftwareSerialDFRobot_DF1201S softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
      
      const int pot = A0;
      int potValue = 0;
      
      // Create the Player object
      DFRobotDFPlayerMini player;DF1201S;
      
      void setup() void){
      
        pinMode(pot, INPUT);
      
        // Init USB serial port for debugging
      
        Serial.begin(9600)115200);
        // Init serial port for DFPlayer Mini
        softwareSerial.DF1201SSerial.begin(9600)115200);
      
        // Start communication with DFPlayer Mini
        ifwhile (player.!DF1201S.begin(softwareSerial)DF1201SSerial)) {
          Serial.println("OK");Init //failed, Setplease 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 ischeck the durationwire of the audio(1)
        		timer = millis(connection!");
      
         		//(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.h>
      #include <SoftwareSerial.h>
      SoftwareSerial mySerial(3, 2); // RX, TX
      
      DFPlayerMini_Fast myMP3;
      
      void setup()
      {
        Serial.begin(115200);
        mySerial.begin(9600);
        myMP3.begin(mySerial, true);
          delay(1000);
        Serial.println("Setting}
      
        volume to max")DF1201S.setVol(0);
        myMP3.volume(30)DF1201S.switchFunction(DF1201S.MUSIC);
        Serial.println("Looping track 1")delay(2000);
        myMP3.loop(1)/*
          SINGLECYCLE = 1,  /**<Repeat one song 
          ALLCYCLE,         /**<Repeat all 
          SINGLE,           /**<Play one song only 
          RANDOM,           /**<Random
          FOLDER,           /**<Repeat all songs in folder 
        */
        DF1201S.setPlayMode(DF1201S.SINGLE); 
        DF1201S.setVol(20);
      
      }
      
      void loop()
      {
      
        DF1201S.playSpecFile("//domp3/005.mp3");
        nothingString fileName = DF1201S.getFileName();
        Serial.print("playing: ");
        Serial.println(fileName);
        delay(1000);
      
      }