import machine
# Load the time module for the delays
import time
# Create a regular p23 GPIO object
p23 = machine.Pin(2, machine.Pin.OUT)
# Create another object named pwm by
# attaching the pwm driver to the pin
pwm = machine.PWM(p23)
# Set the pulse every 20ms
pwm.freq(50)
# Set initial duty to 0
# to turn off the pulse
pwm.duty(0)
# Creates a function for mapping the 0 to 180 degrees
# to 20 to 120 pwm duty values
def map(x, in_min, in_max, out_min, out_max):
    return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
# Creates another function for turning 
# the servo according to input angle
def servo(pin, angle):
    pin.duty(map(angle, 0, 180, 20, 120))

# To rotate the servo motor to 0 degrees
servo(pwm, 0)
# To rotate the servo motor to 90 degrees
servo(pwm, 90)
# To rotate the servo motor to 180 degrees
servo(pwm, 180)
# To rotate the servo from 0 to 180 degrees
# by 10 degrees increment
for i in range(0, 181, 10):
    servo(pwm, i)
    time.sleep(0.5)
    
# To rotate the servo from 180 to 0 degrees
# by 10 degrees decrement
for i in range(180, -1, -10):
    servo(pwm, i)
    time.sleep(0.2)
    
servo(pwm, 180)
time.sleep(1)
servo(pwm, 0)
time.sleep(1)
servo(pwm, 180)

-------------

import machine
import time

p15 = machine.Pin(15, machine.Pin.OUT)
pwm = machine.PWM(p15)
pwm.freq(50)
pwm.duty(0)

def map(x, in_min, in_max, out_min, out_max):
    return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)

def servo(pin, angle):
    pin.duty(map(angle, 0, 180, 20, 120))


servo(pwm, 0)

for i in range(0, 181, 10):
    servo(pwm, i)
    time.sleep(0.5)
    
for i in range(180, -1, -10):
    servo(pwm, i)
    time.sleep(0.5)



from time import sleep
from machine import Pin
from machine import PWM
pwm = PWM(Pin(0))
pwm.freq(50)
#Function to set an angle
#The position is expected as a parameter
def setServoCycle (position):
    pwm.duty_u16(position)
    sleep(0.01)
while True:
    for pos in range(1000,9000,50):
        setServoCycle(pos)
    for pos in range(9000,1000,-50):
        setServoCycle(pos)


-------------------------------------------
import machine import time p23 = machine.Pin(2, machine.Pin.OUT) #led1 = machine.Pin(2, Pin.OUT) button1 = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)#D3 pwm = machine.PWM(p23) pwm.freq(50) pwm.duty(0) def map(x, in_min, in_max, out_min, out_max): return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) def servo(pin, angle): pin.duty(map(angle, 0, 180, 20, 120)) servo(pwm, 0) while True: if button1.value()==0: #led1.value(1) servo(pwm, 0) print("0") #time.sleep(1) if (button1.value()==1): print("180") servo(pwm, 180) #D8 #time.sleep(1)