ESP32 and DFPlayermini, not play sound from push button

Hello, I am moving a project from Arduino UNO, to ESP32, but I am having difficulties with the code in ESP32, in this case, when pressing a button, it should play a specific sound, but it does not work. I am basing it on my old Arduino UNO code.

My diagram

Current code:

#include <Arduino.h>
#include "DFRobotDFPlayerMini.h"

bool doneFlag = false;

HardwareSerial mySoftwareSerial(1);
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

# define ACTIVATED LOW

int btnpuerta = 35;
int btnwelcome = 34;



void setup()
{

  mySoftwareSerial.begin(9600, SERIAL_8N1, 16, 17);  // speed, type, RX, TX
  Serial.begin(115200);

  pinMode(btnpuerta, INPUT);
  digitalWrite(btnpuerta, HIGH);


  pinMode(btnwelcome, INPUT);
  //digitalWrite(btnwelcome, HIGH);


  
  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  
  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true);
  }
  Serial.println(F("DFPlayer Mini online."));
  
  myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
  
  //----Set volume----
  myDFPlayer.volume(30);  //Set volume value (0~30).
  myDFPlayer.volumeUp(); //Volume Up
  myDFPlayer.volumeDown(); //Volume Down
  
  //----Set different EQ----
  myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);

  
  //----Set device we use SD as default----

    myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);

  //----Read imformation----
  Serial.println(myDFPlayer.readState()); //read mp3 state
  Serial.println(myDFPlayer.readVolume()); //read current volume
  Serial.println(myDFPlayer.readEQ()); //read EQ setting
  Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
  Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
  Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read file counts in folder SD:/03

}

void loop()
{

  if (digitalRead(btnpuerta) == ACTIVATED) {
    myDFPlayer.play(1);
    delay(2000);
  }

}

void printDetail(uint8_t type, int value){
  switch (type) {
    case TimeOut:
      Serial.println(F("Time Out!"));
      break;
    case WrongStack:
      Serial.println(F("Stack Wrong!"));
      break;
    case DFPlayerCardInserted:
      Serial.println(F("Card Inserted!"));
      break;
    case DFPlayerCardRemoved:
      Serial.println(F("Card Removed!"));
      break;
    case DFPlayerCardOnline:
      Serial.println(F("Card Online!"));
      break;
    case DFPlayerPlayFinished:
      Serial.print(F("Number:"));
      Serial.print(value);
      Serial.println(F(" Play Finished!"));
      break;
    case DFPlayerError:
      Serial.print(F("DFPlayerError:"));
      switch (value) {
        case Busy:
          Serial.println(F("Card not found"));
          break;
        case Sleeping:
          Serial.println(F("Sleeping"));
          break;
        case SerialWrongStack:
          Serial.println(F("Get Wrong Stack"));
          break;
        case CheckSumNotMatch:
          Serial.println(F("Check Sum Not Match"));
          break;
        case FileIndexOut:
          Serial.println(F("File Index Out of Bound"));
          break;
        case FileMismatch:
          Serial.println(F("Cannot Find File"));
          break;
        case Advertise:
          Serial.println(F("In Advertise"));
          break;
        default:
          break;
      }
      break;
    default:
      break;
  }
}

My old code, used in Arduino UNO, this code working perfectly

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"


bool doneFlag = false;

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX


DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

# define ACTIVATED LOW

int btnpuerta = 7;
int btnwelcome = 6;


void setup()
{

  pinMode(btnpuerta, INPUT);
  digitalWrite(btnpuerta, HIGH);

  pinMode(btnwelcome, INPUT);
  digitalWrite(btnwelcome, HIGH);

  Serial.begin(9600);
  
  mySoftwareSerial.begin(9600);




  Serial.println();
  Serial.println(F("Escanmotor.com"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));


  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true);
  }
  Serial.println(F("DFPlayer Mini online."));

  myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms

  //----Set volume----
  myDFPlayer.volume(30);  //Set volume value (0~30).
  myDFPlayer.volumeUp(); //Volume Up
  myDFPlayer.volumeDown(); //Volume Down

  //----Set different EQ----
  myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);


  //----Set device we use SD as default----

  myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);



  //----Read imformation----
  Serial.println(myDFPlayer.readState()); //read mp3 state
  Serial.println(myDFPlayer.readVolume()); //read current volume
  Serial.println(myDFPlayer.readEQ()); //read EQ setting
  Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
  Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
  Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read file counts in folder SD:/03


}

void loop() {



  mySoftwareSerial.listen();




  if (digitalRead(btnpuerta) == ACTIVATED) {
    myDFPlayer.play(1);
    delay(2000);
  }


  if (digitalRead(btnwelcome) == ACTIVATED) {
    if (doneFlag == false) // fragmento para repetir el comando una sola vez
    {
      myDFPlayer.play (5);
      delay (2000);
      myDFPlayer.stop ();
      doneFlag = true;
    }
    /*Restablecemos para que se vuelva activar al desaparecer la señal*/
  } else {
    doneFlag = false;
  }



}


void printDetail(uint8_t type, int value) {
  switch (type) {
    case TimeOut:
      Serial.println(F("Time Out!"));
      break;
    case WrongStack:
      Serial.println(F("Stack Wrong!"));
      break;
    case DFPlayerCardInserted:
      Serial.println(F("Card Inserted!"));
      break;
    case DFPlayerCardRemoved:
      Serial.println(F("Card Removed!"));
      break;
    case DFPlayerCardOnline:
      Serial.println(F("Card Online!"));
      break;
    case DFPlayerUSBInserted:
      Serial.println("USB Inserted!");
      break;
    case DFPlayerUSBRemoved:
      Serial.println("USB Removed!");
      break;
    case DFPlayerPlayFinished:
      Serial.print(F("Number:"));
      Serial.print(value);
      Serial.println(F(" Play Finished!"));
      break;
    case DFPlayerError:
      Serial.print(F("DFPlayerError:"));
      switch (value) {
        case Busy:
          Serial.println(F("Card not found"));
          break;
        case Sleeping:
          Serial.println(F("Sleeping"));
          break;
        case SerialWrongStack:
          Serial.println(F("Get Wrong Stack"));
          break;
        case CheckSumNotMatch:
          Serial.println(F("Check Sum Not Match"));
          break;
        case FileIndexOut:
          Serial.println(F("File Index Out of Bound"));
          break;
        case FileMismatch:
          Serial.println(F("Cannot Find File"));
          break;
        case Advertise:
          Serial.println(F("In Advertise"));
          break;
        default:
          break;
      }
      break;
    default:
      break;
  }

}

Thnks for u help

Post the output/errors from your serial monitor.

Your buttons are not grounded. The esp (or Uno) cannot read them like that.

  digitalWrite(btnpuerta, HIGH);

  pinMode(btnwelcome, INPUT);

You need to use INPUT_PULLUP here. This code will not work on esp or Uno. What code and circuit did you use with Uno?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.