# ===================== READ ME!!! ============================
# For this example you can only use the LED or the SERVO
# at one time. In this case the LED functionality is commented
# out by being put between the ''' some code '''
# If you want to used the LED example simply move the quotes
# =============================================================
from time import sleep
from machine import Pin
from machine import PWM
from machine import ADC
from machine import Timer
# ====================
# Set up SERVO pin
# ====================
# Set up servo pin
servo = PWM(Pin(21))
servo.freq(50)
# Note: We can easily convert to Degrees once we know the range
DEG_0 = 1700
DEG_90 = 4700
DEG_180 = 7800
INCREMENTS = 33
# This function just sets the current servo position and holds for .25 sec
def setServoCycle(position):
servo.duty_u16(position)
print(f"Position: {position}")
sleep(.05)
# ====================
# Full Function Loop
# ====================
while True:
# We increment by 1 deg from 0 to 180 degrees
for position in range(DEG_0, DEG_180, INCREMENTS):
setServoCycle(position)
# We deccrement by 1 deg from 0 to 180 degrees
for position in range(DEG_180, DEG_0 , -INCREMENTS):
setServoCycle(position)