Servo motor sometimes struggles moving to right position
Posted: Sun Mar 29, 2020 7:56 pm
I have RPI 3 b+ and i have servo connected to it via 5V power and GNd and GPIO 18 and the problem is sometimes the servo moves well and sometimes it looks like it struggles moving randomly in different directions which cause lock don't open. any help will be appreciated. here is the code:
Code: Select all
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
pwm=GPIO.PWM(12, 50)
pwm.start(0)
def SetAngle(angle):
duty = angle / 18 + 2.5
GPIO.output(12, True)
pwm.ChangeDutyCycle(duty)
time.sleep(0.6)
GPIO.output(12, False)
pwm.ChangeDutyCycle(0)
if __name__ == '__main__':
try:
while True:
time.sleep(2)
SetAngle(90)
print("done close")
time.sleep(2)
SetAngle(0)
print("done open")
except KeyboardInterrupt:
print("Goodbye")
pwm.stop()
GPIO.cleanup()