- Introduction
- Hardware Requirements
- Software Requirements
- Installation
- Example Usage
- Connections
- Troubleshooting
- Images
- License
- Acknowledgments
- Contributing
- Version History
The DFPlayer Mini is a compact and affordable MP3 player module that can be used with Arduino and ESP32 boards. This library provides a streamlined and easy-to-use interface specifically for the ESP32 platform, allowing for efficient MP3 playback and control.
- ESP32 board (e.g., ESP32 DevKitC, ESP32 WROVER, etc.)
- DFPlayer Mini module
- MicroSD card with MP3 files
- Speaker or headphones
- Jumper wires
- Arduino IDE (version 1.8.x or later)
- ESP32 board support package (version 1.0.4 or later)
- DFRobotDFPlayerMini Library (for reference and usage)
- Download the Library: Clone or download the DFPlayer Mini library from DFRobot's GitHub repository.
- Extract the Library: Unzip the downloaded file if necessary.
- Add to Arduino IDE:
- Open the Arduino IDE.
- Navigate to
Sketch > Include Library > Add .ZIP Library
. - Select the extracted
.zip
file and clickOpen
.
- Open Arduino IDE.
- Navigate to
File > Preferences
. - Add the URL
https://dl.espressif.com/dl/package_esp32_index.json
to the "Additional Boards Manager URLs" field. - Go to
Tools > Board > Boards Manager
. - Search for
esp32
and clickInstall
.
The following example code demonstrates how to use the DFPlayer Mini library to play MP3 files on an ESP32 board. Ensure that your MP3 files are named and organized correctly on your TF card as specified below.
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
// Define the serial port for communication with the DFPlayer Mini
#define FPSerial Serial2
// Create an instance of the DFPlayer Mini
DFRobotDFPlayerMini myDFPlayer;
// Function to print detailed messages from the DFPlayer Mini
void printDetail(uint8_t type, int value) {
// Handle messages based on type
}
// Function to play a specific sound file with a given duration
void playSound(int soundNumber, int duration) {
myDFPlayer.play(soundNumber);
delay(duration * 1000); // Convert duration from seconds to milliseconds
}
void setup() {
FPSerial.begin(9600);
Serial.begin(115200);
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(FPSerial)) {
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); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(30); // Set volume value. From 0 to 30
}
void loop() {
// Example usage: Play sound file 1 for 5 seconds
playSound(1, 5);
// Add more code here to play other sound files or implement other functionality
}
Important: MP3 File Organization
- Create a Folder: On your TF card (microSD card), create a folder named
mp3
in the root directory. - Organize MP3 Files: Place your MP3 files inside this
mp3
folder. Name the files in the format0001.mp3
,0002.mp3
,0003.mp3
, and so on.
Example Directory Structure:
/mp3
/0001.mp3
/0002.mp3
/0003.mp3
Ensure that you follow this naming and directory structure, as the DFPlayer Mini expects to find MP3 files in this specific format.
Connect the DFPlayer Mini module to the ESP32 as follows:
- VCC: Connect to 5V on the ESP32 board
- GND: Connect to GND on the ESP32 board
- RX: Connect to TX on the ESP32 board (Serial2)
- TX: Connect to RX on the ESP32 board (Serial2)
- SPK1: Connect to speaker or headphones
- SPK2: Connect to speaker or headphones (optional)
- Ensure Proper Connections: Verify that the DFPlayer Mini is correctly connected to the ESP32.
- Check SD Card: Ensure the microSD card is inserted and contains MP3 files in the correct directory and format.
- Verify Audio Output: Confirm that the speaker or headphones are properly connected.
- Reset Module: If the DFPlayer Mini is unresponsive, try resetting it by pressing the reset button.
Here is the circuit diagram for connecting the DFPlayer Mini to the ESP32:
Credits: The circuit diagram was created by Techiesms. For further details, refer to their YouTube video.
This library is released under the MIT License.
This library is based on the DFRobotDFPlayerMini Library by DFRobot. Thanks to DFRobot for the original code and contributions.
Contributions to this library are welcome. Please submit pull requests to this repository.
- 1.0.0: Initial release