Reading incoming MIDI messages can come in handy for troubleshooting, hacking or general curiosity. In this example, you'll see how to receive MIDI in messages and display them on an LCD screen.

Circuit Diagram

You can easily connect the LCD to the QT Py RP2040 with a STEMMA QT cable.

  • LCD 3V to QT Py RP2040 3V
  • LCD GND to QT Py RP2040 GND
  • LCD SDA to QT Py RP2040 SDA
  • LCD SCL to QT Py RP2040 SCL

Setup the QT Py RP2040

This example uses an QT Py RP2040. To make sure it is setup properly with CircuitPython, please follow the steps in the guide below.

CircuitPython Code

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
import displayio
import terminalio
from adafruit_display_text import label
import adafruit_displayio_ssd1306
from adafruit_midi.control_change import ControlChange
from adafruit_midi.note_off import NoteOff
from adafruit_midi.note_on import NoteOn
from adafruit_midi.pitch_bend import PitchBend

displayio.release_displays()

oled_reset = board.D1

# I2C setup for display

# STEMMA I2C setup pre-CP 7.2
i2c = busio.I2C(board.SCL1, board.SDA1)

#  STEMMA I2C setup for CP 7.2+
#  i2c = board.STEMMA_I2C()

display_bus = displayio.I2CDisplay(i2c, device_address=0x3D, reset=oled_reset)

#  midi setup
print(usb_midi.ports)
midi = adafruit_midi.MIDI(
    midi_in=usb_midi.ports[0], in_channel=0, midi_out=usb_midi.ports[1], out_channel=0
)

msg = midi.receive()

#  display width and height setup
WIDTH = 128
HEIGHT = 64
BORDER = 5

#  display setup
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=WIDTH, height=HEIGHT)

splash = displayio.Group()
display.show(splash)

# text area setup
text = "MIDI Messages"
text_area = label.Label(
    terminalio.FONT, text=text, color=0xFFFFFF, x=30, y=HEIGHT // 2+1)
splash.append(text_area)

while True:
    #  receive midi messages
    msg = midi.receive()

    if msg is not None:
        #  if a NoteOn message...
        if isinstance(msg, NoteOn):
            string_msg = 'NoteOn'
            #  get note number
            string_val = str(msg.note)
        #  if a NoteOff message...
        if isinstance(msg, NoteOff):
            string_msg = 'NoteOff'
            #  get note number
            string_val = str(msg.note)
        #  if a PitchBend message...
        if isinstance(msg, PitchBend):
            string_msg = 'PitchBend'
            #  get value of pitchbend
            string_val = str(msg.pitch_bend)
        #  if a CC message...
        if isinstance(msg, ControlChange):
            string_msg = 'ControlChange'
            #  get CC message number
            string_val = str(msg.control)
        #  update text area with message type and value of message as strings
        text_area.text = (string_msg + " " + string_val)

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. 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.

How the CircuitPython Code Works

msg is acting as a variable to hold the incoming MIDI messages received with midi.receive(). Every time a new message comes in, the text on the screen is updated using text_area.text. The message values are converted to strings using str() so that they can be displayed.

Usage

You can connect your QT Py RP2040 via USB to either your computer or a USB MIDI host. Setup your software to send MIDI messages out and you will see the messages update in real time on the screen. 

You can use this project to figure out what MIDI messages are being sent out, especially with ControlChange messages.

This guide was first published on Mar 02, 2022. It was last updated on 2022-03-02 13:24:41 -0500.

This page (Receive and Display MIDI Messages) was last updated on May 20, 2022.

Text editor powered by tinymce.