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


DFplayer Mini Wiring
- VCCVIN to 5V (Power)
- GND to GND (Ground)
- RX to D2via 1K resistor
- TX to D3
- SPK_1R+ to Speaker(+) red wire
- GND to GND (Ground)
- SPK_2R- to Speaker(-) black wire
potentiometerL+ Wiring
& 
File handling
The
- You youcancopydownload our example-sounds.zip.
- Connect the module to a computer with a USBC cable.
- Create a folder called mp3.
- Place the mp3 onto the micro SD card will affect the order mp3 played, which means theplay(1)function will play the first mp3 copiedfiles intothethatmicro 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:- Finder - Go to your USB drive
- Press- Shift- +- Command- +- .- to reveal all hidden files
- Select all- .XXXXXX- files and directories and delete
- Empty Bin
- Eject your USB drivefolder.
 LibraryDFRobotDFPlayerMiniDFRobot_DF1201SGet StartedIn 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'sRXstatic const uint8_t PIN_MP3_RX = 3; // Connects to module'sTXSoftwareSerialDFRobot_DF1201SsoftwareSerial(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 debuggingSerial.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,Setpleasevolume 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 thedurationwireof 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 LibrarySince the official library usesdelay()in the code, it can be problematic when the code is used with other components or sensors.DFRobotDFPlayerMini_Fastlibrary will be used for this module. You will need to do a manual install for this library.FireTimerlibrary is also needed.We have a tutorial onhow to install a libraryhere.Example codeThe 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()/domp3/005.mp3");nothingString fileName = DF1201S.getFileName(); Serial.print("playing: "); Serial.println(fileName); delay(1000); }
 
        

