Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

I'd like to control a small RC servo (5 V, 500 mA) from the GPIO pins via Python.

Hardware

  • What are the required electrical connections?
  • Can I connect multiple servos to the Raspberry Pi?
  • Are there any current limitations? Do I need to buffer the output pin with a transistor?
  • Can I power the servo directly from the Raspberry Pi, or should I use an external power supply? Do I need a capacitor to smooth the servo's jerky power draw?

Software

  • What software/Python code do I need? I know I need 20 ms pulses, how can I achieve this?
  • What kind of CPU load can I expect from using the Raspberry Pi in this way?
share|improve this question
    
This is a similar question that could help get you started: raspberrypi.stackexchange.com/questions/27/… –  Andrew Larsson Jun 12 '12 at 22:31

7 Answers 7

The electrical connections required depends on the servo you have. The GPIO provides 3.3 V and up to 16 mA, which is insufficient for your servo, so you will need to buffer it with a transistor.

There are 8 GPIO pins on the expansion header, though other pins can be reconfigured to give up to 17 GPIO pins in total. If you need more, you will have to add some kind of multiplexer.

Getting precisely timed signals will be tricky on the Raspberry Pi, or on any non-real-time OS, as interrupts and task switches can happen any time. You will probably need external electronics to provide accurately timed signals.

Andrew has pointed you to some software. The CPU load will depend on what you are doing, but the GPIO itself does not add any load.

share|improve this answer
1  
On the other hand - 20 ms is a LONG time, even for Raspberry Pi's CPU terms. I wouldn't be worried too much about time precision for RC servos. –  Tibor Jun 13 '12 at 14:56
2  
@Tibor: 20ms is the period of servo pulses, not the width en.wikipedia.org/wiki/Servo_control#Pulse_duration. And the resolution requirement is much worse than that - to get 8 bits of resolution, you need 256 steps across 2 milliseconds, so ~8us. –  Martin Thompson Jun 14 '12 at 8:13

If you are running a realtime operating system on your Raspberry Pi this is indeed possible. As others have said already you will need an external power source to the servos but except for that you do not need any other additional hardware.

If you are running Xenomai (realtime patch for Linux) you can run a completely familiar Linux system, but with added realtime capabilities.

I have written a tiny C library for the purpose of controlling servos from the Raspberry Pi. I have tried controlling 3 servos at the same time with it, but there's really no reason why it shouldn't work with more.

You can find my tiny library here: https://github.com/simonfi/pi-servo Adding a Python wrapper for it should be trivial. Xenomai for Raspberry Pi can be found here: http://powet.eu/2012/07/25/raspberry-pi-xenomai/

Generating the needed signals for three servos is basically consuming a negligible amount of cpu. I have not tested with more than three yet.

share|improve this answer

Servos are, generally, controlled using PWM signals; you should refer to GPIO as PWM output.

There is PWM support on the chip, so we should be able to use that in the future. Basically, all the software does is tell the hardware to produce pulses at a certain frequency. Some people have had success using external PWM controllers over I2C.

share|improve this answer

If you don't mind using a Linux Kernel driver there is servoblaster which exposes the servos as a char device.

https://github.com/richardghirst/PiBits

I have forked a new version which does not consume all of the gpio pins for servos use. I'm currently using such on a small servo driven robot the I have built and demoed for our robot club using IPGamePad to control it.

https://github.com/jronald/PiBits

Also, you really don't want power that servos via the RPI but instead use a separate power supply. Of course you need to tie that grounds together. Since the RPI outputs to the servo no voltage shifting is required, but you will want to be very careful to not wire things incorrectly.

share|improve this answer

There is an extension board available for the raspberry pi called the gertboard that is used to control servos and other loads that the Pi can't drive by itself.

share|improve this answer
    
I should also add that due to the popularity of this product I believe it is sold out at most sites that offer the raspberry pi and accessories. –  Brandon Bailey Oct 28 '12 at 15:58

I'd be surprised if you can time the outputs sufficiently well whilst running Linux to accurately control a servo.

The pulse has to be controlled over a range of 2 milliseconds. Even if the scheduler tick has a resolution of 100us (which is pushing it - it's usually 10ms), that's only 20 steps you can resolve. And that's without considering the jitter introduced by task switching.

If there's another timer available, you could potentially write a very low-level driver which can pre-empt even the OS to get it's timing accurate. Sounds like fun!

share|improve this answer

I think that trying to run a servo directly from the GPIO port would be difficult and cause a lot of CPU overhead if it's even possible. I use a USB servo controller and it works great and uses almost no CPU, although it is a little tricky to set up. The one I have is 24 channels, but they do make a 6 channel version. I have also seen 2 and 4 channel controllers from other vendors that use serial which would be easier and cheaper than USB if you do not need to control so many servos.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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