How to use DFPlayer mini to play MP3

What is a DFPlayer?

The DFPlayer Mini MP3 Player For Arduino is a small and low price MP3 module with an simplified output directly to the speaker. The module can be used as a stand alone module with attached battery, speaker and push buttons or used in combination with an Arduino UNO or any other with RX/TX capabilities. Know More

dfplayer.jpg

Wiring

Wiring up the sensor is quite complex, the pins are not labelled so you will have to refer to the pinout. dfplayerpinout.png

DFplayer Mini Wiring
  1. VCC to 5V (Power)
  2. RX to D2 via 1K resistor
  3. TX to D3
  4. SPK_1 to Speaker(+) red wire
  5. GND to GND (Ground)
  6. SPK_2 to Speaker(-) black wire
potentiometer Wiring
  1. right pin to 5V (Power)
  2. middle pin to A0 (Signal)
  3. left pin to GND (Ground) dfplayermini_bb.png

File handling

The order you copy the mp3 into micro SD card will affect the order mp3 played , which means play(1) function will play the first mp3 copied into micro SD card.

MAC User Attention!
If you are using Mac OS X to copy the mp3, the file system will automatically add hidden files like: "._0001.mp3" for index, which this module will handle as valid mp3 files.

It is really annoying. So you can change YourSDCardName in the below command and then run it in terminal to eliminate those files.

dot_clean /Volumes/<YourSDCardName>

Library

DFRobotDFPlayerMini library will be used for this module. We have a tutorial on how to install a library here.

Get Started

In this example, we are using the potentiometer control two audios. It will play the first audio when the potentiometer turns to right and play the second when it turns to left.

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

const int pot = A0;
int potValue = 0;

// Create the Player object
DFRobotDFPlayerMini player;

void setup() {

  pinMode(pot, INPUT);

  // 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);
  } else {
    Serial.println("Connecting to DFPlayer Mini failed!");
  }      
}

void loop() {

	potValue = analogRead(pot);

	if(potValue > 500 ){ 

	 static unsigned long timer = millis();
 	 if (millis() - timer > 2000) { //2000 is the duration of the audio(1)
  		timer = millis();

   		//(2) is the 2rd file in the sd card, the order = the order you copied the file to it
   		player.play(2);  
  }
  
	}else {
  	  static unsigned long timer = millis();
  
 	 if (millis() - timer > 3000) { //3000 is the duration of the audio(2)
  	  	timer = millis();
   		player.play(1); 
  		}
	}
}

Revision #2
Created 24 November 2022 16:28:08 by Joanne Leung
Updated 12 January 2023 16:46:51 by Joanne Leung