Tuesday 12 January 2021

Arduino MP3 Player : Talking Sums

Back in November I blogged about a tiny board, the size of a microSD card holder, which plays sounds and music.  Although I liked the idea, I ran into big problems using the weird AD4 format required for sound files on the SDcard.  I saw that the DFPlayer mini is newer and supposed to be much better (and cheaper) so I ordered some.  I am pleased to say it is wonderful; sound quality is impressive, Arduino support / compatibility is  fine; it permits lots of storage, has plenty of features and it is very, very cheap. 

For my purposes, spoken word sound files are more interesting than music for which I already have various solutions.  As a proof of concept I will write a simple Arduino calculator sketch which speaks the numbers, operators and result as you type.  This is probably very similar to a number of kiddies electronic toys.



The first steps are to find out how it works and  produce some sound.  The definitive reference is at the DF Robot product page  which includes specs and links to a manual and wiki. The wiki shows a simple setup requiring only 5 connections (VCC, GND, SPK1, SPK2 and IO 1) to play sounds.



 I put a few music tracks by the incomparable Ariane onto a 2GB FAT microSD card as /mp3/0000.mp3, /mp3/0001.mp3,......  I then connected a small speaker and power on a bread board.  When I touched pin 9 (IO 1) to GND music began to play.  Very impressive and pretty good quality!

There was some distortion in the music so I amended it in Audacity normalising it by -6db so that the peak values were reduced  and lowering the sample rate from 320kbps to 192kbps.  Sound quality is now very good, even on the dodgy speaker I am using for testing.  It is also possible to use the DAC outputs (DAC_L, DAC_R), but I didn't try that.


Next we want to connect an Arduino to DFPlayer which uses a serial command interface for full control.  It requires just two pins for serial transmission.  As we still want to use the Arduino RX/TX pins for the Serial Monitor we connect a couple of PWM GPIO pins and use the Arduino SoftwareSerial functions to program them.  




We also download the DFRobotDFPlayerMini library which gives us a set of functions to control the player.  Some articles (e.g. instructibles) use the command interface directly, but it is better and easier to use a library.  The samples provided aren't very exciting but they do show you how the sketch should be set up and the different functions which are available.  I was quickly able to provide a sketch to choose a track and start it playing.

Our objective is a talking calculator.
As the DFPlayer can store thousands of samples, a whole dictionary in fact, it is perfect for spoken instructions or feedback.  I couldn't find a good set of mp3 words on the internet so I set up some of my own.  It was easy to record the digits 0 to 9, and operators plus, minus, equals using a headset and Audacity software.  I recorded them as a single file with short pauses inbetween.  I could then select a snippet and save it as 0000.mp3, 0001.mp3 etc.

A simple function within the sketch speaks whatever you type and then the result.  For my POC I only dealt with single digits, non-negative numbers, addition and subtraction.  The result was very gratifying.

My verdict is that this is a great little tool.  Addition of a chip and a small speaker gives an extra dimension to your Arduino projects which can tell you what to do and describe results to you.










No comments:

Post a Comment