1

I have two servos which I want to control from my RPi. My problem is that the servos start shaking if I use them both. Both servos are connected to the +5V pin and to the ground of the Pi. So I have written following code:

import RPi.GPIO as GPIO
import time
import sys

signalPinH = 7
signalPinV = 11

GPIO.setmode(GPIO.BOARD)

GPIO.setup(signalPinV,GPIO.OUT)
GPIO.setup(signalPinH,GPIO.OUT)

p = GPIO.PWM(signalPinH,50)
pv = GPIO.PWM(signalPinV, 50)

p.start(0)
pv.start(0)

try:
         while(True):
               try:
                        moveTo = int(raw_input("Number"))
                        p.ChangeDutyCycle(moveTo)
                        pv.ChangeDutyCycle(moveTo)
                        time.sleep(1.5)
                except ValueError:
                        print "Nope."
except KeyboardInterrupt:
        p.stop()
        pv.stop()
        GPIO.cleanup()
2

RPi.GPIO uses software timing for PWM (Pulse Width Modulation). It is not suitable for servos.

You have several choices

  1. put up with the jitter
  2. use one or both of the two hardware PWM channels on, for example, GPIO 12/13.
  3. use a module which generates hardware timed PWM, e.g. servoblaster, or my pigpio Python module.
  4. use external hardware to generate the PWM
| improve this answer | |
  • I'm using WiringPi and now it doesn't shake anymore but I have problems understanding following lines: wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(400) wiringpi.pwmSetRange(1024) //Max. frequency = 1024 – nullexception Jan 25 '16 at 14:49
  • @theCarver Is that using the hardware PWM GPIO? If so you'll need to search for the values to set for a particular pulse width. It should be documented on this site if you do a search. If it's software PWM then you will still get jitter when the system is busy. – joan Jan 25 '16 at 14:56
2

Use RPIO library instead of the default GPIO. It uses DMA for PWM instead of software PWM.

| improve this answer | |
  • RPIO.GPIO PWM does not work on the Pi2, otherwise I may have mentioned it. – joan Jan 25 '16 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.