"""
MicroPython code for ESP32
Usage:
1. Go to ...
2.
"""
from machine import Pin,PWM
import time
PWM_PIN = 22
PWM_FREQ = 50 #Per ESP32 fino a 5kHZ, Arduino Uno fino a 500-499 Hz
def servoDir(dirDeg, minDeg=26, maxDeg=123):
riscala = (maxDeg-minDeg+1)/180
return int(dirDeg*riscala)+minDeg
PinServo = Pin(PWM_PIN, mode=Pin.OUT)
ServoMotore = PWM(PinServo)
ServoMotore.freq(PWM_FREQ)
ServoMotore.duty(0)
#ServoMotore = PWM(Pin(PWM_PIN), freq=PWM_FREQ, duty=0)
while True:
print(servoDir(0))
ServoMotore.duty(servoDir(0))
time.sleep(1)
print(servoDir(90))
ServoMotore.duty(servoDir(90))
time.sleep(1)
print(servoDir(180))
ServoMotore.duty(servoDir(180))
time.sleep(1)
"""
import machine
from machine import Pin, PWM
import utime, math
from time import sleep
# pwm
pwm = PWM(Pin(2), freq=50, duty=0)
def Servo(servo, angle):
# angle / 180( * 2(0°-180°) + 0.5()/ 20ms * 1023
pwm.duty(int(((angle)/180 *2 + 0.5) / 20 * 1023))
def moveServo(position):
Servo(pwm, position)
utime.sleep(1)
print("Servo Angle : ",position)
"""
"""
# PWM control of a servo motor without any external libraries
from machine import Pin
from time import sleep_us
servo=Pin(0,Pin.OUT)
pulseWidth=1000
while True:
#create a small high pulse followed by a longer low signal with a total period of 20ms
servo.on()
sleep_us(pulseWidth)
servo.off()
sleep_us(20000-pulseWidth)
print(pulseWidth)
pulseWidth=pulseWidth+1; #change the increment to change the speed of the servo
if pulseWidth>2000:
pulseWidth=1000
"""