Advertisement
JonD1988

DFPlayerMiniTest

Dec 19th, 2022
1,133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Rev 0 This was developed on 12/19/2022 and is a simple sketch allowing the user to test out the DFPlayer Mini using ESP32 Node MCU - Note ththe DFPlayer Mini is wired to the ESP32 it is to the Arduino
  2. //#include <SoftwareSerial.h> //Note this is the ESP32 Software Serial
  3. #include <HardwareSerial.h>
  4. #include "Arduino.h" //Needed for FullFunction DFRobotDFPlayerMini library sketch
  5. #include "DFRobotDFPlayerMini.h" //Needed for FullFunction DFRobotDFPlayerMini library sketch
  6.  
  7. //Now to Use a Software Serial Port to Control DFPlayer Mini
  8. //SoftwareSerial portTwo(27, 26); //Here ESP32 GPIO27 is Rx and ESP32 GPIO26 is Tx
  9. //Connect ESP32 GPIO 27 to DFPlayer Mini Pin Tx through 1 kΩ Resistor
  10. //Connect ESP32 GPIO 26 to DFPlayer Mini Pin Rx through 1 kΩ Resistor
  11. HardwareSerial MySerial(1);
  12. #define DFPlayerTx 27
  13. #define DFPlayerRx 26
  14. DFRobotDFPlayerMini myDFPlayer; //Defines an Object to Call the DFPlayer Mini with object name myDFPlayer - Needed for FullFunction DFRobotDFPlayerMini library sketch
  15. void printDetail(uint8_t type, int value); //Needed for FullFunction DFRobotDFPlayerMini library sketch
  16. int volC=25; //Volume Level returned from VolumeControl function
  17.  
  18. void setup() {
  19.   Serial.begin (9600); //Starts up the serial monitor
  20.   //portTwo.begin(9600); //Start Serial Port to DFPlayer Mini
  21.   MySerial.begin(9600, SERIAL_8N1, DFPlayerTx, DFPlayerRx);
  22.  
  23.   //Start of DFPlayer Mini setup - comes from FullFunction DFRobotDFPlayerMini library sketch
  24.   Serial.println();
  25.   Serial.println(F("DFRobot DFPlayer Mini Demo"));
  26.   Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  27.  
  28.   if (!myDFPlayer.begin(MySerial)) {  //Use hardware serial to communicate with DFPlayerMini
  29.     Serial.println(F("Unable to begin:"));
  30.     Serial.println(F("1.Please recheck the connection!"));
  31.     Serial.println(F("2.Please insert the SD card!"));
  32.     while(true);
  33.   }
  34.   Serial.println(F("DFPlayer Mini online."));
  35.  
  36.   myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
  37.  
  38.   //----Set volume----
  39.   myDFPlayer.volume(volC);  //Set volume value (0~30).
  40.   myDFPlayer.volumeUp(); //Volume Up
  41.   myDFPlayer.volumeDown(); //Volume Down
  42.  
  43.   //----Set different EQ----
  44.   myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
  45.  
  46.   //----Set device we use SD as default----
  47.   myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
  48.  
  49.   //----Read imformation----
  50.   Serial.println(myDFPlayer.readState()); //read mp3 state
  51.   Serial.println(myDFPlayer.readVolume()); //read current volume
  52.   Serial.println(myDFPlayer.readEQ()); //read EQ setting
  53.   Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
  54.   Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
  55.   Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read file counts in folder SD:/03
  56.   //End of DFPlayer Mini setup
  57. }
  58.  
  59. void loop() { //Start of void loop
  60.  
  61.   //This if statement comes from FullFunction DFRobotDFPlayerMini library sketch
  62.   //if (myDFPlayer.available()) {
  63.     //printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
  64.  //   myDFPlayer.enableLoopAll(); //loop all mp3 files.
  65.  // }
  66.  
  67.  myDFPlayer.enableLoopAll(); //loop all mp3 files.
  68.  
  69. } //End of void loop
  70.  
  71. //References
  72. //Reference 1- To use a second serial port in the ESP32 https://quadmeup.com/arduino-esp32-and-3-hardware-serial-ports/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement