MP3 Player Interface

I was terrible at history in school because most of it was memorization of dates and names.  I wanted to know about the people involved and how they felt about what was happening at the time. One advantage of getting older is that you can look back on the history that you have actually witnessed in your lifetime and look forward to what is yet to come.  For instance, my grandmother was born in the days of horse drawn carriages and lived to see Space Shuttles winging back and forth to the Space Station.  In my music life I can say that I’ve gone from the days of 78 vinyl records (my first purchase was “The Flying Purple People Eater”) to digital MP3’s.  And in my electronics life I’ve gone from vacuum tubes to computer chips.  For this project I combine those two more modern things by taking a common MP3 module and showing a few ways to use it with a PIC microcontroller.

DFPlayer Mini

DfPlayer Mini.png

There are a variety of MP3 modules out there but the most common one I found was the DFPlayer  Mini.  I was able to get one on ebay for just over $2 and found that there are lots of Arduino examples out on the web.  Unfortunately, the specifics of the module are a bit jumbled because the documentation is not well written.  Below I’ve included a couple of different documents, one specifically for the DFPlayer and one for a similar module.  Some of the details missing in the DFPlayer document are available in the second document.

I_O_control

The module is almost completely self contained because it also includes a 3 watt (at 4 ohms) amplifier chip and if you are just interested in just making an MP3 player then you don’t even need a microcontroller.  In fact, a power supply, a single speaker, and two normally open momentary contact switches are the minimum requirements.    That simple setup shown above has four switches but only the two connected to IO1 and IO2 are required.  For instance, a short press on the IO1 switch will command it to play the next track and a long press will command it to decrease the volume.  You will need to press either the “Next” or “Prev” switch to start playing a track. The speaker can be from 4-8 ohms and it’s important to note that the module is programmed to start at full power.

A_D_control.png

Another way to make a stand alone MP3 player is shown in the diagram where there are 20 switches and resistors.  This is known as the A/D control and each switch is connected to the player through a specific value of resistor.  That feeds an internal analog to digital circuit that determines which switch is pressed based on the voltage through the resistor.  You don’t need all of the switches and resistors to make it work, just the ones for the controls you want.  Typically that would be just switches 1 and 3-6 on input  ADKEY1.  Switch 2 would only be needed if you wired up a USB connector for use with a flash drive or a computer.

USB Connection

The DFPlayer Mini comes with a holder for using a micro SD (also known as a TF card) and that is sufficient for making a basic MP3 player.  But it also has connections for a USB interface.  The USB wiring is shown above and all that is needed is a female USB connector.  The player will automatically recognize the SD card and the USB flash drive when either or both are inserted.  If both are inserted I found that the player will select the SD card over the USB flash drive unless specifically commanded to use the USB port.

Also shown above is the connection to the DAC output pins.  These are used if you want a stereo output and they cannot drive a speaker directly because they do not run through the 3-watt amplifier module.  I connected them to a standard stereo headphone socket and then plugged in a set of amplified computer speakers.  That worked well.  If you use that connection, then it’s best to leave the volume control of the module at its default maximum power so that you have full volume range with the volume control on your external amplifier or computer speakers.

Manuals

SD/Flash File Setup

There are a variety of ways to store and access the MP3 files on the SD card or flash drive.  If you don’t need anything too complicated, then the recommended way is to create a folder labeled “MP3” on the drive and place all of your MP3’s in there.  There seems to be some disagreement as to how many you can place in that folder but 3000 seems to be an ok maximum.  If you want to be able to specify specific files to play then the files names need to start with 4 digits.  The first file would be “0001.mp3”, and so on.  It also seems to work if you have named files but just add the 4 digits in front of the name.  I used the format “0001 – My File Name.mp3” and that worked ok.

Another feature you might want to use on the DFPlayer is the advertisement cut-in mode.  That allows you to be playing an MP3 and then, at any point, specify a second MP3 to interrupt and play.  When the second MP3 finishes, the first one should resume from where it left off.  In order to use the command associated with this feature the advertisement files need to be placed in a second folder labeled “ADVERT”.  The files in this folder also need to be labeled with the 4 digits in front.  An example usage might be to have music playing at a shop and then have it play a welcome message when the system detects that a customer walks into the shop.  If you just specify a different MP3 to start playing without using the advertisement commands, then the MP3 that was interrupted does not start up again when the interrupting MP3 finishes.

DFPlayer Mini Wiring

MP3 Player

After reading a bunch of posts on the web about this player I got the impression that there can be noise issues if improperly connected.  If you look at the specs it says it can range up to 5 volts on the VCC pin but most 5 volt adapters I’ve used typically put out up to 5.2 volts.  Doesn’t sound like much difference but the spec for the MP3 player also states a typical input of 4.2 volts.  One of the potential noise issues seems to be related to running the module at 5 volts or more.  An easy way to cut this down closer to the desired 4.2 volts is to simply put a diode in line with the supply voltage (as shown in the diagram above).  That will reduce the voltage by about 0.6 volts and put it pretty close to the desired value.  One thing to keep in mind for the diode is that it needs to be one like the 1N4001 that can handle 1 amp, not a small signal diode like the 1N4148 or 1N914.  Any standard silicon diode (not Schottky or germanium) that can handle at least 1 amp can be used.

Another possible factor in noise issues comes when a microcontroller is used and communicates via the serial port (RX/TX).  The DFPlayer module logic actually runs at 3.3 volts so there are issues when using a microcontroller that is running at 5 volts.  The general recommendation on the web seems to be to simply add a 1k ohm resistor between the microcontroller TX pin and the DFPlayer RX pin.  That sort of works but the resulting voltage input is still out of spec for the DFPlayer.  Adding one more resistor from the DFPlayer RX pin to ground provides a voltage divider that gets the level within spec.  The diagram above shows a 2.2k resistor but it can be anything from 1.5k to 2.2 k.  The DFPlayer TX pin can interface directly with the microcontroller RX pin with no problems.  The DFPlayer TX output is really only needed if you want to monitor the status information from the player.

Serial Communications

All of the examples below use the PIC to send commands to the DFPlayer module.  Those commands, and any DFPlayer responses, are communicated at 9600 baud over a standard TTL-level USART interface.  The manuals provided in an earlier step detail the various commands that can be used to either supplement or replace the basic switch command inputs.  One interesting supplemental command is the capability to specify which file to play and we will use that in a later step.

The basic command/response message formats are pretty easy but one confusing thing is the message checksum.  It’s actually easy to calculate but the explanations and examples in the manuals can quickly lead you down the wrong path.  In essence, the checksum bytes are the inverse of the sum of bytes 2-7 of the message.  In other words, add up those 6 bytes (they will overflow into a two-byte result) and take the XOR of the result.  Unfortunately, if you try to get the results for some of the examples in the manuals you won’t get a match because they are wrong.  The really good news is that the DFPlayer will happily accept messages where the checksum bytes are set to “00 00”.  Examples of these non-checksum messages are shown in the “Similar Board Manual 2” listed in a previous step.  Most of the examples I provide below don’t read the MP3 module responses and in the one example where the software does read the responses the checksums are ignored.

Test Setup

Test Setup

No Card Hex.jpg

There is a quick test setup you can make to verify that the DFPlayer can communicate with the PIC.  As shown in the diagram, the PIC is wired to use a 4-bit LCD interface for a common 1602 LCD.  That interface is included in all of the examples in this post but it could be eliminated if you don’t want it.  The test setup software does require the LCD but you can skip this step if you want.  In order to do the quick test of the DFPlayer communication, just connect it to power as shown in the previous step and connect the TX pin to the PIC RX pin.  If you have not installed an SD card then you should see the result shown in the picture above.  If you did install an SD card then the last “0” digit in the picture should be a “2”.  That means that the SD card is online.

MP3_Test SW

Sensor Detector

Sensor Detector

Sensor Display

This example provides a template for using the MP3 player to provide audio messages whenever the PIC detects sensor inputs.  The example provided includes the LCD interface so there are only four PIC pins available for sensor inputs.  If you remove the LCD interface then there are a lot more pins that can be used.  The sensors are illustrated in the diagram above as simple switches that connect to ground when activated.  The software is set up so that it will not reactivate the message for a given sensor if the sensor never resets itself.  To test this out I used a free voice recorder to make sample MP3 files and then numbered them as specified in a previous step so that the PIC could send a file specific command to the MP3 player.  The software also includes an example of how to receive and decode messages from the MP3 module.  I found the receive messages to be useful when I was debugging the software but not of much use for the applications in this project.  A sample display is pictured above.

Sensor Detector SW

IR Control

IR Control

Play

Pause

Next Track

The IR control example comes from what was originally done in earlier IR interface projects.  You can easily add or change the codes in the software example for your needs and your remote output.  I just used one of the cheap remotes I had laying around and verified the key codes using the IR reader from an earlier project.  The LCD interface software doesn’t have to be removed if you decide to not connect a display.  The connection from the MP3 module TX to the PIC RX is not used in this example.  Some sample displays are pictured above.

IR_MP3 SW

Bluetooth Control

Bluetooth Control

The Bluetooth control example comes from what was originally done in an earlier post on Bluetooth control.  As shown in the diagram, the Bluetooth module receives the codes and transmits them to the PIC.  Both the Bluetooth module and the MP3 module communicate at 9600 baud so no changes were needed in that area other than to eliminate the software that handled responses from the MP3 module.  The Bluetooth TX output replaces the MP3 TX output as the RX input for the PIC.  For test purposes I used the Android app I had set up on my phone for the Bluetooth Controlled Car project.  The LCD interface software doesn’t have to be removed if you decide to not connect a display.  The sample displays shown in the IR Control step apply here as well.  That’s it for this post.  Check out my other electronics projects.

Bluetooth_MP3 SW