DF Player Mini - Reading the Busy Pin to Start a function

I have a project that is using the Busy pin on my DF Player Mini to start a light sequence based on a whether the mp3 is playing. At first the approach seemed pretty straightforward, but after implementing the wiring and DigitalRead and my IF statements, I havent seen any responses.

Full Code:

// Tue 16 Aug 2023; Added Busy pin input for mp3 player

#include <DFRobotDFPlayerMini.h>
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define BUTTON_PIN 4
#define BUSY_PIN 8




bool buttonState = 1; // Normally high, goes low when pressed
bool previousButtonState = 1; // Because we will test when CHANGED

///////////////Create the Player object/////////////////////////////
DFRobotDFPlayerMini player;

static const uint8_t PIN_MP3_TX = 2; // UNO Tx connects module's RX
static const uint8_t PIN_MP3_RX = 3; // UNO Rx connects module's TX
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

//////////////////////////////////////////////////////////////////


/////////////////Create Fairy Light Variables////////////////////

int led = 11;           // the PWM pin the LED is attached to
int brightness = 3;    // how bright the LED is
int fadeAmount = 2;    // how many points to fade the LED by

////////////////////////////////////////////////////////////////

int busyState = 0;
int prevBusyState = 0;

void setup()
{

  ///////////////////////Button Trigger//////////////////////////

  pinMode (BUTTON_PIN, INPUT_PULLUP); // Avoids need for resistor

  ///////////////////////////////////////////////////////////////


  pinMode(BUSY_PIN, INPUT_PULLUP); //check to see if song is still playing



  ///////////////////////Music Functions/////////////////////////

  // Init USB serial port for debugging
  Serial.begin(115200);
  Serial.println();
  // Init serial port for DFPlayer Mini
  softwareSerial.begin(9600);
  delay(1000);

  // Start communication with DFPlayer Mini
  if (!player.begin(softwareSerial, 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);
    }
  }

  busyState = digitalRead(BUSY_PIN);
  prevBusyState = busyState;

  ///////////// End Music Functions/////////////////////////////


  ////////////////////Fairy Lights//////////////////////////////

  pinMode(led, OUTPUT); //declare pin 11 to be an output

  //////////////////End Fairy Light/////////////////////////////
}

void loop()
{

  buttonState = digitalRead(BUTTON_PIN);
  busyState = digitalRead(BUSY_PIN);

  if (busyState != prevBusyState) {
    if (busyState == LOW) {
      Serial.print ("song is playing. lights are go");

      /////////////////Fairy Light Fading Routine////////////////////////
      // set the brightness of pin 10:
      analogWrite(led, brightness);

      // change the brightness for next time through the loop:
      brightness = brightness + fadeAmount;

      // reverse the direction of the fading at the ends of the fade:
      if (brightness <= 1 || brightness >= 240) {
        fadeAmount = -fadeAmount;
      }
      // wait for 30 milliseconds to see the dimming effect
      delay(45);
      /////////////////////////////////////////////////////////////////
    }
    prevBusyState = busyState;
  }


  if (buttonState == LOW && previousButtonState == HIGH)
  {
    /////////////////////Play Song///////////////////////

    // The following NOT inside the previous if()
    // Set volume to maximum (0 to 30).
    player.volume(23);
    // Play the first MP3 file on the SD card
    player.play(1); // i.e. usually file 0001.mp3

    ////////////////////////////////////////////////////

    //if (!player.readState()) {}  //only resets the song when button is flipped

  }
  previousButtonState = buttonState;
}

I created my busy state variables

int busyState = 0;
int prevBusyState = 0;

Getting Data from Board

pinMode(BUSY_PIN, INPUT_PULLUP); //check to see if song is still playing

I Initialized my Busy States in my Setup

 busyState = digitalRead(BUSY_PIN);
  prevBusyState = busyState;

And in my loop, when a song is playing, the Busy state should trigger this function via the following code:

  buttonState = digitalRead(BUTTON_PIN);
  busyState = digitalRead(BUSY_PIN);

  if (busyState != prevBusyState) {
    if (busyState == LOW) {
      Serial.print ("song is playing. lights are go");

      /////////////////Fairy Light Fading Routine////////////////////////
      // set the brightness of pin 10:
      analogWrite(led, brightness);

      // change the brightness for next time through the loop:
      brightness = brightness + fadeAmount;

      // reverse the direction of the fading at the ends of the fade:
      if (brightness <= 1 || brightness >= 240) {
        fadeAmount = -fadeAmount;
      }
      // wait for 30 milliseconds to see the dimming effect
      delay(45);
      /////////////////////////////////////////////////////////////////
    }
    prevBusyState = busyState;
  }

According to the DF Player Mini docs, the Busy pin reads as "LOW" when a song is playing. So thats why I have it set as "==LOW".

if (busyState == LOW) {
      Serial.print ("song is playing. lights are go");

Right now, the only thing I'm getting out of my code is an mp3 file playing whenever I hit my button and resets whenever I hit the button. No light sequence. So we're 50% of the way there. Not a total failure!

pinMode(BUSY_PIN, INPUT_PULLUP); //check to see if song is still playing

I am not sure that you need the INPUT_PULLUP here as I would expect that the player will be keeping the pin HIGH or LOW unlike a pushbutton. Change it to INPUT and print its state each time you read it before testing its value

It would be helpful if you posted your whole sketch rather than posting it in parts

Sorry, I have the full code at the top of my post.

Lol, I was trying a thing, I guess it didn't work out. My earlier posts were just one onslaught of code and a question. Eventually I'll nail this "how to ask the perfect question". fingers crossed

So I updated the code and re-ran it, no errors, but also no responses from the Busy pin

// Wed 17 Aug 2023; Modified BUSY_PIN INPUT_PULLUP to INPUT to accomodate for player keeping track of it's own state of HIGH or LOW

#include <DFRobotDFPlayerMini.h>
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define BUTTON_PIN 4
#define BUSY_PIN 8




bool buttonState = 1; // Normally high, goes low when pressed
bool previousButtonState = 1; // Because we will test when CHANGED

///////////////Create the Player object/////////////////////////////
DFRobotDFPlayerMini player;

static const uint8_t PIN_MP3_TX = 2; // UNO Tx connects module's RX
static const uint8_t PIN_MP3_RX = 3; // UNO Rx connects module's TX
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

//////////////////////////////////////////////////////////////////


/////////////////Create Fairy Light Variables////////////////////

int led = 11;           // the PWM pin the LED is attached to
int brightness = 3;    // how bright the LED is
int fadeAmount = 2;    // how many points to fade the LED by

////////////////////////////////////////////////////////////////


int busyState = 0;
int prevBusyState = 0;

void setup()
{

  ///////////////////////Button Trigger//////////////////////////

  pinMode (BUTTON_PIN, INPUT_PULLUP); // Avoids need for resistor

  ///////////////////////////////////////////////////////////////


  pinMode(BUSY_PIN, INPUT); //check to see if song is still playing



  ///////////////////////Music Functions/////////////////////////

  // Init USB serial port for debugging
  Serial.begin(115200);
  Serial.println();
  // Init serial port for DFPlayer Mini
  softwareSerial.begin(9600);
  delay(1000);

  // Start communication with DFPlayer Mini
  if (!player.begin(softwareSerial, 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);
    }
  }

  busyState = digitalRead(BUSY_PIN);
  prevBusyState = busyState;

  ///////////// End Music Functions/////////////////////////////


  ////////////////////Fairy Lights//////////////////////////////

  pinMode(led, OUTPUT); //declare pin 11 to be an output

  //////////////////End Fairy Light/////////////////////////////
}

void loop()
{

  buttonState = digitalRead(BUTTON_PIN);
  busyState = digitalRead(BUSY_PIN);

  if (busyState != prevBusyState) {
    if (busyState == LOW) {
      Serial.print ("song is playing. lights are go");

      /////////////////Fairy Light Fading Routine////////////////////////
      // set the brightness of pin 10:
      analogWrite(led, brightness);

      // change the brightness for next time through the loop:
      brightness = brightness + fadeAmount;

      // reverse the direction of the fading at the ends of the fade:
      if (brightness <= 1 || brightness >= 240) {
        fadeAmount = -fadeAmount;
      }
      // wait for 30 milliseconds to see the dimming effect
      delay(45);
      /////////////////////////////////////////////////////////////////
    }
    prevBusyState = busyState;
  }


  if (buttonState == LOW && previousButtonState == HIGH)
  {
    /////////////////////Play Song///////////////////////

    // The following NOT inside the previous if()
    // Set volume to maximum (0 to 30).
    player.volume(23);
    // Play the first MP3 file on the SD card
    player.play(1); // i.e. usually file 0001.mp3

    ////////////////////////////////////////////////////

    //if (!player.readState()) {}  //only resets the song when button is flipped

  }
  previousButtonState = buttonState;
}

So you do. My mistake

@iramos85 is there a reason why you could not try @UKHeliBob 's suggestion?

Why is the topic marked as solved?

I dunno, that was either an accidental click on my part or who knows. That was not my intention.

I did include the suggestion, but I didn't get the desired result which is my attached LED to turn on when the mp3 plays when the BUSY signal was detected.

What do you see when you print the state of the BUSY signal ?

I dont know how else to show the results of my Serial Monitor other than showing the image so here you go:

You can select the text from the Serial monitor and paste it here in code tags

As to what you have shown, the baud rate of the Serial monitor needs to match that used in Serial.begin() in order to see sensible output

The busy pin takes time to activate. The serial command needs to be sent at 9600 bauds, interpreted by the module, the right file needs to be found on the SD card and playing starts. That’s only when all this is done that you’ll see the busy pin.

Not every change suggested to you on the forum is intended to fix your problem. They may be intended to help to diagnose the problem or narrow down the search for the problem by eliminating one of the possible causes. If you try these suggestions but don't report back what you found, we don't more forward.

I understand - my response wasn't a slight towards someone helping me. It was a "cause and effect" response.

Have you fixed the baud rate mismatch and tried again ?

ARGH. I see it now. Yes, I just fixed that mismatch. Okay, I'm getting a response from my Busy Pin.

(Through the course of this afternoon, i got overzealous and updated to IDE 2.0, please ignore the change of venue)

So I am getting a status message to my Serial Monitor when the song plays, but the LED still doesnt turn on. I was testing this with a simple two pin LED


Which has resulted in no sequence/turning on

Then I tried it using the lights that the finished project would use - Fairy Lights, connected by their two strands (one to LED input and the other to ground)


Which resulted in an always on state.

I can see no current limiting resistor in series with the LED. Do you have one ?

So i went and added a 10k resistor to my to the wire going to my input pin 11.

Which dampened the LED intensity, but doesn't shut it off entirely. I tried adding a 10k resistor to the ground cable as well, same results.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.