DFPlayer Mini Arduino Uno + Buttons

Hi guys,

I am a novice programmer and first time using Arduino. I am Using an Uno to control a DFPlayer Mini through the use of tactile buttons to play specific audio files. I can confirm all three buttons work and return the serial message that they are playing their respective file, however only button 3 successfully plays the file. When the other buttons are pressed, the DFPlayer flashes and no audio is played. Can't work out if it's my code telling it to play files that don't exist, or if it's a problem with the DFPlayer. The files are named 0001.mp3, 0002.mp3 and 0003.mp3.

Just want to push button 1 and play track one, push button 2 and play track 2 etc..

Can anyone help?

TIA!

/* 
To create audio: https://ttsmaker.com/
DFPlayer module supports up to 3W. 
SD card: 2GB ~ 32GB formatted with FAT or FAT32. 
MP3 / WAV - audio files. 
*/

#include "SoftwareSerial.h"         // Include the SoftwareSerial library for serial communication
#include "DFRobotDFPlayerMini.h"    // Include the DFRobotDFPlayerMini library for the DFPlayer Mini module

SoftwareSerial mySoftwareSerial(10, 11); // Create a software serial connection on pins 10 (RX) and 11 (TX)
DFRobotDFPlayerMini myDFPlayer;          // Create a DFPlayerMini object

const int pushButtonPin1 = 2;
const int pushButtonPin2 = 3;
const int pushButtonPin3 = 4;        // Define the pin number for the push button
bool buttonState1;
bool buttonState2;
bool buttonState3;                   // Variable to store the state of the button
bool wasButton1Pressed = false;
bool wasButton2Pressed = false;
bool wasButton3Pressed = false;      // Flag to track whether the button was previously pressed

void setup() {
  mySoftwareSerial.begin(9600);     // Start software serial communication at 9600 baud rate
  Serial.begin(115200);             // Start serial communication at 115200 baud rate
  pinMode(pushButtonPin1, INPUT_PULLUP); // Set push button pin as input with internal pull-up resistor
  pinMode(pushButtonPin2, INPUT_PULLUP);
  pinMode(pushButtonPin3, INPUT_PULLUP);

  if (!myDFPlayer.begin(mySoftwareSerial)) { // Initialize the DFPlayer Mini module
    Serial.println(F("Not initialized:"));
    Serial.println(F("1. Check the DFPlayer Mini connections"));
    Serial.println(F("2. Insert an SD card"));
    while (true);                  // If initialization fails, print error messages and halt the program
  }
  
  Serial.println();
  Serial.println(F("DFPlayer Mini module initialized!")); // Print initialization success message
  myDFPlayer.setTimeOut(500);       // Set the timeout value for serial communication
  myDFPlayer.volume(30);            // Set the volume level (0 to 30)
  myDFPlayer.EQ(0);                 // Set the equalizer setting (0: Normal, 1: Pop, 2: Rock, 3: Jazz, 4: Classic, 5: Bass)
}

void loop() {
  buttonState1 = digitalRead(pushButtonPin1); // Read the state of the button

  if (buttonState1 == LOW) {          // If the button is pressed (assuming pull-up configuration)
    if (!wasButton1Pressed) {         // If the button was not previously pressed
      Serial.println("Playing song 1");
      myDFPlayer.play(1);            // Play the first audio file on the SD card
      delay(2000);                   // Delay for 2 seconds

      wasButton1Pressed = true;       // Set the flag to true to indicate that the button was pressed
    }
  } else {                           // If the button is not pressed
    Serial.println("Push button not pressed");
    wasButton1Pressed = false;        // Reset the flag when the button is released
  }
  buttonState2 = digitalRead(pushButtonPin2); // Read the state of the button

  if (buttonState2 == LOW) {          // If the button is pressed (assuming pull-up configuration)
    if (!wasButton2Pressed) {         // If the button was not previously pressed
      Serial.println("Playing song 2");
      myDFPlayer.play(2);            // Play the second audio file on the SD card
      delay(2000);                   // Delay for 2 seconds

      wasButton2Pressed = true;       // Set the flag to true to indicate that the button was pressed
    }
  } else {                           // If the button is not pressed
    Serial.println("Push button not pressed");
    wasButton2Pressed = false;        // Reset the flag when the button is released
  }
  buttonState3 = digitalRead(pushButtonPin3); // Read the state of the button

  if (buttonState3 == LOW) {          // If the button is pressed (assuming pull-up configuration)
    if (!wasButton3Pressed) {         // If the button was not previously pressed
      Serial.println("Playing song 3");
      myDFPlayer.play(3);            // Play the third audio file on the SD card
      delay(2000);                   // Delay for 2 seconds

      wasButton3Pressed = true;       // Set the flag to true to indicate that the button was pressed
    }
  } else {                           // If the button is not pressed
    Serial.println("Push button not pressed");
    wasButton3Pressed = false;        // Reset the flag when the button is released
  }
}

post mfrc522-dfplayer-wifi-ap may give you some ideas

also have a look at Voice Sound Playback Module Arduino MP3 Player Module UART I/O - you can connect upto eight buttons to IO0 to IO7 - take to ground to play associated MP3 file - all done on the module and removes the requirement for the UNO

1 Like

This looks ideal, however ideally want to be able to connect 10 buttons

Reorganise the files on your SD card. Make a folder called "MP3" and move the audio files into that.

Next, change your code to use

myDFPlayer.playMp3Folder(3);

for example. I have found this method to work much more reliably than leaving the files in the root folder and using .play().

1 Like

I recommend you stop copy & pasting code. Your code is already 3 times longer than it should be. You don't want to make it 10 times longer than it should be! Learn to use arrays.

I would also recommend to think about using a matrix for your buttons, to save Arduino pins. This will require 7 pins instead of 10, which may not sound that great. But if you later want 20 buttons, this will need only 9 pins rather than 20.

There is a clever technique called charlieplexing that will allow you to read 10 buttons with 4 pins or 20 buttons with 5 pins. But that is quite advanced for a beginner, so ask the forum about that when you have more experience.

1 Like

in addition to independent mode selecting one from eight files there is Integrated mode which enables selecting from 255 files - have a look at the section I/O Integrated Mode 0 (key combination playing) in MP3 Playback Audio Module with Amplified Output – DY-HV8F

1 Like

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