Raspberry Pi Music Box – my own project


For a long time, I’ve wanted to do a project which involved two of my passions: the Raspberry Pi and music. I’ve seen several projects which have successfully managed it, in particular the Joytone which used lots of joysticks to create music. More recently, Calvin Cherry created a musical instrument with sensors attached to his body which produced sound based on movement and position.

My effort is pictured above. It’s just called The Music Box – I called it that for so long that, despite asking for suggestions on Twitter, the name stuck.

If you want to watch a video rather than read through my ramblings, head to the bottom of the page.


The Music Box is a wooden box (procured from the magical place known as eBay) fitted out with buttons on top and at the back, three 10K potentiometers (with coloured knobs which make all the difference, of course) and an obligatory (purple!) LED at the front. Inside, it is stuffed with all the wiring necessary to connect the buttons, potentiometers and LED to a Raspberry Pi 2 via an Adafruit Proto Pi HAT board. I have used an MCP3008 analog-to-digital converter chip to take the potentiometer readings.

I had initially used a ProtoPal from the Average Man but I messed it up. I just wanted to give a shout-out about the board as it would’ve done the job slightly better than the Adafruit. I will re-use it at some point but I need to get my solder-sucker out and clean it up a bit!

Also in the box is some power circuitry, an amplifier, two speakers and a LIPO battery (I also got a little microUSB charger for the LIPO). The buttons on top (both yellow and white) and most of the other components (including the lovely LED bezel on the front) came from Makersify, which is a fantastic UK-based maker store that carries most of the Adafruit stock and can order in anything that isn’t in stock.

Oh, something I learned while putting The Music Box together: 1K potentiometers will not do the job. The MCP3008 acts as though nothing is connected. A 10K potentiometer, however, will work very nicely. Big thanks to Alex Eames and others on Twitter who helped me to diagnose this. Once again the valuable Raspberry Pi community comes through! If anyone knows why this is, by all means leave a comment!

Here’s a photograph of the inside of the Music Box. My apologies that it isn’t more detailed – there’s only so far I can open the box without the innards falling out! You can see more in the video at the bottom.



The software is a mixture of GPIO Zero, standard Python and the pyFluidSynth library which communicates with FluidSynth, a synthesiser that plays sound fonts. I loaded thirty-two different sound fonts and it will be easy enough to add more as I can just drop them into the folder and the software will automatically load them.

The program imports the necessary libraries, loads the sound font files and then uses GPIO Zero to detect when_pressed and when_released events. When these events are detected, the sound samples are started or stopped accordingly. GPIO Zero is inherently multi-threaded which means I can press more than one button at once to form chords. The code also plays a sample and lights up the LED when the program is ready for use.

The three potentiometers control various aspects of playback. The red pot controls the volume at which the samples are played. The green pot controls which of the thirty-two sound fonts are used, i.e. what instrument is played back. The blue pot controls the set of notes playable on the seven buttons. By turning the pot to the left, lower notes are played whilst turning it to the right raises the pitch.

Thanks to Ben Nuttall for help at the start to understand how the event handlers worked. Also, thanks to Pimoroni who wrote the code for the Pi Piano and got me going. I didn’t end up doing it that way in the end, but the Pi Piano code helped me to work out how to load unlisted files from a directory.

All the code is available on GitHub for you to inspect! Any questions, please leave a comment!


Here is a five-minute walkaround video I made of The Music Box. In it, I describe the hardware and software and also turn it on and play a few sounds.

I hope this won’t be the last music/tech project that I do as I’ve found it really fulfilling. It’s been great to use so many different parts and bring them together to create something which is both portable and, above all, usable.

Any questions about the hardware or software, please feel free to leave a comment or contact me.

3 comments for “Raspberry Pi Music Box – my own project

  1. John
    31 May 2016 at 9:56 am

    Well done! Inspiring.

  2. 31 May 2016 at 11:19 am

    Very cool project. I did not know about FluidSynth, so I really appreciate this post. I will definitely be playing with this. So I would have to agree that it’s inspiring! 🙂

    Also, I clicked through to your GitHub account and I find it rather amusing that we have projects that seem fairly closely related to each other. I have a Raspberry Pi camera project (a security camera) and a project that uses servos via a web interface. (My GitHub username is lelandg.) And I do love music and synthesizers, and have owned one since the early 80s! I have played around with Sonic Pi and thoroughly enjoyed it. So I look forward to exploring FluidSynth as I am an experienced programmer and actually specialized in Python for part of my career.

    So I guess this is a “hello” as well. 🙂

    • Michael Horne
      31 May 2016 at 11:28 am

      Hello! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *