from machine import Pin, PWM
import time
servo = PWM(Pin(2), freq=50)
def set_angle(angle):
duty = int(((angle)/180 * 2 + 0.5) / 20 * 1023)
servo.duty(duty)
btn0 = Pin(4, Pin.IN, Pin.PULL_UP)
btn90 = Pin(5, Pin.IN, Pin.PULL_UP)
btn180 = Pin(18, Pin.IN, Pin.PULL_UP)
while True:
if btn0.value()==0:
set_angle(0)
print("Servo moved to 0")
time.sleep(0.3)
elif btn90.value()==0:
set_angle(90)
print("Servo moved to 90")
time.sleep(0.3)
elif btn180.value()==0:
set_angle(180)
print("Servo moved to 180")
time.sleep(0.3)