MIDI, MicroPython and the Raspberry Pi Pico

It was an exciting day on Thursday this week on Twitter.  I was able to watch the release of the Raspberry Pi Foundation’s new microcontroller board, the Raspberry Pi Pico and it looks like it will have lots of features that will be of interest for Simple DIY Electronic Music Projects.

And the best bit?  It already has a huge, supportive community to grow around it, extensive documentation, some really neat hardware features (thanks to it’s custom chip from the RPi people)., and you can buy one for just £3.60!  In fact, if you subscribe to Hackspace Magazine (as I do) you get one for free with Issue 39.

I’m not going to go over it in detail or “review” it, as you can essentially pick any of your regular online maker news sites and read about it there (here is a good start), but this is my MIDI “Hello World” as it were.

Warning! I strongly recommend using old or second hand equipment for your experiments.  I am not responsible for any damage to expensive instruments!

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

Update Feb 2021: If you are after MIDI IN functionality, then see MIDI In for 3.3V Microcontrollers.

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

Parts list

The Circuit

PiPicoMIDIOut_bb

First a note of warning.  That is a lot of pins to push into a breadboard, and even more when it comes to trying to pull it off again. I could see the PCB flexing quite a lot when inserting and removing – so be careful, do it slowly, and try to lever it up by the plastic on the pins not the PCB itself if you can.

This is hooking up MIDI to the second UART port of the Raspberry Pi Pico, which can be found on GP4 and GP5 (pins 6 and 7).  Transmit (TX) is GP4, which is all I need for MIDI OUT. If like me you find DIN connector pinouts confusing, then take a look at the MIDI Connections Cheat Sheet.

A side note on voltage and current…

One thing to watch out for is that the Pi Pico has 3.3V signal levels, but there was a revision of the MIDI electrical specification that showed how to hook up a MIDI out port to 3.3V signals.  This is detailed in the “(CA-033) MIDI 1.0 Electrical Specification Update [2014]”:

MIDI OUT

The core difference being that the resistor values are different for 3.3V operation.  A 33Ω resistor is required to link to the 3.3V power and DIN pin 4; and a 10Ω resistor is required to link to the TX pin and DIN pin 5.

The Raspberry Pi Pico isn’t totally clear on the subject of GPIO current limits, but as the above components are designed to implement at 5mA current loop, as long as the GPIO can support 5mA all should be fine.  The only statements I can find are:

  • “The amount of current a GPIO sources or sinks is dependant on the load attached to it.” (RP2040 datasheet, 5.3.3.5).
  • “Sum of all current being sourced/sunk into GPIO and QSPI pins” is 50mA total (RP2040 datasheet, Table 622).

For “robust” applications, it is recommended to use a buffer (e.g. two inverters in series) on the transmit side between the TX pin and the MIDI circuitry.  This will protect your 3V3 device in cases where it gets plugged into another device with a dodgy or faulty MIDI IN circuit.  Without some kind of buffer there is a risk that a faulty receiving device could draw too much current from your Pico.

Using something like a 74HCT14 (the HCT version not the HC version) has the added advantage that it can act as a level shifter too if powered from a 5V supply.  A 3V3 TX signal can go on and a 5V TX signal can come out.

If you are after a MIDI interface for a full Raspberry Pi, I can recommend the design from the Clumsy MIDI interface, which I’ve used myself a number of times too.  Considering the relative cost of a Pi vs a Pico, it is worth doing it properly to protect your Pi!

I might come back to showing how to do a full DIY buffered MIDI OUT and THRU circuit at some point.

Off-the-shelf Modules

Alternatively, there are Ready-Made MIDI Modules that support a 3.3V signal connection too.  I tried it with my Hobbytronics MIDI module, which has a solder jumper to select 3.3V signals (highlighted below), and it worked fine.

The power to the module still has to be 5V though, but the Pi Pico VBUS line will provide 5V when it is powered from USB so I used that.  The links to the module are shown below.

PiPico-HobbyTronics

The Code

I’m not going to go into how you get the Raspberry Pi Pico up and running.  There are lots of details in the Raspberry Pi Pico Getting Started pages.

For this first application I’ve used the MicroPython environment with the Thonny Python editor and development environment.  There is a UART class for the built-in RP2 “machine” so I’ve used that with UART1 (not UART0) which is linked up to GP4 and GP5 (pins 6 and 7), and driven it directly to write out the required MIDI message values at the MIDI baud rate of 31250.

I also kept in the flashing of the built-in LED (GP25) as a visual indicator that it was doing something.

Here is the complete test code (hear it playing here).

import machine
import time
import ustruct

pin = machine.Pin(25, machine.Pin.OUT)
uart = machine.UART(1,31250)

notes = [60,61,62,63,64,63,62,61]

while True:
for x in notes:
pin.value(1)
uart.write(ustruct.pack("bbb",0x90,x,127))
time.sleep(0.5)
pin.value(0)
time.sleep(0.5)
uart.write(ustruct.pack("bbb",0x80,x,0))

This constructs the MIDI messages as a “bbb” packed structure – i.e. three consecutive bytes – and sends them out over the UART using the uart.write() call.  If you are familiar with MIDI messages you will see a MIDI noteOn message on channel 1 first (0x90), for note “x” at volume 127, followed by a MIDI noteOff message on channel 1 (0x80), for note “x” at volume 0.

I sat and typed in all the note numbers for Bach’s Prelude in C Major (BWV846), as the duration of all the notes are the same.  You can see the result in the video at the start of this post.

There is a MIDI library for MicroPython, but I haven’t looked into how to get that across to the Pi Pico yet.

Closing Thoughts

There is so much about the Raspberry Pi Pico that looks interesting for music, this post really is just a first look.  Both Adafruit and Arduino are releasing boards with the new RP2040 chip (the heart of the Pico) and there is support from the Arduino environment, Circuit Python, as well as options for directly programming it in C.

It has support for lots of PWM outputs and a whole hardware subsystem of programmable IO.  The downside from an audio point of view, is that from what I’ve seen so far, I don’t think there is an actual DAC output and it looks like there is only one timer (as far as I can see).

But there are already audio peripherals out there.  Alongside my Pico in the post, I’ve also received the PicoAudio from Pimoroni, which gives me a high quality DAC over I2S, so I’m looking forward to having a play with that.

Kevin

14 thoughts on “MIDI, MicroPython and the Raspberry Pi Pico

  1. This is great, thanks a ton. The ONLY information I could find online tackling hardware MIDI-out from a MC or SBC. With no experience in microcontrollers or Python at all (but some in electronics in general as well as lots of Lua scripting) i have now prototyped a pedalboard to control an old Rocktron Chameleon (preset up/down, individual fx on/off, and expression pedal via analog volume pedal > ADC) in less than 2 evenings.

    Like

  2. This guide is awesome! One question- am I reading it wrong or does the midi spec image for 3.3v specify 33ohms for RA to Pin 4 while the text in the paragraph following the image has that to Pin 5? (Etc. For 10ohms…) basically, are they reversed in the text or the image?

    Like

    1. No – you read it right! My text was wrong – 33R should be in the VCC link as it is in the MIDI spec (of course) and the Fritzing diagram and 10R in the TX line. Now updated 🙂

      MIDI pin numbers will surely be my undoing! That is one of the reasons I ended writing the MIDI Cheat Sheet 🙂

      Thanks,
      Kevin

      Like

    2. Funny coincidence that you saw this and posted today, as I finally got around to finishing the rough of my MIDI pedalboard today that I described above. Still need to co-ordinate some date between the Pico and the Chameleon, as the individual FX on/off and expression pedal will control different parameters depending on the patch, but for now it’s all assembled and working. I never noticed the discrepancy in the text, as I would have skimmed the text and then actually built based off the diagrams.

      Once again, this is the only resource online for Pico and DIN MIDI that I’ve found, which is crazy, since that’s such a perfect use-case for such a cheap little board.

      Excuse the absolute mess, but if you want to see the pedal it’s here: https://youtu.be/JpFNoA6fFlE with the enclosure built entirely out of wood and plastic I found on the side of the road on garbage day, cut and assembled with a backsaw and a dremel :’)

      Like

  3. Hi! Great tutorial. Do you know if it’s possible to use this same code but adding usb functionality, so I can use the Pico as a midi controller for my daw? I’ve been googling a lot and this topic seems absurdly complex, compared to Arduino for example.

    Like

    1. Micropython doesn’t have good USB MIDI support (yet). Most people look to Circuitpython on the Pico for that – I have a couple of posts on that (see the tags) but Adafruit have a whole pile of learning guides on using CIrcuitpython with USB MIDI.

      Kevin

      Like

Leave a comment