Arduino and DFPlayer works only after pressing the reset button

Hello, I've been developing a project using an Arduino Mega and the MP3 DFPlayer mini. Until now, everything works when the Arduino is connected to the PC via USB but when I use an external power source (connected to the VIN pin of the Arduino) the code only works after I manually press the RESET button. Maybe it is related to the use of Serial and SoftwareSerial?

Here's my code:

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

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

static boolean estadoUltimo_1 = LOW;
static boolean estadoUltimo_2 = LOW;
static boolean estadoUltimo_3 = LOW;
static boolean estadoUltimo_4 = LOW;
static boolean estadoUltimo_5 = LOW;
static boolean estadoUltimo_6 = LOW;
static boolean estadoUltimo_7 = LOW;

boolean estadoCancion_1;
boolean estadoCancion_2;
boolean estadoCancion_3;
boolean estadoCancion_4;
boolean estadoCancion_5;
boolean estadoCancion_6;
boolean estadoCancion_7;

int volumen;

int switch_1 = 27;
int switch_2 = 29;
int switch_3 = 31;
int switch_4 = 33;
int switch_5 = 35;
int switch_6 = 37;
int switch_7 = 39;

int led_1 = 41;
int led_2 = 43;
int led_3 = 45;
int led_4 = 47;
int led_5 = 49;
int led_6 = 51;
int led_7 = 53;

void setup() {
  mySoftwareSerial.begin(9600);
  Serial.begin(9600);

  Serial.println(F("DFRobot DFPlayer Mini"));
  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) {
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }

  Serial.println(F("Cajita de Sonido Online"));

  pinMode(switch_1, INPUT_PULLUP); // SWITCH 1
  pinMode(switch_2, INPUT_PULLUP); // SWITCH 2
  pinMode(switch_3, INPUT_PULLUP); // SWITCH 3
  pinMode(switch_4, INPUT_PULLUP); // SWITCH 4
  pinMode(switch_5, INPUT_PULLUP); // SWITCH 5
  pinMode(switch_6, INPUT_PULLUP); // SWITCH 6
  pinMode(switch_7, INPUT_PULLUP); // SWITCH 7

  pinMode(led_1, OUTPUT); //LED 1
  pinMode(led_2, OUTPUT); //LED 2
  pinMode(led_3, OUTPUT); //LED 3
  pinMode(led_4, OUTPUT); //LED 4
  pinMode(led_5, OUTPUT); //LED 5
  pinMode(led_6, OUTPUT); //LED 6
  pinMode(led_7, OUTPUT); //LED 7
}

void loop() {
  myDFPlayer.volume(volumen);
  volumen = map(analogRead(A0), 0, 1023, 15, 20);

  cancion_1();
  cancion_2();
  cancion_3();
  cancion_4();
  cancion_5();
  cancion_6();
  cancion_7();
}

void cancion_1() {
  delay(30);
  estadoCancion_1 = digitalRead(switch_1);
  if (estadoCancion_1 != estadoUltimo_1) {
    if (estadoCancion_1 == HIGH) {
      Serial.println("Cancion 1 Sonando");
      myDFPlayer.play(1);
      digitalWrite(led_1 , HIGH);
    } else if (estadoCancion_1 == LOW) {
      Serial.println("Cancion 1 Callada");
      myDFPlayer.stop();
      digitalWrite(led_1 , LOW);
    }
  }
  estadoUltimo_1 = estadoCancion_1;
}

void cancion_2() {
  delay(30);
  estadoCancion_2 = digitalRead(switch_2);
  if (estadoCancion_2 != estadoUltimo_2) {
    if (estadoCancion_2 == HIGH) {
      Serial.println("Cancion 2 Sonando");
      myDFPlayer.play(2);
      digitalWrite(led_2, HIGH);
    } else if (estadoCancion_2 == LOW) {
      Serial.println("Cancion 2 Callada");
      myDFPlayer.stop();
      digitalWrite(led_2, LOW);
    }
  }
  estadoUltimo_2 = estadoCancion_2;
}

void cancion_3() {
  delay(30);
  estadoCancion_3 = digitalRead(switch_3);
  if (estadoCancion_3 != estadoUltimo_3) {
    if (estadoCancion_3 == HIGH) {
      Serial.println("Cancion 3 Sonando");
      myDFPlayer.play(3);
      digitalWrite(led_3, HIGH);
    } else if (estadoCancion_3 == LOW) {
      Serial.println("Cancion 3 Callada");
      myDFPlayer.stop();
      digitalWrite(led_3, LOW);
    }
  }
  estadoUltimo_3 = estadoCancion_3;
}

void cancion_4() {
  delay(30);
  estadoCancion_4 = digitalRead(switch_4);
  if (estadoCancion_4 != estadoUltimo_4) {
    if (estadoCancion_4 == HIGH) {
      Serial.println("Cancion 4 Sonando");
      myDFPlayer.play(4);
      digitalWrite(led_4, HIGH);
    } else if (estadoCancion_4 == LOW) {
      Serial.println("Cancion 4 Callada");
      myDFPlayer.stop();
      digitalWrite(led_4, LOW);
    }
  }
  estadoUltimo_4 = estadoCancion_4;
}

void cancion_5() {
  delay(30);
  estadoCancion_5 = digitalRead(switch_5);
  if (estadoCancion_5 != estadoUltimo_5) {
    if (estadoCancion_5 == HIGH) {
      Serial.println("Cancion 5 Sonando");
      myDFPlayer.play(5);
      digitalWrite(led_5, HIGH);
    } else if (estadoCancion_5 == LOW) {
      Serial.println("Cancion 5 Callada");
      myDFPlayer.stop();
      digitalWrite(led_5, LOW);
    }
  }
  estadoUltimo_5 = estadoCancion_5;
}

void cancion_6() {
  delay(30);
  estadoCancion_6 = digitalRead(switch_6);
  if (estadoCancion_6 != estadoUltimo_6) {
    if (estadoCancion_6 == HIGH) {
      Serial.println("Cancion 6 Sonando");
      myDFPlayer.play(6);
      digitalWrite(led_6, HIGH);
    } else if (estadoCancion_6 == LOW) {
      Serial.println("Cancion 6 Callada");
      myDFPlayer.stop();
      digitalWrite(led_6, LOW);
    }
  }
  estadoUltimo_6 = estadoCancion_6;
}

void cancion_7() {
  delay(30);
  estadoCancion_7 = digitalRead(switch_7);
  if (estadoCancion_7 != estadoUltimo_7) {
    if (estadoCancion_7 == HIGH) {
      Serial.println("Cancion 7 Sonando");
      myDFPlayer.play(7);
      digitalWrite(led_7, HIGH);
    } else if (estadoCancion_7 == LOW) {
      Serial.println("Cancion 7 Callada");
      myDFPlayer.stop();
      digitalWrite(led_7, LOW);
    }
  }
  estadoUltimo_7 = estadoCancion_7;
}

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;
  }
}

Any help is very much appreciated!

Why are you using SoftwareSerial on the Mega? It has 4 hardware serial ports, and you are using only one of them.

I thought I had to use it because the main DFPlayer library example uses it but now I see that there’s another example that uses Serial1. I’ll try with that one.

Ok, I found out that I have to use the AdvancedSettingWithoutReset exmaple. :o

It still uses SoftwareSerial but it adds a true/false condition to this section:

  if (!myDFPlayer.begin(mySoftwareSerial, true, false)) {  //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) {
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }
  Serial.println(F("DFPlayer Mini online."));

The final code:

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

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

static boolean estadoUltimo_1 = LOW;
static boolean estadoUltimo_2 = LOW;
static boolean estadoUltimo_3 = LOW;
static boolean estadoUltimo_4 = LOW;
static boolean estadoUltimo_5 = LOW;
static boolean estadoUltimo_6 = LOW;
static boolean estadoUltimo_7 = LOW;

boolean estadoCancion_1;
boolean estadoCancion_2;
boolean estadoCancion_3;
boolean estadoCancion_4;
boolean estadoCancion_5;
boolean estadoCancion_6;
boolean estadoCancion_7;

int volumen;

int switch_1 = 27;
int switch_2 = 29;
int switch_3 = 31;
int switch_4 = 33;
int switch_5 = 35;
int switch_6 = 37;
int switch_7 = 39;

int led_1 = 41;
int led_2 = 43;
int led_3 = 45;
int led_4 = 47;
int led_5 = 49;
int led_6 = 51;
int led_7 = 53;

void setup() {
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);

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

  if (!myDFPlayer.begin(mySoftwareSerial, true, false)) {  //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) {
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }
  Serial.println(F("DFPlayer Mini online."));
  Serial.println(F("Cajita de Sonido Online"));

  pinMode(switch_1, INPUT_PULLUP); // SWITCH 1
  pinMode(switch_2, INPUT_PULLUP); // SWITCH 2
  pinMode(switch_3, INPUT_PULLUP); // SWITCH 3
  pinMode(switch_4, INPUT_PULLUP); // SWITCH 4
  pinMode(switch_5, INPUT_PULLUP); // SWITCH 5
  pinMode(switch_6, INPUT_PULLUP); // SWITCH 6
  pinMode(switch_7, INPUT_PULLUP); // SWITCH 7

  pinMode(led_1, OUTPUT); //LED 1
  pinMode(led_2, OUTPUT); //LED 2
  pinMode(led_3, OUTPUT); //LED 3
  pinMode(led_4, OUTPUT); //LED 4
  pinMode(led_5, OUTPUT); //LED 5
  pinMode(led_6, OUTPUT); //LED 6
  pinMode(led_7, OUTPUT); //LED 7
}

void loop() {
  myDFPlayer.volume(volumen);
  volumen = map(analogRead(A0), 0, 1023, 15, 20);

  if (myDFPlayer.available()) {
    printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
  }

  cancion_1();
  cancion_2();
  cancion_3();
  cancion_4();
  cancion_5();
  cancion_6();
  cancion_7();
}

void cancion_1() {
  delay(30);
  estadoCancion_1 = digitalRead(switch_1);
  if (estadoCancion_1 != estadoUltimo_1) {
    if (estadoCancion_1 == HIGH) {
      Serial.println("Cancion 1 Sonando");
      myDFPlayer.play(1);
      digitalWrite(led_1 , HIGH);
    } else if (estadoCancion_1 == LOW) {
      Serial.println("Cancion 1 Callada");
      myDFPlayer.stop();
      digitalWrite(led_1 , LOW);
    }
  }
  estadoUltimo_1 = estadoCancion_1;
}

void cancion_2() {
  delay(30);
  estadoCancion_2 = digitalRead(switch_2);
  if (estadoCancion_2 != estadoUltimo_2) {
    if (estadoCancion_2 == HIGH) {
      Serial.println("Cancion 2 Sonando");
      myDFPlayer.play(2);
      digitalWrite(led_2, HIGH);
    } else if (estadoCancion_2 == LOW) {
      Serial.println("Cancion 2 Callada");
      myDFPlayer.stop();
      digitalWrite(led_2, LOW);
    }
  }
  estadoUltimo_2 = estadoCancion_2;
}

void cancion_3() {
  delay(30);
  estadoCancion_3 = digitalRead(switch_3);
  if (estadoCancion_3 != estadoUltimo_3) {
    if (estadoCancion_3 == HIGH) {
      Serial.println("Cancion 3 Sonando");
      myDFPlayer.play(3);
      digitalWrite(led_3, HIGH);
    } else if (estadoCancion_3 == LOW) {
      Serial.println("Cancion 3 Callada");
      myDFPlayer.stop();
      digitalWrite(led_3, LOW);
    }
  }
  estadoUltimo_3 = estadoCancion_3;
}

void cancion_4() {
  delay(30);
  estadoCancion_4 = digitalRead(switch_4);
  if (estadoCancion_4 != estadoUltimo_4) {
    if (estadoCancion_4 == HIGH) {
      Serial.println("Cancion 4 Sonando");
      myDFPlayer.play(4);
      digitalWrite(led_4, HIGH);
    } else if (estadoCancion_4 == LOW) {
      Serial.println("Cancion 4 Callada");
      myDFPlayer.stop();
      digitalWrite(led_4, LOW);
    }
  }
  estadoUltimo_4 = estadoCancion_4;
}

void cancion_5() {
  delay(30);
  estadoCancion_5 = digitalRead(switch_5);
  if (estadoCancion_5 != estadoUltimo_5) {
    if (estadoCancion_5 == HIGH) {
      Serial.println("Cancion 5 Sonando");
      myDFPlayer.play(5);
      digitalWrite(led_5, HIGH);
    } else if (estadoCancion_5 == LOW) {
      Serial.println("Cancion 5 Callada");
      myDFPlayer.stop();
      digitalWrite(led_5, LOW);
    }
  }
  estadoUltimo_5 = estadoCancion_5;
}

void cancion_6() {
  delay(30);
  estadoCancion_6 = digitalRead(switch_6);
  if (estadoCancion_6 != estadoUltimo_6) {
    if (estadoCancion_6 == HIGH) {
      Serial.println("Cancion 6 Sonando");
      myDFPlayer.play(6);
      digitalWrite(led_6, HIGH);
    } else if (estadoCancion_6 == LOW) {
      Serial.println("Cancion 6 Callada");
      myDFPlayer.stop();
      digitalWrite(led_6, LOW);
    }
  }
  estadoUltimo_6 = estadoCancion_6;
}

void cancion_7() {
  delay(30);
  estadoCancion_7 = digitalRead(switch_7);
  if (estadoCancion_7 != estadoUltimo_7) {
    if (estadoCancion_7 == HIGH) {
      Serial.println("Cancion 7 Sonando");
      myDFPlayer.play(7);
      digitalWrite(led_7, HIGH);
    } else if (estadoCancion_7 == LOW) {
      Serial.println("Cancion 7 Callada");
      myDFPlayer.stop();
      digitalWrite(led_7, LOW);
    }
  }
  estadoUltimo_7 = estadoCancion_7;
}

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;
  }

}

Just because the example is written for Arduinos with only one hardware serial port does NOT mean that you have to use SoftwareSerial on the Mega.

If the library is properly written, it takes an instance of the base class that both HardwareSerial and SoftwareSerial derive from.

pierre_horizontal: Ok, I found out that I have to use the AdvancedSettingWithoutReset exmaple. :o

Did that fix the problem you reported with having to press the reset button?