How to verify if an audio file has completed playing on the DFPlayer Mini

I'm working on building an electronic scale using load cells connected to an HX711 and a DFRobot DFPlayer Mini. When the weight meets certain criteria, the DFPlayer should play a specific audio file. The code is linked here.

This setup mostly works, but the audio starts playing within one of the if loops, and I currently have to add a delay to allow the audio to finish.

I need help with two things:

  1. Is there a way to check if the audio file has finished playing so that I can avoid using delays?
  2. If the weight remains on the scale, the if loop executes again, causing the audio to replay. How can I prevent this?

There is a busy pin on the DFPlayer Mini. When you launch a command you should wait for this pin to get active (the pin goes "LOW" when a song is playing if I remember well) as the command needs a bit of time to be decoded from the Serial line and then it will stay LOW while the sound is playing. When the audio is completed, it will read HIGH again and you'll know it's done.

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

Before entering this loop verify "first_time_in_this_loop" flag is clear. If the flag is set, you have entered the loop before (therefore, do not re-enter).

I completely missed this. I just looked through the Wiki and in their sample code, there seems to be a return value for myDFPlayer.readType() called DFPlayerPlayFinished. I will try this out too. Monitoring that pin after playing seems like a surefire way to detect playback completion.

Will read! Thanks.

I use the busy pin to know if a song is playing. Works fine.
But how can I STOP playing and reset the player with a press on a button ?
e.g. :
if (digitalRead(x) == 0)
{
.?.?. // Stop playing and reset player
}

Thanks in advance !

There's a stop function in the library. All it apparently does is send 0x16 to the player.

You could assign an interrupt to your switch pin. When pushed, the code lands in the ISR where you could send the command to stop playback.

You could alternatively, keep monitoring the BUSY pin status in a while loop after playback. Inside this loop you can check for button state and stop playback.

wow "wildbill" and "electrphile"! that was fast ! thanks * 1000 !
A combination of both your replies most likely will do the trick.
Lemme try this and I'll come back here with the results of my testing. (could take a while 'cause I'm not familiar with interrupts... :sunglasses:

BTW. Here's the complete thing I want to achieve :
I need to incorporate the mp3 player in an alarm clock based on ESP32.
At a certain time I play 1.mp3 and do a few other things (e.g. simulate sunrise with LED's -> this works fine) The mp3 has to keep playing until it ends OR until I press a button.

I'll be BACK...

The interrupt is not advised as you speak to the DFPlayer Mini through SoftwareSerial and in the context of the interrupts, the interruptions are disabled so SoftwareSerial is not really sending anything. The buffering likely works so that could be fine, but if the outgoing buffer was full for some reason, you would end up stuck in the ISR.

In practice there is no need to go for an interrupt if you keep the loop looping and check both the busy pin and the stop button pin as possible conditions to go do something else.

OK, thanks JML. I think you're right ! (I don't use SoftwareSerial because I use Serial2 of the ESP32) so after all, interrupts won't do a good job for me. Also, I can't add another key to "stop" because all the GPIO's of my ESP32(30pin) are already in use : This project is not ONLY an alarm clock, but also will make it possible to

  • SET colors (RGB) at any time
  • randomly change colors (with adjustable speed)
  • mimic sun down with a single push on a button.

So I have GPIO's in use for :

  • 4 potentiometers -> to be used to set colors (and speed) when not in "alarm mode"
  • 4 push buttons -> 1 to set alarm on/off, 2 to set other variables, 1 for sunset.
  • 3 for rotary encoder -> set alarm time
  • 3 for pwm LED's (RGB)
  • 5 for ISP display

I made a PCB (and test-setup) -> see pic) for this project and mounted all parts, and everything (except for the "stop playing mp3" :frowning: ) works just fine.

test setup pic

may be you can say that when audio is playing, pushing any button means stop...

I think you have way too many buttons :wink:
with the right coding you could use just one rotary encoder and its builtin button to drive a menu that would let you set colors, speed, alarm, .... That would save a lot of pins and hardware (at the expense may be the user interface is less direct)

at the expense may be the user interface is less direct

LOL yeah, you're right here too ! :slight_smile:
If I would use it myself : YES ! Just ONE rotary would be enough for the whole project! :innocent:
but I make it for my niece who wants it as easy as possible to operate. Also, I do understand that when you just wake up, you don't want to "mess around" in menu's etc. She wants a colored button for this and another color for that ...
For the moment, I stop the sunrise with a push on the red button (works fine -> full white light for 5 mins, then off) , but I also want to stop the music at that time. (This red button also serves as "stop" for the sundown...)

PS : the button on the rotary serves as acknowledge when setting alarm hour and minute (3rd GPIO for rotary)

nevertheless : thanks for your coöperation !!!
I (we? :sunglasses:) keep searching for a solution to suit my poor niece...

How difficult can it be ? :innocent:

void loop()
{
  mp3Player.play(1);
  Serial.println("START");
  delay(1000);
  while (digitalRead(5) == 0)
  {
    // other code (e.g. update clock display)
    if (digitalRead(27) == 0) // red button
    {
      Serial.println("STOP");
      mp3Player.stop();
      delay(500);
    }
  }
  delay(5000); //  or back to where we came from...
} //

Again : 100000000000 x thanks for your interest, time !!!

PS. digitalRead(5) = busy from DFPlayer

this would be easier to read if you had defined

const byte dfPlayerBusyPin = 5;
#define BUSY LOW

then you could write

  while (digitalRead(dfPlayerBusyPin) == BUSY) {
    ...
  }