Jukebox on an Arduino Uno
A DFPlayer Mini over SoftwareSerial with DFRobot’s library: it plays the microSD card’s files one after another, hears each one end, and watches BUSY. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
Step by step, with the wiring and the common mistakes: Arduino DFPlayer Mini MP3 Player: Wiring and Code
What you'll learn
- Software serial for a second serial port
- Sending commands to a module instead of decoding audio
- Reading a BUSY pin to know when sound plays
- Wrapping a counter with the % operator
- Why a 1k resistor protects a 3.3 V input
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
The DFPlayer Mini decodes the music itself from its microSD card. The Uno only sends short commands over a software serial port: pin 10 listens and pin 11 talks, through a 1k resistor because the module's input is 3.3 V. The BUSY pin on pin 3 goes low while a file plays.
At startup the sketch sets the volume to 20, asks how many files are on the card, and plays file 1. When the module reports that a file finished, the sketch moves to the next one and wraps back to 1 after the last. The serial monitor prints playing and quiet.
Parts list
4 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × DFPlayer Mini MP3 moduleIn Halloween Prop Kit, Christmas Lights Kit, Laser Sword Kit.
- 1 × Resistor, 1k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 1k Ω pin 1 | Arduino Uno R3 pin 11 |
| DFPlayer Mini MP3 module pin TX | Arduino Uno R3 pin 10 |
| DFPlayer Mini MP3 module pin BUSY | Arduino Uno R3 pin 3 |
| DFPlayer Mini MP3 module pin VCC | Arduino Uno R3 pin 5V |
| DFPlayer Mini MP3 module pin GND | Arduino Uno R3 pin GND |
| Resistor, 1k Ω pin 2 | DFPlayer Mini MP3 module pin RX |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Jukebox: a DFPlayer Mini plays the files on its microSD card one after
// another, driven with DFRobot's library over a software serial port.
//
// DFPlayer VCC -> 5 V GND -> GND
// RX <- 1 k <- pin 11 (the module's inputs are 3.3 V)
// TX -> pin 10
// BUSY -> pin 3 (low while a file plays)
//
// The module does the decoding itself: the sketch only sends it ten-byte
// commands, "play file 1", "volume 20", and hears back when a file ends. The
// card on the canvas holds four files of three seconds each; there is no sound,
// but the module shows what it is playing.
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
const int BUSY_PIN = 3;
SoftwareSerial link(10, 11);
DFRobotDFPlayerMini player;
int track = 1;
int files = 0;
bool wasBusy = false;
void setup() {
Serial.begin(9600);
link.begin(9600);
pinMode(BUSY_PIN, INPUT);
if (!player.begin(link)) {
Serial.println("Unable to begin: check the wiring and the card");
while (true) {
}
}
player.volume(20);
files = player.readFileCounts();
Serial.print("Jukebox ready, ");
Serial.print(files);
Serial.println(" files");
player.play(track);
}
void loop() {
bool busy = digitalRead(BUSY_PIN) == LOW;
if (busy != wasBusy) {
wasBusy = busy;
Serial.println(busy ? "playing" : "quiet");
}
// The module reports the end of a file twice; only the report for the
// file that is playing moves on.
if (player.available() && player.readType() == DFPlayerPlayFinished && (int)player.read() == track) {
Serial.print("finished file ");
Serial.println(track);
track = track % files + 1;
delay(200);
player.play(track);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Opens both serial ports and sets BUSY_PIN as an input. player.begin(link) checks that the module and card respond. If not, it prints a hint and stops. Otherwise it sets the volume to 20, reads the file count into files, and starts file track.
void setup() {
Serial.begin(9600);
link.begin(9600);
pinMode(BUSY_PIN, INPUT);
if (!player.begin(link)) {
Serial.println("Unable to begin: check the wiring and the card");
while (true) {
}
}
player.volume(20);
files = player.readFileCounts();
Serial.print("Jukebox ready, ");
Serial.print(files);
Serial.println(" files");
player.play(track);
}loop sketch.ino
Reads the BUSY pin and prints playing or quiet when it changes. Then it checks for a message from the player. If it says a file finished and it is the file that was playing, the sketch moves track on, wraps it with track % files + 1, waits 200 ms and plays the next.
void loop() {
bool busy = digitalRead(BUSY_PIN) == LOW;
if (busy != wasBusy) {
wasBusy = busy;
Serial.println(busy ? "playing" : "quiet");
}
// The module reports the end of a file twice; only the report for the
// file that is playing moves on.
if (player.available() && player.readType() == DFPlayerPlayFinished && (int)player.read() == track) {
Serial.print("finished file ");
Serial.println(track);
track = track % files + 1;
delay(200);
player.play(track);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The monitor says Unable to begin This project
Check the TX wire goes to pin 10, the resistor leads from pin 11 to RX, and the card is in. - It plays one file and stops This project
The sketch only moves on when the module reports the finished file. Check that the wiring from TX to pin 10 is solid. - The module stays silent or does not respond to commands. DFPlayer Mini MP3 module
Format the microSD card as FAT32 and name files 0001.mp3, 0002.mp3 and so on. Copy them in the order you want. Check the card is seated in the slot. - Commands are garbled or ignored on the serial link. DFPlayer Mini MP3 module
The module uses 9600 baud and 3.3 V logic on RX. Put a 1 k resistor in series with the Arduino TX line to its RX. Cross the lines: Arduino TX to module RX. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.