Wanted to do a project with the Pi Pico and thought a Music Box would be a good start. Made one in 2012 following the instructions on elm-chan and using an ATTINY45. Time for an update!
Effort/LevelEasy/Beginner Soldering required. About 2 hours assembly time.
3D print takes about 6 hours for the box. The "Aperture Labs" logo is a separate print in white material. You'll need to paint the top of the letters. I used black hobby acrylic paint. There are options for plain boxes (no logo), too.
Or, you can get a wooden "craft box" and drill holes in it for speaker and USB connector. For the craft box, look for one at least 100mm L x 60mm W x 60mm H (4 in x 2.25 in x 2.25in). You'll need a hole drill bit about the size of your speaker.
CostAll costs USD.
About $30, not counting 3D print, or craft box. Also depends on what you have on hand.
The simplest and cheapest option shown below is no box, just the Pico, a speaker, a resistor and an Adafruit Amp, about $12 if you have some wire, a breadboard and a spare 5V power adapter on hand,
The "Full Portal" version, with Neopixels as a backlight, is around $30.
ResearchI reviewed what was available and found some source material from Markus Gritsch, from 2012. Markus supplied a midi file for the Portal game song "Still Alive". Did a little more searching around and found I could convert a midi file to a "music box" sound on the Music Box Maniacs site.
That meant that you could convert almostANY midi file to a music box song, given size restrictions for memory on the Pico. You have to stay below 1MB for code and files. If you wanted longer songs, you could add an SD card reader and play from there. Limited by the size of your SD card. I leave that for the interested student to figure out....
If you don't like the mp3 version in the downloads, there are lots of different versions of Still Alive on Music Box Maniacs, available for download. Or, other songs you might like in a music box. The github download has plain and Portal-themed STL files.
Next question...There's no I2S output on the Pico - could the Pico play audio? I found a very helpful article by Michael Horne, Playing Sounds from the Raspberry Pi Pico using CircuitPython.. This was a "Maker's Journey" tale of Michael discovering how to play a wav file on the Pico.
TL;DR: Adafruit to the rescue! Michael's solution was based on Adafruit's CircuitPython. CircuitPython already has libraries for the Pico that use PIO, "programmable input-output" to help play audio. The PIO functions use the new hardware on the Pico and a software "state machine" for absent hardware functions. For an introduction on PIO, see this blog post.
More digging and I found a CircuitPython example for playing mp3 files. I mashed up Michael's code with the mp3 example and Ta-Da!, mp3 played out the speaker!
For extra effect, I added a backlit fade-in/fade-out with a Neopixel stick. Adafruit also had the code for that, in Logan's Run Hand Jewel LED. I used a NeoPixel stick, so only needed to change the numpix to 8, and the pixpin to board.GP0. Note that I did get a little flicker, which I could not resolve. I think I had poor solder joints. [SIGH].
WiringSee the images below for a wiring guide. See Gain section below for resistor value and where to wire it in.
Once assembled, it's pretty easy to load the code:
- Download the UF2 and Library files for CircuitPython
- Install the UF2 per these instructions. Copy over the lib/neopixel.mpy to the Pico lib folder.
- Download files from the project github repo. There are files for a plain box and for one with the backlit "Aperture Laboraties" logo.
- Copy over the files code.py and still-alive.py to the Pico
- Power up and enjoy!
The 3D printed box is based on this (great!) youtube video from Product Design Online. The video guides you through how to create a print-in-place. parameterized box with hinges. The video notes have a link to download, in different formats, the base box design. I used that as a base for the music box, adding features for a speaker, USB connector, mounting standoffs, and a back-lit logo. Fusion 360 STL and design files are included in my downloads.
Print the box in the orientation shown below. I used Prusa Slicer to create the printer gcode. Prusa uses "Expert" mode to allow you to have specific support material. I used a box support on the USB connector. See this video for more information on how to use support enforcers.
Michael pointed out that the music was glitchy when played from a computer USB port. This was the presumed result of the computer continually trying to read the Pico as a USB device, generating noise. Solution: play from a power adapter or USB battery bank.
Michael's article used an example of a wav file and was based on an Adafruit article for using the MAX98375A I2S breakout board and amp.
There were still distortions when playing the sound file. There was a comment in an Adafruit FAQ stating that could happen with the gain set too high - I had left it at the default 9dB, and I was using a tiny speaker. A quick addition of a 100 Ohm resistor between the gain pin and voltage lowered the gain to 3dB. That sounds fine.
Songs in mp3 format downloaded directly from Music Box Maniacs had distorted sound even when played on a wall wart or a battery bank. I used Audacity to mix the stereo tracks to mono only and down-sample to a 22khz rate. Problem solved!.
Comments