Raspberry Pi Pico CircuitPython MIDI Interface
February 13, 2023
In this article, I’ll first outline the basics of using MIDI with CircuitPython on the Pico for direct interface with a computer. I’ll then show how a Pi 4 (or 3/2) can be used as a host to pipe signals to a keyboard or other device.
RPi Pico and Pi 4 (with PCB enclosure) can be used as a MIDI setup (Image Credit: Jeremy Cook)
MIDI, or Musical Instrument Digital Interface, is a standard language that allows digital music devices to work together between brands and types. It’s a beautiful thing, which has saved countless hours of setup time–and certainly a massive amount of money in equipment expenses. The standard can even be used with non-musical devices, and has been updated from the original 5-pin connectors (which are still very much in-use) to allow MIDI over USB.
As it just so happens, the Raspberry Pi Pico development board has a USB port onboard, and is very much capable of working with MIDI. In this article, I’ll first outline the basics of using MIDI with CircuitPython on the Pico for direct interface with a computer. I’ll then show how a Pi 4 (or 3/2) can be used as a host to pipe signals to a keyboard or other device.
CircuitPython MIDI With Raspberry Pi Pico
If you’re entirely unfamiliar with MIDI, this video presents a great crash course on the concept, and there’s more detail in this longer clip if you would like to delve into it further.
For a Pico/RPi hardware setup, my example code (with the adafruit_midi library available via the bundle found here) plays a G sharp on the second octave when a button is pressed. Code is based on the official Adafruit usage example found here, adding a command to turn off the sustain synth function so the note doesn’t continue to ring when the button is released: midi.send(ControlChange(64, 0)).
Per the MIDI control specs, control number 64 dictates whether sustain is on or off. Values of ≤ 63 turn it off, while ≥ 64 turns it on. I’ll admit, this bit had me quite confused initially. That being said, I was pleasantly surprised to find that my Pico controller interfaces with GarageBand on my Macintosh by just plugging it in (with the proper CircuitPython code installed).
I’m entirely new to MIDI, and while this is easy to implement on one level, it appears to be a deep rabbit hole if you choose to follow it. As shown below, I was also able to use the Pico as an ultrasonic instrument using code that I posted here:
Making music with the @Raspberry_Pi Pico w/ Garageband over MIDI + a couple of ultrasonic sensors & a PCB coaster by @PCBWayOfficial 🎵🔊 pic.twitter.com/pLRkPEDzEd— Jeremy Cook 🤖 (@JeremySCook) January 30, 2023
Raspberry Pi MIDI Host
If you'd like to send MIDI signals to an instrument like a keyboard, you'll also need a host device to properly route these commands. You can buy a MIDI host, or if you have a Raspberry Pi available you can flash it to act as one. Instructions are available here, and I used the pre-made image on a Raspberry Pi 4B.
Pico connected to Pi 4B host via USB, Pi 4B connected to keyboard (Image Credit: Jeremy Cook)
Once the Raspberry Pi 4B was set up as a host and powered on, I plugged it into our family’s rather old, but quite serviceable CTK-2300 keyboard via USB. The Pico was then plugged into the Pi 4, which allowed me to play notes on the keyboard via my custom breadboard/button interface. Again, I was pleasantly surprised.
Attempt at MIDI keyboard interface, using @Raspberry_Pi Pico W and RPi 4 w/ @CircuitPython 🔊— Jeremy Cook 🤖 (@JeremySCook) December 26, 2022
Kind of surprised it just worked, after getting code smoothed out on the computer. pic.twitter.com/jcg17h5S1Q
Given my experience so far, it almost seems that the kinks in this technology have been worked out in its ~40 years of development. On the other hand, USB has “only” been around since the mid-1990s, the 5-pin MIDI standard is still very much in use, and there are many, many compatible devices that I’ve yet to explore. I’m just scratching the surface here, so certainly hardware and software bugs do still pop up.
MIDI: THE Standard
There’s no real competitor to MIDI in the musical production arena. But if you really want to see just how awesome it is, check out the video below. There Philip Karlsson Gisslow shows how he’s able to use a Raspberry Pi Pico, along with a Digilent Analog Discovery 2 and other assorted hardware to transpose an electric guitar input to a MIDI output.
Gisslow can therefore use his guitar as a general purpose input for whatever sound is needed. Sort of a synth for those of us that don’t play keyboard–or the inverse of a keytar.
Jeremy Cook is a freelance tech journalist and engineering consultant with over 10 years of factory automation experience. An avid maker and experimenter, you can follow him on Twitter, or see his electromechanical exploits on the Jeremy Cook YouTube Channel!