Arduino Mega 2560 and DFPlayer Mini

Hello! I am just wondering, "How do I connect and code the DFPlayer Mini to play a sound with an Arduino Mega 2560?" May someone, please, tell me how to do this? My attempt with someone's wiring diagram seemed to work in only some cases, and before you ask, YES, ground is connected from the Arduino Mega 2560 to the DFPlayer Mini. I don't know what library to use either. I tried using this library...

DFPlayer Mini Library

and that seemed to work for only one example, though. I'm talking about this example (PlayRandom):

// this example will play a random track from all on the sd 
//
// it expects the sd card to contain some mp3 files

#include <SoftwareSerial.h>
#include <DFMiniMp3.h>

// implement a notification class,
// its member methods will get called 
//
class Mp3Notify
{
public:
  static void OnError(uint16_t errorCode)
  {
    // see DfMp3_Error for code meaning
    Serial.println();
    Serial.print("Com Error ");
    Serial.println(errorCode);
  }
  static void OnPlayFinished(uint16_t track)
  {
    Serial.print("Play finished for #");
    Serial.println(track);  
  }
  static void OnCardOnline(uint16_t code)
  {
    Serial.println("Card online ");
  }
  static void OnUsbOnline(uint16_t code)
  {
    Serial.println("USB Disk online ");
  }
  static void OnCardInserted(uint16_t code)
  {
    Serial.println("Card inserted ");
  }
  static void OnUsbInserted(uint16_t code)
  {
    Serial.println("USB Disk inserted ");
  }
  static void OnCardRemoved(uint16_t code)
  {
    Serial.println("Card removed ");
  }
  static void OnUsbRemoved(uint16_t code)
  {
    Serial.println("USB Disk removed ");
  }
};

// instance a DFMiniMp3 object, 
// defined with the above notification class and the hardware serial class
//
DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial1);

// Some arduino boards only have one hardware serial port, so a software serial port is needed instead.
// comment out the above definition and uncomment these lines
//SoftwareSerial secondarySerial(10, 11); // RX, TX
//DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);

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

  Serial.println("initializing...");
  
  mp3.begin();
  mp3.reset(); 
  
  // show some properties and set the volume
  uint16_t volume = mp3.getVolume();
  Serial.print("volume ");
  Serial.println(volume);
  mp3.setVolume(24);
  
  uint16_t count = mp3.getTotalTrackCount();
  Serial.print("files ");
  Serial.println(count);

  uint16_t mode = mp3.getPlaybackMode();
  Serial.print("playback mode ");
  Serial.println(mode);
  
  Serial.println("starting...");
  
  mp3.playRandomTrackFromAll(); // random of all folders on sd
}

void loop() 
{
  // calling mp3.loop() periodically allows for notifications 
  // to be handled without interrupts
  mp3.loop();
}

Here is my code, as well...

//www.elegoo.com
//2016.12.9

#include <DFMiniMp3.h>

// implement a notification class,
// its member methods will get called
//
class Mp3Notify
{
  public:
    static void OnError(uint16_t errorCode)
    {
      // see DfMp3_Error for code meaning
      Serial.println();
      Serial.print("Com Error ");
      Serial.println(errorCode);
    }

    static void OnPlayFinished(uint16_t globalTrack)
    {
      Serial.println();
      Serial.print("Play finished for #");
      Serial.println(globalTrack);
    }

    static void OnCardOnline(uint16_t code)
    {
      Serial.println();
      Serial.print("Card online ");
      Serial.println(code);
    }

    static void OnUsbOnline(uint16_t code)
    {
      Serial.println();
      Serial.print("USB Disk online ");
      Serial.println(code);
    }

    static void OnCardInserted(uint16_t code)
    {
      Serial.println();
      Serial.print("Card inserted ");
      Serial.println(code);
    }

    static void OnUsbInserted(uint16_t code)
    {
      Serial.println();
      Serial.print("USB Disk inserted ");
      Serial.println(code);
    }

    static void OnCardRemoved(uint16_t code)
    {
      Serial.println();
      Serial.print("Card removed ");
      Serial.println(code);
    }

    static void OnUsbRemoved(uint16_t code)
    {
      Serial.println();
      Serial.print("USB Disk removed ");
      Serial.println(code);
    }
};

// instance a DFMiniMp3 object,
// defined with the above notification class and the hardware serial class
//
DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial1);

// Some arduino boards only have one hardware serial port, so a software serial port is needed instead.
// comment out the above definition and uncomment these lines
//SoftwareSerial secondarySerial(10, 11); // RX, TX
//DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);


int pirPin = 7; // Input for HC-S501

int pirValue; // Place to store read PIR Value

int ledPin = 13;  // LED on Pin 13 of Arduino

void setup() {

  pinMode(ledPin, OUTPUT);

  mp3.begin();

  mp3.setVolume(24);

  pinMode(pirPin, INPUT);

  digitalWrite(ledPin, LOW);

}

void loop() {

  pirValue = digitalRead(pirPin);

  digitalWrite(ledPin, pirValue);

  while (pirValue == HIGH) {
    mp3.playMp3FolderTrack(1);  // sd:/mp3/0001.mp3
  }

}

I tried looking for tutorials and couldn't find any tutorials on how to use the Arduino Mega 2560 with the DFPlayer Mini.

Here is my schematic (I am powering the DFPlayer from a different 5V power source and the Arduino Mega 2560 is powered through a USB, also, why does the speaker make a weird noise every time I play a sound?):

My Schematic

Just in case you can't see the schematic:

The library you linked has a rather odd API. There's really no reason to have to create your own callback functions or have a templated class. Consider using this library.

Your schematic looks correct, but why are you using SoftwareSerial on a Mega? Are you using all of the other hardware ports for other sensors/actuators? There are 3 hardware ports (plus the USB port) on a Mega.

Also, are there any printouts in the serial monitor when you run the code?

  1. I changed to that library you are talking about and the example worked, it's just I don't know how to use the library without the SoftwareSerial library, but I still put the library in my code.

  2. I don't how to use the library without the SoftwareSerial library.

  3. No, I am not all of the digital I/O pins for other sensors/actuator, I just chose to follow the circuit diagram, although, I did change the RX and TX pin to be 10 and 11 and that seemed to work just fine, even though I still get a strange buzzing noise. And YES I have the 1k ohm connected to the RX pin of the MP3 player.

  4. Yes, when I tested the new example program this the message I got...

"Setting volume to max
Playing track 1 for 5 sec
Sleeping for 5 sec
Waking up
Looping track 1"

and an error message for the other library.

Example code:

#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>

SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;

void setup()
{
  Serial.begin(9601);
  mySerial.begin(9600);

  myMP3.begin(mySerial);
  
  Serial.println("Setting volume to max");
  myMP3.volume(30);
  delay(20);
  
  Serial.println("Playing track 1 for 5 sec");
  myMP3.play(1);
  delay(5000);

  Serial.println("Sleeping for 5 sec");
  myMP3.sleep();
  delay(5000);

  Serial.println("Waking up");
  myMP3.wakeUp();

  Serial.println("Looping track 1");
  myMP3.loop(1);
}

void loop()
{
  //do nothing
}

My code:

#include <DFPlayerMini_Fast.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;

int pirPin = 7; // Input for HC-S501

int pirValue; // Place to store read PIR Value

int ledPin = 13;  // LED on Pin 13 of Arduino

void setup() {

  pinMode(ledPin, OUTPUT);

  Serial.begin(9601);

  mySerial.begin(9600);

  myMP3.volume(15);

  pinMode(pirPin, INPUT);

}

void loop() {

  if (digitalRead(pirPin) == LOW) {
    digitalWrite(ledPin, HIGH);
    myMP3.play(1);
  }
else {
  digitalWrite(ledPin, LOW);
}
}

The PIR sensor works with a test program, but it's like when I test it with MY program, it doesn't work. I say this because the on-board LED is going wack everytime I try to test the sensor, the LED just blinks like, "BLINK, pause, BLINK!"

Test program:

//www.elegoo.com
//2016.12.9

int ledPin = 13;  // LED on Pin 13 of Arduino
int pirPin = 7; // Input for HC-S501

int pirValue; // Place to store read PIR Value


void setup() {
  
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
 
  digitalWrite(ledPin, LOW);
  
  
  
}

void loop() {
  pirValue = digitalRead(pirPin);
  digitalWrite(ledPin, pirValue);

}

Hi UptownKitten!

First!
Don't yell for help like that, you'll just push everybody away.

Second!
You just started this yesterday, don't be so hurry.

And the about your circuit, does it really need that resistor at all, I'm not sure.
I don't have it, and I don't see that in most instructions I have found.

Did you follow the instructions about mp3-files and memory card?

If you use serial1, you need to add to setup:

Serial1.begin(your_baudrate_for_DFplayer); // I don't remember the correct speed...

Cheers,
Kari

GaryP:
Did you follow the instructions about mp3-files and memory card?

What does that mean and what is the deal with the Serial thing?

Thanks for all the help! Figured it out:

/*Software from Mert Arudino:
https://www.hackster.io/mertarduino/how-to-make-a-talking-pir-motion-security-system-d74e0a
Special thanks to all who helped as well:
https://forum.arduino.cc/index.php?topic=657163.msg4431743#msg4431743
*/

#include <DFPlayerMini_Fast.h>

DFPlayerMini_Fast myMP3;
void printDetail(uint8_t type, int value);

#define SENSORPIN 7
#define SENSOR2PIN 6
#define PAUSETIME 2000

byte SENSORCOUNT = 1;
byte SENSOR2COUNT = 0;

void setup() {
  Serial1.begin(9600);
  Serial.begin(9600);
  pinMode(SENSORPIN, INPUT);
  pinMode(SENSOR2PIN, INPUT);

  Serial.println();
  Serial.println(F("Initializing DFPlayer..."));

  //Use softwareSerial to communicate with MP3
  if (!myMP3.begin(Serial1)) {
    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."));

  //Set volume value (From 0 to 30)
  myMP3.volume(30);
}

void sensor2Activated() {
  
  int IRSensor = digitalRead(SENSOR2PIN);
  
  if(IRSensor == LOW && SENSOR2COUNT == 0)
  {
    SENSOR2COUNT++;
    Serial.println("Sensor Activated");
    Serial.println("DFPlayer Working...");
    myMP3.play(9);
  }
  else{
    myMP3.sleep();
  }
  return;
}

void sensorActivated() {
  
  int pirSensor = digitalRead(SENSORPIN);
  
  if(pirSensor == HIGH && SENSORCOUNT == 1)
  {
    SENSORCOUNT++;
    Serial.println("Sensor Activated");
    Serial.println("DFPlayer Working...");
    myMP3.play(10);
    delay(1500);
  }
  else{
    myMP3.sleep();
  }
  return;
}

void loop() {
  sensorActivated();
  sensor2Activated();
  delay(PAUSETIME);
}