Skip to main content

DFPlayer Mini

The DFPlayer Mini is a small (cheap!) and flexible mp3 player unit that can be controlled via an Arduino. Unless you have a specific need for a 'trigger' function (where individual tracks are tripped using specific linked buttons) it's a nifty alternative to the larger and more expensive Sparkfun mp3 trigger.

Connections: rx and tx connected to 0 and 1 of arduino, connect DFPlayer rx through a 1k resistor)

There's a number of different Arduino libraries for communicating with the DFPlayer on line, though the simplest is probably the one made by the manufacturers, DFRobotDFPlayerMini. Install it using the Arduino library manager, instructions here.

This sample code is adapted from the library's 'Getting Started' example, and will play tracks while also showing any error messages over the serial monitor.

#include "DFRobotDFPlayerMini.h"

DFRobotDFPlayerMini myDFPlayer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(9600);
  
  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

  //Use softwareSerial to communicate with board
  if (!myDFPlayer.begin(Serial1)) {  
    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);
  }
 
  Serial.println(F("DFPlayer Mini online."));

  myDFPlayer.volume(0);
  delay(100);

  myDFPlayer.volume(15);  //Set volume value. From 0 to 30
  myDFPlayer.play(1);  //Play the first mp3. to loop, exchange 'play' for 'loop'
}

void loop()
{
  if (myDFPlayer.available()) {
    printDetail(myDFPlayer.readType(), myDFPlayer.read()); 
  }
}

void printDetail(uint8_t type, int value){
  switch (type) {
    case TimeOut:
      Serial.println(F("Time Out!"));
      break;
    case WrongStack:
      Serial.println(F("Stack Wrong!"));
      break;
    case DFPlayerCardInserted:
      Serial.println(F("Card Inserted!"));
      break;
    case DFPlayerCardRemoved:
      Serial.println(F("Card Removed!"));
      break;
    case DFPlayerCardOnline:
      Serial.println(F("Card Online!"));
      break;
    case DFPlayerPlayFinished:
      Serial.print(F("Number:"));
      Serial.print(value);
      Serial.println(F(" Play Finished!"));
      break;
    case DFPlayerError:
      Serial.print(F("DFPlayerError:"));
      switch (value) {
        case Busy:
          Serial.println(F("Card not found"));
          break;
        case Sleeping:
          Serial.println(F("Sleeping"));
          break;
        case SerialWrongStack:
          Serial.println(F("Get Wrong Stack"));
          break;
        case CheckSumNotMatch:
          Serial.println(F("Check Sum Not Match"));
          break;
        case FileIndexOut:
          Serial.println(F("File Index Out of Bound"));
          break;
        case FileMismatch:
          Serial.println(F("Cannot Find File"));
          break;
        case Advertise:
          Serial.println(F("In Advertise"));
          break;
        default:
          break;
      }
      break;
    default:
      break;
  }
}