To use with CircuitPython, you need to first install a few libraries, into the lib folder on your CIRCUITPY drive. Then you need to update with the example script.

Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the file in a zip file. Extract the contents of the zip file, open the directory MIDI_Solenoid_Drum_Kit/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.

Your CIRCUITPY drive should now look similar to the following image:

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

import time
import board
import digitalio
import usb_midi
import adafruit_midi
from adafruit_midi.note_on import NoteOn

#  pins for the solenoid output signals
noid_pins = [board.D5, board.D6, board.D9, board.D10]

#  array for the solenoids
noids = []

#  setup for the solenoid pins to be outputs
for pin in noid_pins:
    noid = digitalio.DigitalInOut(pin)
    noid.direction = digitalio.Direction.OUTPUT

#  MIDI note array
notes = [60, 61, 62, 63]

#  MIDI in setup
midi = adafruit_midi.MIDI(midi_in=usb_midi.ports[0], in_channel=0)

#  delay for solenoids
speed = 0.03
retract = 0

while True:

    #  msg holds MIDI messages
    msg = midi.receive()

    for i in range(4):
        #  states for solenoid on/off
        noid_output = noids[i]

        #  states for MIDI note recieved
        notes_played = notes[i]

        #  if NoteOn msg comes in and the MIDI note # matches with predefined notes:
        if isinstance(msg, NoteOn) and msg.note is notes_played:
            print(time.monotonic(), msg.note)

            #  solenoid is triggered
            noid_output.value = True
            #  quick delay
            retract = time.monotonic()

        #  retracts solenoid using time.monotonic() to avoid delays between notes activating
        if (retract + speed) < time.monotonic():
            noid_output.value = False

Your Feather M4 Express CIRCUITPY drive should look like this after you've copied everything over:


This guide was first published on Aug 05, 2020. It was last updated on Jul 18, 2024.

This page (Coding the MIDI Solenoid Drum Kit) was last updated on Jul 18, 2024.

Text editor powered by tinymce.