Once you've finished setting up your QT Py RP2040 with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.

To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.

# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
# SPDX-License-Identifier: MIT

import board
import busio
import usb_midi
import adafruit_midi
# pylint: disable=unused-import
from adafruit_midi.control_change import ControlChange
from adafruit_midi.pitch_bend import PitchBend
from adafruit_midi.note_off import NoteOff
from adafruit_midi.note_on import NoteOn
from adafruit_midi.program_change import ProgramChange

#  uart setup
uart = busio.UART(board.TX, board.RX, baudrate=31250, timeout=0.001)
#  midi channel setup
midi_in_channel = 1
midi_out_channel = 1
#  midi setup
#  UART is setup as the input
#  USB is setup as the output
midi = adafruit_midi.MIDI(
    midi_in=usb_midi.ports[0],
    midi_out=uart,
    in_channel=(midi_in_channel - 1),
    out_channel=(midi_out_channel - 1),
    debug=False,
)

while True:
    #  receive MIDI message over USB
    msg = midi.receive()
    #  if a message is received...
    if msg is not None:
        #  send that message over UART
        midi.send(msg)
        #  print message to REPL for debugging
        print(msg)

Upload the Code and Libraries to the QT Py RP2040

After downloading the Project Bundle, plug your QT Py RP2040 into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the QT Py RP2040's CIRCUITPY drive. 

  • lib folder
  • code.py

Your QT Py RP2040 CIRCUITPY drive should look like this after copying the lib folder and the code.py file.

circuitpy

How the CircuitPython Code Works

The code begins by importing the libraries. All of the MIDI message libraries are imported so that any incoming MIDI message type will be recognized.

import usb_midi
import adafruit_midi
import board
import busio
import time
from adafruit_midi.control_change import ControlChange
from adafruit_midi.pitch_bend import PitchBend
from adafruit_midi.note_off import NoteOff
from adafruit_midi.note_on import NoteOn
from adafruit_midi.program_change import ProgramChange

This is followed by the MIDI setup. The QT Py RP2040's USB port is setup as the MIDI input and the UART pin is setup as the MIDI output. This means that MIDI messages are received over USB and sent out over UART.

#  uart setup
uart = busio.UART(board.TX, board.RX, baudrate=31250)
#  midi channel setup
midi_in_channel = 1
midi_out_channel = 1
#  midi setup
#  USB is setup as the input
#  UART is setup as the output
midi = adafruit_midi.MIDI(
    midi_in=usb_midi.ports[0],
    midi_out=uart,
    in_channel=(midi_in_channel - 1),
    out_channel=(midi_out_channel - 1),
    debug=False,
)

In the loop, if a MIDI message is received it is stored as msg. That msg is then sent out over UART exactly as it is received. This allows for direct USB to UART MIDI communication. 

while True:
    msg = midi.receive()
    if msg is not None:
        midi.send(msg)
        print(msg)

This guide was first published on Apr 26, 2022. It was last updated on Apr 26, 2022.

This page (Coding the QT Py RP2040 USB to Serial MIDI Friends) was last updated on Sep 29, 2023.

Text editor powered by tinymce.