Monday, August 8, 2011

Tutorial: Gravitech Arduino Nano MP3 Player

Hello readers

In this article we will introduce playing MP3 files using the Gravitech Arduino Nano and MP3 add-on board as shown earlier. This board utilises the VLSI VS1053B decoder IC, which is quite well specified (download the data sheet [.pdf]). However in the interests of keeping things simple we will look at the most popular use – playback of MP3 files. The MP3 files can be in stereo, see the data sheet for more information with regards to bit rate etc. Here again is our board, both top:

… and bottom:

Using this board we can initiate playback to a pair of earphones, or an external amplifier. There is also a tiny speaker connected to the left audio channel, however it is not made for loud broadcasting! Some of you may be thinking “oh wow!, let’s make our own MP3 player” – which is a pretty normal thought after seeing this shield for the first time. However due to the size of the system, portability may be an issue. However, after some thought there are many uses for the shield, such as:

  • Event-driven recording playback. For example, you could standardise PA announcements by playing back pre-recorded messages at the press of a button instead of attempting to train staff to articulate themselves in an understandable manner (e.g. railway annoucements);
  • Modification of devices for the vision-impaired – a recording could be played after a particular event has occurred with the device;
  • Adding voice or music output to an art installation;
  • Etcetera

You get the idea. From a hardware perspective, you will need the Arduino Nano, the MP3 add-on board, and a microSD card. It is recommended that you not use a microSDHC card as they can be somewhat unreliable in this situation. Furthermore, please format the card with the FAT16 or FAT32 file system. Finally, to prepare for our first example – please copy the MP3 files downloadable from here onto your microSD card.

Now some preliminary software work needs to be done. Please download and install the SdFat library. The VLSI 1053B decoder chip uses the SPI bus (SPI? See my tutorials, parts one and two) – and so does the microSD card. Generally the CS (chip select) line is connected to digital 10 on Arduino – but our MP3 board uses 4. So we need to slightly modify the SdFat library to take this into account. Let’s do this now. First of all, locate and open in a text editor the file Sd2PinMap.h. It will be located in the library folder as shown below:

Now, scroll down to line number 279 in the file, and change the parameter uint8_t const SS_PIN from 10 to 4 – as such:

Finally, save and close the file. If you are working with other products that use the SPI bus and an SD card reader, you may want to run two separate Arduino IDE/library installations. At this point ensure your microSD card is formatted, contains the initial demonstration MP3 files, and inserted into the card socket underneath the board. Finally – insert your Nano into the MP3 board, plug in some headphones (or move the volume knob to mid-postion) and the USB lead. Then download and install this demonstration sketch. You will hear some bells (track001.mp3) and drums (track002.mp3).

A brief look at that sketch reveals much code that to the beginner or intermediate user could be somewhat confusing. But don’t let this worry you, we can rearrange a few things to make using the MP3 shield very easy. For our next example, we will learn how to alter the headphone output volume, and select which track to play. Download and open this sketch, and also download and copy these example sound files to your microSD card.

You can ignore everything except for the void loop() section of the sketch – there are two functions of note. The first being:

Mp3SetVolume(0,0);

This sets the output volume for the left and right audio channel. 0 is maximum volume, and the minimum is over 250. You should call this function every time if you need a default volume lower than 100% after every system reset/power cycle. The other function to note is:

playMP3("news.mp3");

This function is the goal of the exercise – just insert the file name of the MP3 you want to play. Note that file names must be in the old 8.3 character format (e.g. “evie123.mp3″ and not “Evie – Parts 1, 2 and 3.mp3″). Now you can have your sketch fire up the MP3 player on request – you can insert your code into this sketch and modify as required.

At this point you may be considering the output options of the MP3 board apart from a pair of earphones. Things are not entirely as they seem, and some thought does need to go into the hardware connections. Do not connect anything else apart from normal stereo “walkman”-style earphones to the socket on the board. Doing so will risk damaging the decoder IC. Furthermore, if you wish to connect the output from the board to an external amplifier, some extra circuitry is required. Some of this is included on the board, and some is not. First, please download and view the VS1053 output guide (.pdf). Turn to page eight to find the following schematic:

In order to connect to a line-out input of an amplifier via the earphone socket, you will need to recreate the circuit above. Some of this has been done on the board – resistors R7~R9 and C3~C5 are preinstalled. You can see this in the MP3 shield schematic. So do the right thing and reap the rewards!

Now you can go forth and musify your creations with MP3 music and other recordings. Please note that this article would not have been possible without the example Arduino  sketches provided by Nathan Seidle, the founder and CEO of Sparkfun. If you meet him, buy him a beer.

If you have any further hardware questions or enquiries relating to the Arduino Nano or MP3 board, please direct them to Gravitech via their contact page. The Gravitech Arduino Nano and MP3 board used in this article were promotional considerations provided by Gravitech, whose products including the Arduino Nano family are available directly from their website or these distributors.

As always, thank you for reading and I look forward to your comments and so on. Furthermore, don’t be shy in pointing out errors or places that could use improvement. Please subscribe using one of the methods at the top-right of this web page to receive updates on new posts, follow on twitterfacebook, or join our Google Group.

Otherwise, have fun, be good to each other – and make something! 

No comments:

Post a Comment