Skip to content

Instantly share code, notes, and snippets.

@tylercrumpton
Created July 12, 2020 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylercrumpton/c2ae1746c7dc0a5f02aad2121c7933d3 to your computer and use it in GitHub Desktop.
Save tylercrumpton/c2ae1746c7dc0a5f02aad2121c7933d3 to your computer and use it in GitHub Desktop.
CircuitPython code for sending USB media controls with a foot pedal
import board # Lists the pin names for the board we're using
from time import sleep
# Set up the USB Human Interface Device (HID) libraries
import usb_hid
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
consumer_control = ConsumerControl(usb_hid.devices)
# Set up the analog input pin to read the pedal state
from analogio import AnalogIn
pedal = AnalogIn(board.A7)
# Loop forever, reading the pedal value
while True:
# The pedal value will be between ~35,000 (unpressed) and ~10,000 (fully pressed)
if pedal.value < 15000:
# We detected the pedal was pressed, so send the Play/Pause command over USB:
consumer_control.send(ConsumerControlCode.PLAY_PAUSE)
# Stop reading for a second to prevent double-presses:
sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment