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...
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?):
Just in case you can't see the schematic: