from machine import Pin, PWM
import utime
# Initialize PWM on a pin (e.g., GP18) and set frequency to 50Hz
servo_pin = Pin(15)
servo = PWM(servo_pin)
servo.freq(50)
MIN_DUTY_NS = 1000000
MID_DUTY_NS = 1500000
MAX_DUTY_NS = 2000000
# --- Move to specific angles ---
def move():
# Move to 0 degrees (approx. minimum duty)
servo.duty_ns(MIN_DUTY_NS) # Adjust value based on your servo
utime.sleep(1)
# Move to 90 degrees (middle duty)
servo.duty_ns(MID_DUTY_NS) # Adjust value
utime.sleep(1)
# Move to 180 degrees (approx. maximum duty)
servo.duty_ns(MAX_DUTY_NS) # Adjust value
utime.sleep(1)
# Stop the PWM signal
# servo.deinit()
while True:
move()
# In MicroPython, the utime module is the primary module for
# time-related functions, essentially serving as a
# MicroPython-specific implementation of the standard Python
# time module. While you can often import utime or import
# time, they refer to the same module in MicroPython.
# In MicroPython's machine.PWM class, duty_u16 and duty_ns are
# two different ways of specifying the PWM duty cycle (the "ON"
# time of the pulse). The primary difference is the unit of
# measurement and the precision they offer