2

I am building a project wherein I'll be controlling four servos using the Raspberry Pi 3. I wanted to control each pair of servos simultaneously (as in, when one servo is moving to a specified angle, the other servo will also move to a different specified angle). Not concurrent control, but parallel control. Is this possible? How would I go about it?

Also, can I achieve non-jittery servo control without the need for an external hardware controller?

2 Answers 2

2

Use GPIO Zero with pigpio as the back end:

from gpiozero import Servo
from gpiozero.pins.pigpio import PiGPIOFactory
from gpiozero.tools import sin_values, cos_values
from signal import pause

pigpio = PiGPIOFactory()

servo1 = Servo(4)
servo2 = Servo(14)

servo1.source = sin_values()
servo2.source = cos_values()

pause()

This example sets each servo to move back and forth at the same rate simultaneously but in different phases. But depending on how you want them to be driven, this could be using threads or something else. Another example where they were driven by buttons (where the events happen in threads without you writing threads yourself):

btn1 = Button(2)
btn2 = Button(3)

btn1.when_pressed = servo1.min
btn1.when_released = servo1.max

btn2.when_pressed = servo2.max
btn3.when_released = servo2.min

It will work without PiGPIOFactory but you'll get jitter on the servos.

Read more about the Servo class, pin factories and more in the gpiozero docs.

0

There are several libraries which allow proper servo control without the need of external PWM generators.

My pigpio library will let you do what you want.

pigpio servo examples.

  1. Python
  2. C
  3. C using pigpio daemon

Other suitable libraries are servoblaster and the RPIO.GPIO Python module (note, not the similarly named RPi.GPIO).

2
  • Thank you. I will check this out. I was wondering if I could control two servos simultaneously, say, through two separate threads. Is this possible?
    – P_Lash
    Commented Jan 29, 2018 at 18:48
  • @P_Lash You can use multiple threads if needed by your application. I've never found the need to do so though.
    – joan
    Commented Jan 29, 2018 at 21:19

Your Answer

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

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