Here's a quick example to get you started with MIDI in CircuitPython.

Install the latest CircuitPython on your Feather board, then download the Library bundle.

Drag a copy of the adafruit_midi library onto your CIRCUITPY drive's lib folder.

You can check out lots more details on this library here.

This example will play notes on MIDI channel 1 with MIDI NoteOn and NoteOff as well as send modulation changes with the ControlChange command.

Plug a DIN-5 MIDI cable or 3.5mm TRS cable into the MIDI FeatherWing MIDI out port on one end and your favorite MIDI-capable synthesizer on the other.

In the example shown in the video above, a classic DIN-5 MIDI cable is being used to have the Feather send notes to a Doepfer MIDI-to-CV/Gate/CC converter, which in turn drives notes, gate, and modulation on a Winterbloom Castor & Pollux Eurorack synthesizer module.

Copy the code below and paste it into Mu, then save it to your CIRCUITPY drive as code.py.

Once the file is saved to the Feather, it will begin sending MIDI notes to your synthesizer, drum machine, or other rad groove machine!

# SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries
# SPDX-License-Identifier: MIT
# midi_UARToutdemo.py - demonstrates sending MIDI notes

import time
import board
import busio
import adafruit_midi

from adafruit_midi.control_change import ControlChange
from adafruit_midi.note_off import NoteOff
from adafruit_midi.note_on import NoteOn

uart = busio.UART(board.TX, board.RX, baudrate=31250, timeout=0.001)  # init UART
midi_in_channel = 2
midi_out_channel = 1
midi = adafruit_midi.MIDI(
    midi_in=uart,
    midi_out=uart,
    in_channel=(midi_in_channel - 1),
    out_channel=(midi_out_channel - 1),
    debug=False,
)
note_hold = 0.85
rest = note_hold / 5

print("MIDI Out demo")
print("Default output channel:", midi.out_channel + 1)

while True:
    # midi.send(ControlChange(64, 0))  # sustain CC
    midi.send(ControlChange(1, 0))  # modulation CC

    midi.send(NoteOn(48, 20))  # play note
    time.sleep(note_hold)  # hold note
    midi.send(NoteOff(48, 0))  # release note
    time.sleep(rest)  # rest

    midi.send(NoteOn(55, 40))
    time.sleep(note_hold)
    midi.send(NoteOff(55, 0))
    time.sleep(rest)

    midi.send(NoteOn(51, 60))
    time.sleep(note_hold)
    midi.send(NoteOff(51, 0))
    time.sleep(rest)

    midi.send(NoteOn(58, 80))
    time.sleep(note_hold)
    midi.send(NoteOff(58, 0))
    time.sleep(rest)

    # midi.send(ControlChange(64, 32))
    midi.send(ControlChange(1, 127))

    midi.send(NoteOn(48, 20))  # play note
    time.sleep(note_hold)  # hold note
    midi.send(NoteOff(48, 0))  # release note
    time.sleep(rest)  # rest

    midi.send(NoteOn(55, 40))
    time.sleep(note_hold)
    midi.send(NoteOff(55, 0))
    time.sleep(rest)

    midi.send(NoteOn(51, 60))
    time.sleep(note_hold)
    midi.send(NoteOff(51, 0))
    time.sleep(rest)

    midi.send(NoteOn(50, 80))
    time.sleep(note_hold)
    midi.send(NoteOff(50, 0))
    time.sleep(rest)

This guide was first published on Mar 23, 2021. It was last updated on 2021-03-26 17:59:15 -0400.

This page (CircuitPython MIDI Example) was last updated on Oct 12, 2021.

Text editor powered by tinymce.