Let's make a simple MIDI note player in CircuitPython. This will give you a good idea of how to send MIDI data over the NeoTrellis M4's UART/serial port, which you can then adapt to make your own arpeggiators, sequencers, modal players, chord keyboards for polyphonic synths, and more.

CircuitPython is designed to simplify experimentation and education on low-cost microcontrollers. It makes it easier than ever to get prototyping by requiring no upfront desktop software downloads. Simply copy and edit files on the CIRCUITPY flash drive to iterate.

Are you new to using CircuitPython? No worries, there is a full getting started guide here.

Adafruit suggests using the Mu editor to edit your code and have an interactive REPL in CircuitPython. You can learn about Mu and its installation in this tutorial.

First, let's prep the board for that.

CircuitPython Prep

To get prep the Trellis M4 to run the sequencer code, follow these steps:

Now, copy the following code, paste it into Mu and then save it to your NeoTrellis M4's CIRCUITPY drive as code.py

A hat tip to Friend of Adafruit, Tod Kurt for creating the initial CircuitPython MIDI over UART example code.

# Simple example of sending MIDI via UART to classic DIN-5 (not USB) synth

import adafruit_trellism4

import board
import busio
midiuart = busio.UART(board.SDA, board.SCL, baudrate=31250)
print("MIDI UART EXAMPLE")

trellis = adafruit_trellism4.TrellisM4Express()

def wheel(pos):
    if pos < 0 or pos > 255:
        return 0, 0, 0
    if pos < 85:
        return int(255 - pos * 3), int(pos * 3), 0
    if pos < 170:
        pos -= 85
        return 0, int(255 - pos * 3), int(pos * 3)
    pos -= 170
    return int(pos * 3), 0, int(255 - (pos * 3))


for x in range(trellis.pixels.width):
    for y in range(trellis.pixels.height):
        pixel_index = (((y * 8) + x) * 256 // 2)
        trellis.pixels[x, y] = wheel(pixel_index & 255)

current_press = set()

while True:
    pressed = set(trellis.pressed_keys)

    for press in pressed - current_press:
        x, y = press
        print("Pressed:", press)
        noteval = 36 + x + (y * 8)
        midiuart.write(bytes([0x90, noteval, 100]))

    for release in current_press - pressed:
        x, y = release
        print("Released:", release)
        noteval = 36 + x + (y * 8)
        midiuart.write(bytes([0x90, noteval, 0]))  # note off

    current_press = pressed

Hook up the NeoTrellis M4 to your synthesizer over the UART-to-MIDI adapter cabling and now when you press a button, you'll send a MIDI note on message, and when you release a button, it'll send a MIDI note off.

This can serve as your launching off point for more elaborate CircuitPython MIDI projects!

This guide was first published on Nov 20, 2018. It was last updated on Nov 20, 2018.
This page (Code with CircuitPython) was last updated on Sep 28, 2020.