-
-
Save TheEyesightDim/818b219db90dda5ac191f20051415a46 to your computer and use it in GitHub Desktop.
MIDI CC knob controller example for Raspberry Pi Pico
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import board | |
import usb_midi | |
import adafruit_midi | |
from analogio import AnalogIn | |
from adafruit_midi.control_change import ControlChange | |
# MIDI CC knob controller example for Raspberry Pi Pico | |
# Prerequisites | |
# | |
# Requires Adafruit CircuitPython: https://learn.adafruit.com/getting-started-with-raspberry-pi-pico-circuitpython | |
# Also requires the following CircuitPython libs: adafruit_midi (drop it into the lib folder) | |
# | |
# Save this code in code.py on your Raspberry Pi Pico CIRCUITPY drive | |
# Read analog pin voltage | |
def get_voltage(pin): | |
return (pin.value * 3.3) / 65536 | |
# The potentiometers' middle pins are connected to analog pins 27 and 28, the | |
# outer pins to GND and 3v3. | |
knob_1 = AnalogIn(board.GP27) | |
knob_2 = AnalogIn(board.GP28) | |
# Set up MIDI | |
midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0) | |
# Use these variables to only update when the value changes | |
last_knob_1_val = 0 | |
last_knob_2_val = 0 | |
while True: | |
# Read voltages and convert to 0-127 (MIDI note range) | |
knob_1_val = min(max(int(get_voltage(knob_1) / 3.3 * 128), 0), 127) | |
knob_2_val = min(max(int(get_voltage(knob_2) / 3.3 * 128), 0), 127) | |
# If value has changed since last value, send MIDI CC message | |
if knob_1_val != last_knob_1_val: | |
midi.send(ControlChange(1, knob_1_val)) | |
last_knob_1_val = knob_1_val | |
if knob_2_val != last_knob_2_val: | |
midi.send(ControlChange(2, knob_2_val)) | |
last_knob_2_val = knob_2_val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just forking for reference later