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


  • 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?


  • 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?

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.

  • 2
    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. – user13 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.


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


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.


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.


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.

  • 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

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.


Knowledge of how to do things on the Pi has improved quite a lot in the years since this question was asked.

There are at least four ways of generating hardware timed PWM/Servo pulses on the Pi's gpios without using external hardware.

The use of kernel modules to do this is pretty much deprecated, The following userland solutions are available.

  • My own pigpio will generate Servo and general PWM pulses independently on any of gpios 0-31 (C, Python, socket, pipe).
  • Servoblaster will generate Servo and general PWM pulses independently on up to 21 gpios (pipe).
  • RPIO will generate Servo and general PWM pulses independently on any of gpios 0-31 (Python).
  • pi-blaster derived from Servoblaster (pipe)

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!


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.


There is a full working GPIO web service that you can write your own plugins for. It appears to already have support for controlling any number of servos. See their "Content" directory for a bunch of images as well for the board layout.


Looks to be in C# running Mono on the Pi.

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.