Skip to content

Instantly share code, notes, and snippets.

@gigafide
Last active January 30, 2023 21:17
Show Gist options
  • Save gigafide/3f49d656d4662cd02a2808bc0c4a1c95 to your computer and use it in GitHub Desktop.
Save gigafide/3f49d656d4662cd02a2808bc0c4a1c95 to your computer and use it in GitHub Desktop.
Raspberry Pi Pico servo and potentiometer control
from machine import Pin, PWM, ADC
from time import sleep
pot = ADC(Pin(28))
servo = PWM(Pin(0))
servo.freq(50)
def move_servo(degrees):
if degrees > 180:
degrees = 180
if degrees < 0:
degrees = 0
MAX = 9000
MIN = 1000
newValue = MIN+(MAX-MIN)*(degrees/180)
servo.duty_u16(int(newValue))
while True:
value = pot.read_u16()
print(value)
degree=value*180/65500
move_servo(degree)
sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment