# This code is lifted from a tutorial by Rui Santos & Sara Santos # https://randomnerdtutorials.com/raspberry-pi-pico-servo-motor-micropython from machine import Pin, PWM from time import sleep import random # Set up PWM Pin for servo control servo_pin = machine.Pin(0) servo = PWM(servo_pin) # Set Duty Cycle for Different Angles max_duty = 7864 min_duty = 1802 off = 0 #half_duty = int(max_duty/2) half_duty = int((max_duty+min_duty)/2) #Set PWM frequency frequency = 50 servo.freq (frequency) try: while True: #Servo at 90 degrees servo.duty_u16(half_duty) sleep(random.randint(7,30)) #Servo at 180 degrees servo.duty_u16(max_duty) sleep(random.randint(8,28)) servo.duty_u16(off) sleep(random.randint(2,6)) servo.duty_u16(min_duty) sleep(random.randint(3,33)) servo.duty_u16(off) sleep(random.randint(2,18)) except KeyboardInterrupt: print("Keyboard interrupt") # Turn off PWM servo.deinit()