Arduino MP3 Piano

This is the first of a series of projects I hope to do based on an MP3 Audio Shield based on the VS1053 chip.  There are so many possibilities with this device, but to start with I gone for a simple “piano” device (five notes only) that will play an MP3 file for each note.  Read on for details!

  • This is the first in a series of posts on the VS1053.  In Arduino MIDI VS1053 Synth we look in more detail at the MIDI capabilities of the module.

These are the key Arduino tutorials for the main concepts used in this project:

If you are new to Arduino, see the Getting Started pages.

Parts list

  • Arduino Uno
  • VS1053 based music shield
  • Micro SD card
  • 5x 10k resistors
  • 5x switches
  • Breadboard and jumper wires

The Circuit

ArduinoMP3Piano_bb

This is basically the Simple Arduino Music Keyboard again, but this time with just five keys (and I’ve built in on a proto-shield for ease of use).  This will sit on top of the MP3 Music Shield, which for me looked like this.

I found a shield online by looking for VS1053 on ebay, but there are many variants out there.  Both Adafruit and Sparkfun do what look like pretty neat modules based on this chip.  The downside of my shield is that it uses quite a large number of digital IO pins, so for the switches for my “keyboard” I used five of the analog pins in digital mode.

The other piece of hardware you’ll need is a micro-SD card.  It doesn’t really matter how big it is, just big enough to hold the MP3 files for your notes to play.  This project makes use of the fact that you can ask the shield to play a track by number, and it will look for a tracks with the names track001.mp3 up to track009.mp3 on the SD card for the numbers 1 to 9.  That makes writing the program a lot simpler.

I wanted a set of notes to play, but there don’t seem to be many around on the Internet.  It doesn’t have to be notes, it could be anything – sound effects, recorded speech, even full MP3 tracks I guess – but I’m making a “piano” so I wanted notes. In the end I found a set of notes thanks to user “SimpleInfinity” on reddit here.  I just used C3 up to C4 with no accidentals as my track001 to track008.

Is this 3V3 or 5V logic?

The VS1053 datasheet states that the maximum power voltage should be 3.6V and that the IO pins have a maximum of the power voltage + 0.3V.

All of the cheap boards I’ve seen include power regulators to allow the boards to be powered from a 5V supply.  But as far as I can tell they do nothing to level shift the GPIO.  This is particularly curious for the board built as an Arduino shield as the Uno is 5V logic throughout!

The Adafruit and Sparkfun boards include a level shifter for proper 5V SPI and IO to 3V3 conversion.  But as far as I can see none of the cheap boards do – even the one that looks designed for use with the Uno. But there are a lot of resistors on the board, so it is possible some voltage dividing is going on… (although I’ve not been able to spot it).

But practically no information that I’ve found on the Internet that describes the use of these modules with microcontrollers seems to mention this issue.  I figure the boards are fairly cheap, so I’ve just gone for it and they seem to work.

But I am not an electronics person – so do your own research and make up your own mind!

The Code

There is an amazing VS1053 library for the arduino.  If you search in the “sketch” – “include libraries” – “manage libraries” section of the IDE for vs1053 it will come up.  Look for “VS1053 for use with SdFat” by Michael P. Flaga and Bill Porter.  It will also need the “SdFat” library which you can install in the same way.

There are so many options, but for now I’m keeping it very simple by just running the initialisation code and then using the playTrack() function, which takes a track number.

The nice thing about this library is that it comes preset to be used with an Arduino Uno and a Sparkfun MP3 Player Shield, which the shield I picked up from ebay seems to be based on, meaning all the pin numbers and so on are the same.  Sparkfun have a great breakdown of the pin arrangements here.

You can read all about the different functions of the library here:

So this project is basically using the Simple Arduino Music Keyboard but instead of playing tones, plays MP3s.  The only quirk is that the library will not automatically wait until the MP3 has been played, so I do that in code myself using the isPlaying() function. This means that it isn’t looking for button presses while MP3s are being played – so it is best to keep them short if you want a responsive instrument!  Apart from that, everything else should be explained in the comments in the code.

When I first ran the program, I had a problem with a clashing IO pin (don’t ask), but that seemed to have the side effect of doing something odd to the SD card.  Windows would still see the card fine and show me the eight track files as expected, but the library kept coming back with an SD card error (0x30 0x4 if you want the gory details).

In the end I ran one of the SdFat library examples – sdinfo – which printed out a whole pile of diagnostic information about the SD card which was really helpful.  Turns out my card had no (or a fault) “MBR”.  I don’t need to go in details about what that means here, but from that I knew that the best way to sort it was to re-format the card in Windows (if you have to do this, make sure, the double check, and maybe check once more, that you have the right “disk” selected before continuing – you will be in for a world of pain if you get this wrong and could completely trash something important like your operating system or all your files). Once I’d done that and copied the track files back over, all was well.

The upshot of all this is that I get the code to play through each MP3 as a test at the start and I’ve included some serial port debugging to give some clues if something isn’t going right.  Having said that I’m afraid I’ve actually been really lazy when it comes to calling the library – I really ought to be checking for errors on startup and doing something about them… but then, what actually would I do?  The libraries are pretty good at using the serial port to give out error messages themselves already.

Find it on GitHub here.

Closing Thoughts

I have so many other things I could do with this shield now I’ve found out a bit about it.  The VS1053 is very versatile – the data sheet is 80 pages long! I’m also now starting to browse around the vs1053 library documentation.

If you don’t want to much about with all this yourself and want something out of the box that just works with all this, take a look at Bare Conductive’s TouchBoard.  That is basically all of the above, with more “keys” set up to be touch sensitive, provided with conductive ink to allow you to “paint” an instrument.  I have one myself and it is great (so it might feature in the future).  But if you want to get into the nuts and bolts and have a much more limited budget, this vs1053 shield has been pretty successful for this first go.

Kevin

Leave a comment