AGID
Posts: 36
Joined: Tue Mar 17, 2020 1:02 pm

Servo motor sometimes struggles moving to right position

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()

User avatar
joan
Posts: 14887
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Servo motor sometimes struggles moving to right position

Sun Mar 29, 2020 8:31 pm

This doesn't sound like a software problem.

Poor connections are more likely.

Need clear photos.

AGID
Posts: 36
Joined: Tue Mar 17, 2020 1:02 pm

Re: Servo motor sometimes struggles moving to right position

Mon Mar 30, 2020 5:41 pm

Thanks in advance i will look at my connections and come back to you but can low current cause this?

User avatar
joan
Posts: 14887
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Servo motor sometimes struggles moving to right position

Mon Mar 30, 2020 6:03 pm

Possibly. I don't see why that would cause it to move to random positions though.

mwrich4
Posts: 99
Joined: Wed Jul 25, 2012 1:24 pm
Location: Stuart, Florida

Re: Servo motor sometimes struggles moving to right position

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.

AGID
Posts: 36
Joined: Tue Mar 17, 2020 1:02 pm

Re: Servo motor sometimes struggles moving to right position

Tue Mar 31, 2020 10:45 pm

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.

Return to “Python”