Page 1 of 1

Servo motor sometimes struggles moving to right position

Posted: Sun Mar 29, 2020 7:56 pm
by AGID
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()

Re: Servo motor sometimes struggles moving to right position

Posted: Sun Mar 29, 2020 8:31 pm
by joan
This doesn't sound like a software problem.

Poor connections are more likely.

Need clear photos.

Re: Servo motor sometimes struggles moving to right position

Posted: Mon Mar 30, 2020 5:41 pm
by AGID
Thanks in advance i will look at my connections and come back to you but can low current cause this?

Re: Servo motor sometimes struggles moving to right position

Posted: Mon Mar 30, 2020 6:03 pm
by joan
Possibly. I don't see why that would cause it to move to random positions though.

Re: Servo motor sometimes struggles moving to right position

Posted: Tue Mar 31, 2020 4:23 am
by mwrich4
joan made a comment on other post that got me thinking. (I like the quadrature waveform avatar)

Running in raspbian is not guaranteed real-time (it may find other things that need doing while you are trying some fine control with user code)

Arduino for example, is nearly real-time code. When the required code speed is easily faster than the events being managed, the results are more predictable.

If you have an oscilloscope or logic analyzer, you may want to examine the actual output signals at the servo.

Re: Servo motor sometimes struggles moving to right position

Posted: Tue Mar 31, 2020 10:45 pm
by AGID
mwrich4 wrote:
Tue Mar 31, 2020 4:23 am
joan made a comment on other post that got me thinking. (I like the quadrature waveform avatar)

Running in raspbian is not guaranteed real-time (it may find other things that need doing while you are trying some fine control with user code)

Arduino for example, is nearly real-time code. When the required code speed is easily faster than the events being managed, the results are more predictable.

If you have an oscilloscope or logic analyzer, you may want to examine the actual output signals at the servo.
Very true. I had similar project with servo with arduino and worked flawlessly compared to raspberry but there are advantages using RPI one of it is being more easier and having os that you can manage stuff through remotely, space and more and these was reasons made me move.