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.

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 10Ω resistor is required to link to the 3.3V power and DIN pin 4; and a 33Ω resistor is required to link to the TX pin and DIN pin 5.

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

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s