Help with DFplayer mini project

I’m trying to build a sound board where one of 2 IR break beam sensors triggers a DFplayer mini to play a specific track (one track for each sensor). And another track to play on load.

I’m modifying a bit of code I found online for something close to what Im looking for.

I have gotten it to upload with no problems. HOWEVER there is no sound.
I have tested the DFPlayer (2 different ones same result), and when I ground the IO1 and IO2 pins the tracks play just fine. and the LEDs are working as well.

Whats happening:

When you trip the AWAY IR, the LED lights up and shuts off Perfectly but NO SOUND

When I trip the Home IR there is NO SOUND, and the LED Does NOT light up. However I can see a state change in the on board TX led.

Also no sound on start up (supposed to play 0003.mp3)

The serial Monitor shows:

14:09:33.682 → track 1

(same for track 2 and track 3)
When the IR Break beams are tripped (track 3 on upload).

Can anyone please help? I’m still learning but I cant seam to see what may be the problem.

// Name Files as follows
//
// sd:/mp3/0001.mp3 // Home Goal
// sd:/mp3/0002.mp3 // Away Goal
// sd:/mp3/0003.mp3 // Start Up Sound

const int buttonPin2 = 4; // Home IR
const int buttonPin3 = 2; // Away IR
const int buttonPin4 = 3; // not used (future option)
const int ledPin1 = 8; // Home Led
const int ledPin2 = 12; // Away Led
int buttonState2 = 0; // variable for reading the pushbutton status
int buttonState3 = 0; // variable for reading the pushbutton status
int buttonState4 = 0; // variable for reading the pushbutton status

#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.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 OnCardInserted(uint16_t code)
{
Serial.println();
Serial.print("Card inserted ");
Serial.println(code);
}

static void OnCardRemoved(uint16_t code)
{
Serial.println();
Serial.print("Card 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);

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

void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);

Serial.begin(9600);

mp3_set_serial (mySerial); //set softwareSerial for DFPlayer-mini mp3 module

Serial.println(“initializing…”);

// Play start up Track
Serial.println(“track 3”);
mp3_play (3);
}
void loop() {
//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
//print out the value of the pushbutton
Serial.println(sensorVal);

// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);

// HOME check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState2 == LOW) {
// play track 1:
Serial.println(“track 1”);
mp3_play (1);
//mp3.playMp3FolderTrack(1); // sd:/mp3/0001.mp3
delay(2500);
// turn LED on:
digitalWrite(ledPin1, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin1, LOW);
}

// AWAY check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState3 == LOW) {
// turn LED on:
digitalWrite(ledPin2, HIGH);
// play track 2:
Serial.println(“track 2”);
//mp3.playMp3FolderTrack(2); // sd:/mp3/0002.mp3
delay(2500);
} else {
// turn LED off:
digitalWrite(ledPin2, LOW);
}
}

I’ve had good luck with the DFPlayer_Mini_Mp3_by_Makuna library. Make sure the examples work.

1 Like

The library you’re using for the DFPlayerMini is deprecated and the Makuna library requires a rather strange and complicated interface.

For a simpler and more flexible library, I suggest using DFPlayerMini_Fast.h instead. The library is installable through the Arduino IDE’s Libraries Manager and example code ships with the source code.

Power_Broker:
The library you're using for the DFPlayerMini is deprecated and the Makuna library requires a rather strange and complicated interface.

For a simpler and more flexible library, I suggest using DFPlayerMini_Fast.h instead. The library is installable through the Arduino IDE's Libraries Manager and example code ships with the source code.

Said the author of the library. I have had no trouble with the Makuna library.

1 Like

Power_Broker:
The library you're using for the DFPlayerMini is deprecated and the Makuna library requires a rather strange and complicated interface.

For a simpler and more flexible library, I suggest using DFPlayerMini_Fast.h instead. The library is installable through the Arduino IDE's Libraries Manager and example code ships with the source code.

Ok Thank you, I'll give it a look. I'm still figuring things out a bit which is why I was trying to modify an existing sketch. After installing the library, do you know what changes might need to be made? or why the LED on the "Home" bit of script does not work? I've tried different pins and still nothing.

Thanks again!

If the LED isn't working, you either have it wired backwards or you burned it out. How do you have it wired up?Why not write a simple sketch to just toggle the LED and test it?

blh64:
If the LED isn’t working, you either have it wired backwards or you burned it out. How do you have it wired up?Why not write a simple sketch to just toggle the LED and test it?

No, the LED is working fine. wired the same as the working one just on a different pin. (via a 120 OHM resistor)

I moved the code around from the first draft so that is exactly mirrors the “AWAY” portion of the sketch, and it sill does not work… HOWEVER After I trigger the “away” IR, the LED comes on, delays, and then instead of shutting off, the “Home” AND “Away” LED comes on, repeats the delay and then shuts off. So I know both LEDs are good.

It’s as if it is waiting for me to trigger the “away” section of the sketch, and then Looping the sketch, However there is still no sound, and the monitor shows Track1 or Track2 just as is intended.

I have been testing different options nothing is working.
Right now I have uploaded the Makuna library example "PlayMp3" and the same thing is happening, There is no sound But when I trigger the Module Via. the IO pins on the DFplayermini, it plays the tracks perfectly.

I have switched between 2 Different Arduino Nanos, and 2 different DFplayer mini's, all with the same result.

All I DO hear is a VERY small crackle from the Speakers every 5 seconds, perhaps corresponding with the delay in the test sketch.

I have the:
RX connected to Arduino Tx (11) ((via. 1k resistor)
TX Connected to Arduino Rx (10)

Vcc to 5v, Gnd to Gnd, Speakers to Gnd and Spk1 and spk2

What Am I doing wrong?

blh64:
Said the author of the library. I have had no trouble with the Makuna library.

I didn't say the Makuna library didn't work, but requiring users (many beginners) to create their own class with callback functions just so that they can instantiate the main class (which is templated for some reason) is ridiculous...

DFPlayerMini_Fast.h is so much simpler to setup, use, and wrap your head around.

Can you post a (nicely) hand drawn schematic? Have you tried using a separate power supply for the MP3 player?

Power_Broker:
Can you post a (nicely) hand drawn schematic? Have you tried using a separate power supply for the MP3 player?

Yes, I have tried a separate pwr supply, both at 5v and at 3.3v and still have exactly the same result.

I have tried reformatting the SD card adding the MP3’s in order, and also loading sketch onto an uno incase it was a compatibility problem with the nanos. But all returned the same result.

a little clicking in the spkr and does not play the tracks, and problems with one led not lighting as intended. AND still no problem playing tracks when grounding the IO pins, so i know its not the speakers the leds or the dfplayer mini.

Schematic is a bit crude, but better then my had righting. The Ir break beams are 2 Ir transmitters and receivers in a package. But I have used them many times in the past and after testing they are working exactly as intended (as switches) as well.

You said you ran the playMP3 example sketch. What did the serial monitor display? Are you getting all the initialization and informational messages?

blh64:
You said you ran the playMP3 example sketch. What did the serial monitor display? Are you getting all the initialization and informational messages?

No, it just says:

\⸮Д⸮⸮`⸮⸮⸮ ⸮⸮⸮⸮⸮⸮⸮⸮⸮

Adds another ? or 2 every time I hear the small crackle about every 5 seconds.

Is the Serial Monitor configured to match the baud rate of the example code (9600)?

blh64:
Is the Serial Monitor configured to match the baud rate of the example code (9600)?

Ah sorry, I'm still learning.

PlayMp3 example says:
"Serial.begin(115200)"

When I set it to 115200 it says this:

-> initializing...
-> volume 24
-> files 5
-> starting...
-> track 1
->
-> Com Error 6
-> track 2
->
-> Com Error 6
-> track 3
->
-> Com Error 6
-> track 1

I guess my problem is "com error 6"?

And why does it say "5 files" the SD card only has 3 files on it (0001,0002,0003)?

Also, I changed it to 9600 and re-uploaded, and am getting the same result.

Com Error 6 (if you look into the .cpp file) is a DfMp3_Error_FileMismatch error.

From this link, it means DfMp3_Error_FileMismatch = 6 - (see chip documentation) Can not find file.

Are the files in a mp3 folder? With the proper names?, etc.

blh64:
Com Error 6 (if you look into the .cpp file) is a DfMp3_Error_FileMismatch error.

From this link, it means DfMp3_Error_FileMismatch = 6 - (see chip documentation) Can not find file.

Are the files in a mp3 folder? With the proper names?, etc.

Thank you, that helped, problem was in that the file names we correct but needed to be in a folder named "mp3". I had tried that before, However, I came across a post where someone had mentioned that he needed to add the files sequentially when copied to the SD card. I don't know if that helped, but at least its playing now.

HOWEVER!

for the example sketch "PlayMp3" 2 things happen that I don't quite understand that I'd like some help understanding for the sketch I'm ultimately trying to build.

  1. on reset; track one starts to play, and then is interrupted sharply by track one being played, this time NOT interrupted.

  2. When Playing tracks 1 and 2 (which are 7 seconds long) the track fades out at the end. I extended the delay from 5000MS to 7000 then to 10000, and tho the delay was correct, the tracks still fade out.

Whats causing the fade out? and the quick play of track one?

Thanks!

I think I may have solved the fade out portion. Its with the MP3 file itself.

However it (and all the examples from all the other libraries I downloaded for the DFplayer mini) play track 1 on start up, and then start track 1 over at about 3 seconds in, then plays the full track and runs the loop normally after that.

Ok, So I was playing around a bit, working on writing the sketch as I want it to work. and I came up with this.
Everything works Perfectly with the exception of the “start Up track”

When it starts up, I would like it to play track 0003.mp3 once and then start the loop.

Right now it does not.

Can anyone help?

Thank you!

// Sketch, plays 3 mp3 files one on start-up and 2 more triggered by switches.
// it expects the sd card to contain these three mp3 files
// but doesn’t care whats in them
//
// sd:/mp3/0001.mp3
// sd:/mp3/0002.mp3
// sd:/mp3/0003.mp3

const int buttonPin2 = 4; // Home IR
const int buttonPin3 = 2; // Away IR
const int buttonPin4 = 3; // not used (future option)
const int ledPin1 = 8; // Home Led
const int ledPin2 = 12; // Away Led
int buttonState2 = 0; // variable for reading the pushbutton status
int buttonState3 = 0; // variable for reading the pushbutton status
int buttonState4 = 0; // variable for reading the pushbutton status

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

// implement a notification class,
// its member methods will get called
//
class Mp3Notify
{
public:
static void PrintlnSourceAction(DfMp3_PlaySources source, const char* action)
{
if (source & DfMp3_PlaySources_Sd)
{
Serial.print("SD Card, ");
}
if (source & DfMp3_PlaySources_Usb)
{
Serial.print("USB Disk, ");
}
if (source & DfMp3_PlaySources_Flash)
{
Serial.print("Flash, ");
}
Serial.println(action);
}
static void OnError(uint16_t errorCode)
{
// see DfMp3_Error for code meaning
Serial.println();
Serial.print("Com Error ");
Serial.println(errorCode);
}
static void OnPlayFinished(DfMp3_PlaySources source, uint16_t track)
{
Serial.print(“Play finished for #”);
Serial.println(track);
}
static void OnPlaySourceOnline(DfMp3_PlaySources source)
{
PrintlnSourceAction(source, “online”);
}
static void OnPlaySourceInserted(DfMp3_PlaySources source)
{
PrintlnSourceAction(source, “inserted”);
}
static void OnPlaySourceRemoved(DfMp3_PlaySources source)
{
PrintlnSourceAction(source, “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()
{

// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);

Serial.begin(9600);

Serial.println(“initializing…”);

mp3.begin();

uint16_t volume = mp3.getVolume();
Serial.print("volume ");
Serial.println(volume);
mp3.setVolume(24);

uint16_t count = mp3.getTotalTrackCount(DfMp3_PlaySource_Sd);
Serial.print("files ");
Serial.println(count);

Serial.println(“starting…”);
}

void waitMilliseconds(uint16_t msWait)
{
uint32_t start = millis();

while ((millis() - start) < msWait)
{
// calling mp3.loop() periodically allows for notifications
// to be handled without interrupts
mp3.loop();
delay(1);

//play startup track
Serial.println(“track 3”);
mp3.playMp3FolderTrack(3); // sd:/mp3/0003.mp3

}
}

void loop()
{

//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
//print out the value of the pushbutton
Serial.println(sensorVal);

// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);

// HOME check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState2 == LOW) {
//turn LED on:
digitalWrite(ledPin1, HIGH);
// play track 1:
Serial.println(“track 1”);
mp3.playMp3FolderTrack(1); // sd:/mp3/0001.mp3
delay(9000);
} else {
// turn LED off:
digitalWrite(ledPin1, LOW);
}

// AWAY check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState3 == LOW) {
// turn LED on:
digitalWrite(ledPin2, HIGH);
// play track 2:
Serial.println(“track 2”);
mp3.playMp3FolderTrack(2); // sd:/mp3/0002.mp3
delay(9000);
} else {
// turn LED off:
digitalWrite(ledPin2, LOW);
}
}

You put the code to play track3 inside the waitMilliseconds() function. Put it inside the setup() function at the end if you want it to play once.