Toy Keyboard USB Matrix Decode with the Pi Pico – Part 2

Having added USB MIDI to my “Toy Keyboard” – I wanted the option to choose either USB or serial MIDI as the output.  This returns to the Toy Keyboard USB Matrix Decode with the Pi Pico project to add a serial port option.

IMG_5210

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:

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

Parts list

  • Keyboard arranged as a key matrix.
  • Raspberry Pi Pico
  • 10Ω resistor
  • 33Ω resistor
  • 3.5 TRS socket
  • Sparkfun Audio Jack Breakout (details here)
  • Breadboard and jumper wires.

The Circuit

Pi Pico TRS MIDI OUT_bb

This is adding a MIDI output to the previous project, so the circuit above shows what needs to be added, assuming you’ve already built the keyboard decoding part.

We are essentially adding the 3.3V MIDI out circuit as used several times previously now but there is a difference here.  I didn’t want the physical space taken up by a 5 pin DIN connector, so I’ve opted to use the MIDI TRS format as used on my CD Rack Synth module. I’ve used a neat little breakout board from Sparkfun to house the jack socket, but you can use any setup that works for you.

The required connections are as follows:

  • Pico 3.3V output -> 33Ω resistor -> TRS RING
  • Pico TX (GP0) -> 10Ω resistor -> TRS TIP
  • PIco GND -> TRS GND

Of course you could continue to use a 5 pin DIN output if you wish, remembering that VCC goes to pin 4 and TX goes to pin 5.

The only quirk is that when using serial (UART) MIDI, the Pico still needs power somehow and the easiest way is to power it using the micro USB port.  When used as s MIDI device, it will get power over the USB from the host device.

Here are a few photos showing how I put mine together.  First, soldering on the two resistors and the ground wire.

IMG_5207IMG_5208

Then adding some heatshrink and attaching the two resistors to TX and 3V as required and cutting the GND wire to link up to a convenient GND pad.

IMG_5209

The Code

When I first looked at CircuitPython with the Raspberry Pi Pico, the serial port wasn’t supported.  Thankfully that is no longer the case and certainly with version 6.3 that I’m using here I can use both USB and serial (UART) MIDI.

There are some relatively minor additions required to add in UART MIDI to the code from the Toy Keyboard USB Matrix Decode with the Pi Pico project.

First, import the busio module and initialise the serial port for use with MIDI.

import busio
uart = busio.UART(board.GP0, board.GP1, baudrate=31250)

Then in the noteOn and noteOff functions, just add calls to write the MIDI message out to the serial port in addition to the USB MIDI port.  I’m not really a python person, so I didn’t quite get the typing right to allow me to use the Adafruit NoteOn/NoteOff objects as you do with USB MIDI (I thought this kind of thing was supposed to be trivial?), so as I’m only sending note on/off messages, I just create them directly as an array of bytes.

def noteOn(x):
    usb_midi.send(NoteOn(x,127))
    uart.write(bytes([0x90,x,127]))

def noteOff(x):
    usb_midi.send(NoteOff(x,0))
    uart.write(bytes([0x80,x,0]))

I’ve not done anything here to cope with different MIDI channels.  It is initialised for USBMIDI to use channel 1 (using the value 0 in the code), so I’m doing the same here with the MIDI commands 0x90 and 0x80 which are Note On and Note Off for channel 1, respectively.

Note it sends it over both UART and USB MIDI at the same time.

Find it on GitHub here.

Closing Thoughts

I’ve now encased everything with hot glue but still need to anchor the ribbon cable down somehow, but this is now a really neat functioning MIDI keyboard for my synth modules using MIDI TRS or MIDI USB.

Kevin

One thought on “Toy Keyboard USB Matrix Decode with the Pi Pico – Part 2

Leave a comment