Arduino + Dfplayermini and buttons to trigger MP3 files

Hi all,

I have setup an Arduino Uno with a Dfplayer Mini to play an MP3 file on SD card when the button on the actual Arduino is pressed.

However, I am wanting to use a number of arcade buttons (up to 10 if possible) to trigger 10 different sounds (one sound for each button)

I am fairly new to coding in general and all I can find online are tutorials how to make an MP3 player, this is not what I am wanting.

Any help on how to trigger sounds with button presses would be most appreciated.

Kind regards

Here is my code to play an MP3 file on button press:

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

// Use pins 2 and 3 to communicate with DFPlayer Mini
static const uint8_t PIN_MP3_TX = 2; // Connects to module's RX
static const uint8_t PIN_MP3_RX = 3; // Connects to module's TX
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

// Create the Player object
DFRobotDFPlayerMini player;

void setup() {

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

  // Start communication with DFPlayer Mini
  if (player.begin(softwareSerial)) {
    Serial.println("OK");

    // Set volume to maximum (0 to 30).
    player.volume(30);
    // Play the first MP3 file on the SD card
    player.play(1);
  } else {
    Serial.println("Connecting to DFPlayer Mini failed!");
  }
}

void loop() {
}
1 Like

If you can "digitalRead pushbuttons" and turn on LEDs as a result, then the principle is the same - digitalReading pushbuttons resulting player commands.

PE - Actually, you don't even need LEDs. digitalRead pushbuttons to result Serial.prints ("pb one", "pb two", "pb three") would be that much nearer to your Objective.

Hello Runaway Pancake, after finding some code which does exactly what I want

https://create.arduino.cc/projecthub/muhammad-aqib/arduino-button-tutorial-using-arduino-digitalread-function-08adb5

I have tried to match my pinouts and board layout to the code and I cannot for the life of me get the button push to work.

I have been on this for 4 hours now and i'm sure it's something simple.

Could you be so kind as to check the code here to my board pinouts.

// 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 = 8;     // the number of the pushbutton pin
const int buttonPin3 = 9;     // the number of the pushbutton pin
const int buttonPin4 = 10;     // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin
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 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(3, 2); // RX, TX
DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);

void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, 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();
}


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);

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

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

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState4 == LOW) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    // play track 3:
    Serial.println("track 3");
    mp3.playMp3FolderTrack(3);  // sd:/mp3/0003.mp3
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Kind regards



What is that supposed to mean?
Does it not Serial.print track 1 ?
Does it not play the track?

That class Mp3Notify stuff hardly seems necessary, but haven't worked that library (DFMiniMp3).
DFRobotDFPlayerMini.h is all that I've done with.

How about a simple sketch to prove out the pushbutton?

Previously, I had suggested that you do some groundwork, a simple experiment that would lead to understanding what you're doing.

I see that you have some kind of clone there, too.

This sketch will prove out your wiring and buttonpushing.

void setup() {
  Serial.begin(19200);
  Serial.print("\r\n\r\nSerMon ok\r\n");
  
  pinMode(pb2, INPUT_PULLUP);
  pinMode(pb3, INPUT_PULLUP);
}

void loop() {
  if (!digitalRead(pb2))
  {
    Serial.println("PB2");
    delay(200);
  }
  if (!digitalRead(pb3))
  {
    Serial.println("PB3");
    delay(200);    
  }
}

Looks like you are not connecting the button pin to GND when pressed. You have it connected to the GND rail of the breadboard... but you need to connect that to the GND of the Arduino.

Hi red_car, I have now attached the GND to the Arduino and it displays "Track 1" on the serial monitor when pushed. However it doesn't play the MP3 track.

I have tried the files in a folder called MP3 and also in the root directory also with no luck.

Is it anything to do with these two lines? As i'm guessing some kind of communication between the DFplayer and Arduino is not happening.

SoftwareSerial secondarySerial(3, 2); // RX, TX
DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);

Many thanks to both replies!

Are you sure you have the Tx and Rx connections the correct way around?

TX and RX coming from the DFmini match up to the correct pin locations on the Arduino.

Just makes a strange clicking/glitch sound when the button is held

Or could it be a problem with this part

mp3.playMp3FolderTrack(1);

What does that mean? Does Rx connect to Tx and vice versa?

yes it does connect correctly, if it is switched, it displays Error code 3

What? When did you mention this? When does this occur?

When i switched the TX and RX (just to make sure they were on correctly), it displayed error code 3 in the serial monitor. When I switched it back, the error was no more.

Com Error 3 i mean.

yes! the push button works and sends a message to the serial monitor

Seriously?... so when you received a response message you determined that things were not wired correctly? The presence of the response indicates that serial is working !

The error message means there is something else wrong, but you will never find out what that is if you connect the serial back to front.

You say that like everyone knows how to wire things up. It's people like you who look down on beginners. This forum and stack overflow are certainly full of people like you. Not once have I had a helpful response.

I didn't mean to offend you.

Just think about it for a second... you changed the wiring... and you got a response on the monitor... that means that it is now wired correctly.

The error message you got is now what you need to focus on.

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