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?
Alex Chamberlain
  • 15,420
  • 14
  • 66
  • 113
Alex L
  • 7,585
  • 12
  • 42
  • 53
  • This is a similar question that could help get you started: http://raspberrypi.stackexchange.com/questions/27/libraries-for-interfacing-with-the-gpio – Andrew Larsson Jun 12 '12 at 22:31

9 Answers9


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.

Peter Mortensen
  • 2,014
  • 2
  • 15
  • 17
David Sykes
  • 1,435
  • 3
  • 19
  • 28
  • 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. –  Jun 13 '12 at 14:56
  • 2
    @Tibor: 20ms is the period of servo pulses, not the width http://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 08: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.

  • 81
  • 1
  • 1

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.

James Ronald
  • 51
  • 1
  • 2
  • fyi - the richartghrist repo seems to have been maintained while the jronald fork seems outdated and broken. Also, I recommend reading how to [ignore all but first two servo controls](https://github.com/richardghirst/PiBits/issues/29) and [Installing Servoblaster](https://github.com/richardghirst/PiBits/issues/24) – cwd Dec 15 '14 at 03:05

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

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)
  • 70,096
  • 5
  • 71
  • 105

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.

Alex Chamberlain
  • 15,420
  • 14
  • 66
  • 113

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!

Martin Thompson
  • 447
  • 3
  • 11

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.

Morgan Courbet
  • 3,703
  • 3
  • 21
  • 37
  • 121
  • 1

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.

  • 111
  • 1