Skip to content

Instantly share code, notes, and snippets.

@sandyjmacdonald
Last active May 26, 2023 12:46
Show Gist options
  • Save sandyjmacdonald/f0d930d24d3675dc74b29103cb1385ec to your computer and use it in GitHub Desktop.
Save sandyjmacdonald/f0d930d24d3675dc74b29103cb1385ec to your computer and use it in GitHub Desktop.
MIDI CC knob controller example for Raspberry Pi Pico
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 by more than 2 since last value, send MIDI CC message
if abs(knob_1_val - last_knob_1_val) > 2:
midi.send(ControlChange(1, knob_1_val))
last_knob_1_val = knob_1_val
if abs(knob_2_val - last_knob_2_val) > 2:
midi.send(ControlChange(2, knob_2_val))
last_knob_2_val = knob_2_val
@Madlost87
Copy link

Madlost87 commented Sep 28, 2021

Hi I wanted to know if it was possible to do the same project with a stereo proximity sensor, i neve reduce mobility hand And he would have been spectacular to use it as input for modify the sound wAve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment