Reputation: 21
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.
Upvotes: 1
Views: 1683
Reputation: 7409
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.
Upvotes: 0
Reputation: 4231
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)
Upvotes: 1