#################################################################################
# By kaputtnikk #
# #
# Trying to achieve the same as the Arduino-Nano-Example By DrCrowller #
# https://wokwi.com/projects/328312829780165204 # #
# just using Pi Pico / RP 2040 and MicroPython. #
# #
# Created : 18 Feb. 2025 #
#################################################################################
# loading libs
from machine import Pin, PWM
from time import sleep
# define pins
servo_pin = 17
pwm = PWM(Pin(servo_pin))
pwm.freq(50)
button_l = Pin(7, Pin.IN, Pin.PULL_UP)
button_r = Pin(6, Pin.IN, Pin.PULL_UP)
# define some positions might be useful
# 0 degrees
deg000 = 500000
# 90 degrees
deg090 = 1470000
# 180 degrees
deg180 = 2400000
# defining position and step
pos = deg090 # starting position
step = 200000 # works fine here
while True:
if button_l.value() == 0: # check if button was pressed
if pos >= (deg000 + step): # check if full step possible
pwm.duty_ns(pos - step) # doing the step
pos = pos - step # updating position variable
sleep(0.2)
else:
pwm.duty_ns(deg000) #allow lowest position, if full step was not possible
sleep(0.2)
if button_r.value() == 0:
if pos <= (deg180 - step):
pwm.duty_ns(pos + step)
pos = pos + step
sleep(0.2)
else:
pwm.duty_ns(deg180) #allow max position
sleep(0.2)
ROTATE LEFT
ROTATE RIGHT