1

I am trying to control my continuous servo motor using this code:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
p = GPIO.PWM(17, 50)
p.start(7.5)
try:
        while True:
                p.ChangeDutyCycle(7.5)
                time.sleep(1)
                p.ChangeDutyCycle(12.5)
                time.sleep(1)
                p.ChangeDutyCycle(2.5)
                time.sleep(1)

except KeyboardInterrupt:
        GPIO.cleanup()

Servo operates correctly as the code sometimes and other it works randomly for the same code. I use another power source for the motor.

I don't think motor is damaged because I use Arduino to control the servo and it works perfectly.

2 Answers 2

1

If by 'works randomly' you mean 'it moves to roughly the right place, but wobbles around a lot' then the problem will be with the Raspberry Pi's Software Pulse-width Modulation (PWM)

The servo needs a consistent pulse to behave consistently, more accurate than the Pi's software can provide. So the pulse is slightly inconsistent which manifests itself in a 'jitter' (in a continuous servo this would be a random change of speed around the correct speed)

Sign up to request clarification or add additional context in comments.

Comments

0

In addition to @JeffUK's answer above -- and I know it's not a software thing, but still germain -- is make sure your servo (even though you say it's powered by another source) and the Pi share a common ground connection. A floating ground can cause all sorts of unpredictable behaviors. It's very common for us software folks to forget that :) little detail when hooking up external devices.

1 Comment

Thanks. I solved problem by using gpio.cleanup() in every loop I use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.