Read Knobs in Python
Time to try things out! Fire up your Pi 400 and then launch a Python editor such as Thonny or Mu.
Copy the code below, paste it into your Python editor, and then run the code by pressing the "play" button.
#!/usr/bin/env python import time import board import adafruit_pcf8591.pcf8591 as PCF from adafruit_pcf8591.analog_in import AnalogIn i2c = board.I2C() pcf = PCF.PCF8591(i2c) pcf_in_0 = AnalogIn(pcf, PCF.A0) pcf_in_1 = AnalogIn(pcf, PCF.A1) pcf_in_2 = AnalogIn(pcf, PCF.A2) pcf_in_3 = AnalogIn(pcf, PCF.A3) while True: a_val = pcf_in_0.value print((a_val)) b_val = pcf_in_1.value print(b_val) c_val = pcf_in_2.value print((c_val)) d_val = pcf_in_3.value print(d_val) time.sleep(0.1)
Turn the knobs and you'll see the four values changing, from 0 to 65,535.
Copy the code shown below and paste it into a new empty document in Thonny or Mu. You can save it as code.py
and then run it.
This will cause the knob readings to send out OSC messages. Next, we'll create Sonic-Pi code to read the messages and play music!
# SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries # SPDX-License-Identifier: MIT #!/usr/bin/env python import time import board from adafruit_simplemath import map_range import adafruit_pcf8591.pcf8591 as PCF from adafruit_pcf8591.analog_in import AnalogIn from pythonosc import udp_client sender = udp_client.SimpleUDPClient("127.0.0.1", 4560) sender.send_message("/trigger/prophet", [43, 110, 1, 0.7]) i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller pcf = PCF.PCF8591(i2c) pcf_in_0 = AnalogIn(pcf, PCF.A0) pcf_in_1 = AnalogIn(pcf, PCF.A1) pcf_in_2 = AnalogIn(pcf, PCF.A2) pcf_in_3 = AnalogIn(pcf, PCF.A3) try: while True: osc_0_val = int(255 - (pcf_in_0.value / 256)) # convert values to useful ranges osc_1_val = int(255 - (pcf_in_1.value / 256)) osc_2_val = int(255 - (pcf_in_2.value / 256)) osc_3_val = int(255 - (pcf_in_3.value / 256)) osc_note_val = int( map_range(osc_0_val, 0, 255, 43, 58) ) # map values to relevant ranges osc_cutoff_val = int(map_range(osc_1_val, 0, 255, 30, 110)) osc_sustain_val = map_range(osc_2_val, 0, 255, 0.2, 2) osc_gain_val = map_range(osc_3_val, 0, 255, 0, 1.0) # print((osc_note_val, osc_cutoff_val, osc_sustain_val, osc_gain_val)) # for plotter sender.send_message( "/trigger/prophet", [osc_note_val, osc_cutoff_val, osc_sustain_val, osc_gain_val], ) time.sleep(0.001) except KeyboardInterrupt: print("done")
Sonic-Pi Script
Here's a Sonic-Pi script that will listen for the OSC values sent from the knob readings, and use those values to adjust the notes, filter cutoff, sustain, and gain.
Copy this and then paste it into a new buffer in Sonic-Pi.
live_loop :knob_patch do use_real_time a, b, c, d = sync "/osc*/trigger/prophet" synth :prophet, note: a, cutoff: b, sustain: c, amp: d sleep 0.25 4.times do # loop a short arpeggio a=a+2 synth :prophet, note: a, cutoff: b, amp: d sleep 0.25 end end
Page last edited January 22, 2025
Text editor powered by tinymce.