Skip to content

Instantly share code, notes, and snippets.

@maxmacstn
Last active February 21, 2025 01:43
Show Gist options
  • Save maxmacstn/805991009e9302977f694e5b17a62b73 to your computer and use it in GitHub Desktop.
Save maxmacstn/805991009e9302977f694e5b17a62b73 to your computer and use it in GitHub Desktop.
Raspberry Pi Pico - CircuitPython volume knob
import digitalio
import board
import usb_hid
import time
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.mouse import Mouse
from adafruit_hid.keycode import Keycode
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
print("== Pi Pico multifunction knob 1.0 ==")
CLK_PIN = board.GP4
DT_PIN = board.GP3
SW_PIN = board.GP2
clk_last = None
count = 0
totalMode = 3
currentMode = 0
cc = ConsumerControl(usb_hid.devices)
mouse = Mouse(usb_hid.devices)
keyboard = Keyboard(usb_hid.devices)
clk = digitalio.DigitalInOut(CLK_PIN)
clk.direction = digitalio.Direction.INPUT
dt = digitalio.DigitalInOut(DT_PIN)
dt.direction = digitalio.Direction.INPUT
sw = digitalio.DigitalInOut(SW_PIN)
sw.direction = digitalio.Direction.INPUT
sw.pull = digitalio.Pull.UP
def millis():
return time.monotonic() * 1000
def ccw():
print("CCW")
if (currentMode == 0): # Mac brightness down
keyboard.send(Keycode.SCROLL_LOCK)
elif(currentMode ==1): # Mac horizontal scroll right
keyboard.press(Keycode.SHIFT)
mouse.move(wheel=-1)
keyboard.release(Keycode.SHIFT)
elif(currentMode == 2): # Volume decrement
cc.send(ConsumerControlCode.VOLUME_DECREMENT)
def cw():
print("CW")
if (currentMode == 0): # Mac brightness up
keyboard.send(Keycode.PAUSE)
elif(currentMode ==1): # Mac horizontal scroll left
keyboard.press(Keycode.SHIFT)
mouse.move(wheel=1)
keyboard.release(Keycode.SHIFT)
elif(currentMode == 2): # Volume increment
cc.send(ConsumerControlCode.VOLUME_INCREMENT)
def long_press():
#Mac sleep: CMD + OPT + EJECT
keyboard.press(Keycode.ALT, Keycode.COMMAND)
cc.send(ConsumerControlCode.EJECT)
keyboard.release_all()
while(1):
clkState = clk.value
if(clk_last != clkState):
if(dt.value != clkState):
cw()
else:
ccw()
if (sw.value == 0):
pressTime = millis()
time.sleep(0.2)
longPress = False
while(sw.value == 0):
if(millis() - pressTime > 1000 and not longPress):
print("longPress")
longPress = True
long_press()
if (not longPress):
currentMode += 1
currentMode %= totalMode
print("Mode: " + str(currentMode))
clk_last = clkState
@Xitee1
Copy link

Xitee1 commented Apr 22, 2023

If anyone is interested, I've made some improvements to the code, for example fixing the problem that the knob sometimes stops working. Xitee1/multimedia-knob

@maxmacstn
Copy link
Author

If anyone is interested, I've made some improvements to the code, for example fixing the problem that the knob sometimes stops working. Xitee1/multimedia-knob

Thanks!

@K5BBoing
Copy link

got error Traceback (most recent call last):
File "", line 5, in
ImportError: no module named 'adafruit_hid'

@Xitee1
Copy link

Xitee1 commented Oct 19, 2023

got error Traceback (most recent call last): File "", line 5, in ImportError: no module named 'adafruit_hid'

You haven't copied the adafruit_hid library to the pico. Follow the instructions carefully. Check out my version of the knob (comment above). There you can download and extract the library from if you can't find it online.

@K5BBoing
Copy link

thanks will try...just starting here ; - )

@luckkyboy
Copy link

thanks alot...

If anyone is interested, I added some features on this, click on the link for more information luckkyboy/multimedia-knob

@Xitee1
Copy link

Xitee1 commented Feb 2, 2025

thanks alot...

If anyone is interested, I added some features on this, click on the link for more information luckkyboy/multimedia-knob

I finally got to try it out and it works very well so far.
I'll add a link to it in my repo as I will use your's from now on πŸ˜„
But you should definitively enable issues for your repo.
Also, it took me a while to find out that you've swapped the pins which made the knob behave weirdly πŸ˜†

@luckkyboy
Copy link

thanks alot...
If anyone is interested, I added some features on this, click on the link for more information luckkyboy/multimedia-knob

I finally got to try it out and it works very well so far. I'll add a link to it in my repo as I will use your's from now on πŸ˜„ But you should definitively enable issues for your repo. Also, it took me a while to find out that you've swapped the pins which made the knob behave weirdly πŸ˜†

Cooool!
So happy you did that!
You can get more information from readme of GPIO or parts list

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