import time
from machine import Pin, PWM
time.sleep(0.1) # Wait for USB to become ready
print("Hello, CIT!")
servo = PWM(Pin(0))
servo.freq(50)
# Move to the value encoded by a particular pulse width
pulse_width = 2
servo.duty_ns(int(pulse_width * 1e6))
print(f"pulse width: {pulse_width}ms")
time.sleep(2)
def sweep_duty_ms(ms_values):
for ms in ms_values:
ns = int(ms * 1e6) # Convert milliseconds to nanoseconds
servo.duty_ns(ns)
time.sleep(1)
print(f"Set pulse width to {ms} ms ({ns} ns)")
# List of pulse widths in milliseconds
# Wokwi simulated servos behave differently to physical servos
duty_ms_values = [
2.4, # beyond typical max (use with caution)
2.0, # approx 180 degrees
1.5, # center
1.0, # approx 0 degrees
0.5 # below typical min (use with caution)
]
# Call the function
sweep_duty_ms(duty_ms_values)