Third party cookies may be stored when visiting this site. Please see the cookie information.

PenguinTutor YouTube Channel

Raspberry Pi Pico MP3 Player using DFPlayer Mini

I've created a MP3 player using a Raspberry Pi Pico and the DFRobot DFPlayer Mini. The player is a tiny PCB board, just a little bigger than the Micro SD Card adapter. It can be controlled by adding external button switches or through using a serial connection via UART. In this case I have connected it to a Raspberry Pi Pico which will provide a way to control the MP3 player using automation or via a WiFi connection from a smart phone.

DFRobot DFPlayerMini MP3 player with Micro SD Card

There is some code available from DFRobot which allows it to be used it with aan Arduino, but not for use with a Raspberry Pi Pico, and the code is in C. Whilst I could have likely updated the C code to run on the Pico I instead decided to created a Python library.

There is limited documentation on the protocol and hardly any comments in the code. Through a process of reading the current documentation, running the Arduino version with extra debugging and reverse engineering the source code I've been able to work out most of how it works. The library I have created is not a full implementation of the protocol, but provides the important commands needed to provide a simple MP3 player that can be controlled by the Raspberry Pi Pico. I've added a lot more comments to the library that I've created.

Raspberry Pi Pico with DFRobot DFPlayer Mini MP3 Player

Wiring up the Raspberry Pi Pico and DFPlayerMini

Here is a representation of the breadboard design. This is wired up for both the simplified button control and a UART connection to a Raspberry Pi Pico.

For the buttons there are two way of wiring up buttons. There are two analog pins which can be used with switches and different resistor values to configure 10 buttons each, given a total of 20 buttons. Or this simplified way allows for 4 buttons which are used for Next/Vol- on one button, Previous/volume + on the next button then segment 1 and 5 on the other two buttons. The buttons were used for testing on my board, but won’t be used now I’ve got the Pico connection working.

Raspberry Pi Pico with DFRobot DFPlayer Mini MP3 Player Breadboard Layout

The real aim is to fully automate this with the help of the Raspberry Pi Pico, which used a two wire UART connection. The yellow wire goes from port GP 4 on the Pico (which is configured as UART transmit on the pico - and UART receive on the MP3 player. The green goes from GP 5 on the Pico configured as UART receive on the Pico going to UART transmit on the MP3 player.

The final part is the speaker. This is intended for use in a model railway locomotive and as such is a basic speaker which does not have a particularly good bass. It is however a good fit for integrating into a model. You can find these from model railway / model railroad shops or by searching for ESU sugar cube speaker kit.

Source code

You can download the source code from github.

Using the Raspberry Pi Pico MP3 Player

Upload the file dfplayermini.py to the Raspberry Pi Pico (you can use the upload functionality within the Thonny editor). Then import that to your own python file.

from dfplayermini import DFPlayerMini

Create an instance of the DFPlayerMini class, providing the UART number (typically 0 or 1), followed by the trasmit pin number (eg. GP 4, for physical pin 5), and then the receive pin number (eg. GP 5 for physical pin 6).

player1 = DFPlayerMini(1, 4, 5)

Issue a reset against the player (which you may need to attempt multiple times - typically 2 to 3 times).

player1.reset()

The reset will return True when it has successfully reset the MP3 player. You can then call any of the methods. For example set the volume to 12 (out of 30) using

player1.set_volume(12)

Select the SD card using

player1.select_source('sdcard')

Start playing a track using

player1.play(track_number)

Library methods in MicroPython MP3 player library

reset()

Reset the board ready to start communicating. Run this until it returns True.

set_volume(volume_value)

Set the volume. volume_value can be between 0 (off) and 30 (max)

volume_up() / volume_down()

Increase / decrease the volume

select_source(source_name)

Choose the source fo the audio. Actual sources depend upon the type of player. Eg. 'usb', 'sdcard', 'aux', 'flash'

stop()

Stops playing the track

play(track_number)

Play a specific track number

play_next() / player_previous()

Play the next, or previous track

play_loop(track_number)

Player the specified track number in a loop

pause()

Pauses the play (resume using start)

start()

Plays the current track (eg. resume after a pause)

query_num_files()

Get the number of files on the SDCard / audio source.

Related projects

I eventually intend to use this as part of a model railway layout providing sounds for the station building. See the link below for more details of model railway projects.

Previous Pico LED Reaction Game
Pico LED Reaction Game
Next Pico 2W web MP3 player
Pico 2W web MP3 player