DFPlayer Mini
Introduction
This JAL Library supports all features of the DFPlayer Mini, an audio playback device.
The library is set up in a way that the main program determines which serial interface is being used for controlling the DFPlayer Mini. Three sample files are available, a simple version called 16f1823_dfplayer.jal that plays the first track, waits for 10 seconds and then plays the next track. The other sample program called 16f19176_dfplayer.jal uses a menu structure with which all features of the library can be controlled. The third sample called 12f617_dfplayer.jal shows the same functionality as the 16f1823 version but this version is meant to show that it can work on a smaller PIC that has no on-board USART so it uses the JAL software serial interface library to control the DFPlayer Mini.
The Hardware
The DFPlayer Mini is controlled via the serial interface using a fixed baudrate of 9600 baud. It can operate on 5 Volt but it is recommended to use 1k resistors in the serial communication lines between the PIC and the DFPlayer Mini. The DFPlayer Mini plays audio files in mp3 or wav format, stored on e.g. a micro sd card that can be inserted into the device. A loudspeaker can be connected directly to the DFPlayer Mini of which the volume can be controlled via the API provided by the library.
The schematic diagram below shows how the DFPlayer mini can be connected to the pic12f617.
The connections of the DFPlayer mini are as follows:
The loudspeaker is connected directly to the pins SPK_1 and SPK2.
Sample program for the DFPlayer Mini using a 12f617
The sample file always start with a header to explain what it is about.
-- ----------------------------------------------------------------------------- -- Title: Sample program for the DFPlayer Mini. -- Author: Rob Jansen, Copyright (c) 2020..2020 all rights reserved. -- Adapted-by: -- Compiler: 2.5r4 -- -- This file is part of jallib (https://github.com/jallib/jallib) -- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php) -- -- Description: Demonstrates the playback feature of the DFPlayer Mini library. -- For a completed demo of all features see 16f19176_dfplayer.jal. -- -- Sources: -- -- Notes: The DFPlayer uses a serial interface. This program uses the software -- serial library since this PIC has no on-board USART. --
Include the device file for the pic12f617.
include 12f617 -- Target PICmicro
Set the pragma's. We use the 4 MHz internal oscillator.
-- Use internal clock and internal reset. pragma target OSC INTOSC_NOCLKOUT -- Internal Clock pragma target PWRTE ENABLED -- Power up timer pragma target MCLR INTERNAL -- Reset internal pragma target WDT DISABLED -- No watchdog pragma target BROWNOUT DISABLED -- Disable to save power pragma target IOSCFS F4MHZ -- Set internal oscillator to 4 MHz pragma target clock 4_000_000 -- Oscillator frequency 4 MHz
At reset all pins of the PIC are set to analog input. Switch them to digital. Note that we also wait some time here. It can happen that a PIC needs some more time to stabilize after power up so we add a delay here before we continue with the rest op the program..
enable_digital_io() -- Give the hardware some time to stabilize. _usec_delay(100_000)
This PIC does not have a serial interface on board, the so called USART. Instead we use a software library that emulates a serial port. We have to define which pins we are going to use for the serial interface. After that we can include the serial software library and initialize it.
-- Setup the serial software interface for communication with the DFPlayer. alias serial_sw_tx_pin is pin_GP4 -- Pin 3 of 8 pin DIP. pin_GP4_direction = output alias serial_sw_rx_pin is pin_GP5 -- Pin 2 of 8 pin DIP. pin_GP5_direction = input const serial_sw_baudrate = 9600 include serial_software serial_sw_init()
The next step is to include the library of the DFPlayer mini and start playing an audio track. In this example we first set the volume, start playing track 1 and play a next track after every 10 seconds. For the 10 seconds delay we use a delay_1s() function from the delay library so we need to include that library too .
include delay -- Now we can include the DFPlayer. include dfplayer dfplayer_init() -- Initial volume is quite loud, so lower it. dfplayer_set_volume(15) -- Play the first track that the DFPlayer found on the storage device. dfplayer_play(1) forever loop -- Play each track for 10 seconds the go to the next available track. dfplayer_play_next() delay_1s(10) end loop
The following video shows this sample program in action. The micro as card has one folder called '01' which has five mp3 tracks labeled '001.mp3' to '005.mp3'.
Some more info about the DFPplayer Mini that you should know
- Folders used must be named 01 to 99, ADVERT or MP3
- Audio files are numbered 001 to 255 or 0001 to 3000 with extension .mp3 or .wav
- Folders 01 to 15 can contain audio files named 001 to 255 and 0001 to 3000, these are the so called special 3000 track folders
- Folders 16 to 99 can contain audio files with names 001 to 255
- Folder ADVERT can contain advertisement audio files named 0001 to 3000
- Folder MP3 can contain mp3 only audio files named 0001 to 3000
So audio files (tracks) are always numbers and they have to be exactly 3 or 4 digits with the extension .mp3 or .wav. Note that some API functions only work on files with 3 digits and some other on audio files with 4 digits. It is possible to combine these different files in folders 01 to 15, so you could have audio files in these folders like 001.wav, 1234.mp3.
To play track 011.wav from folder 02 you the procedure call is dfplayer_play_folder(2,11) but if you want to play track 1234.wav from the same folder with number 02 the procedure call is dfplayer_play_3000_folder(2, 1234). When calling procedure dfplayer_play_advertisement(2000), the DFPlayer will interrupt the playback of the current track, plays the advertisement track 2000 (.wav or .mp3) from the ADVERT folder and will resume play after the advertisement track has completed.
The module has some other special features like a repeat track, repeat folder and an equalizer. It also has a sleep function that can be called via the API of the library but the only way to wakeup the DFPlayer after sleep is by switching the power off and on again.
The tests of the library where done with the folders and files stored on a micro sd card but the module and the library also supports other storage devices.
This library was immediately used by one of the JAL users. The result of this project can be seen in the video of Bobby .