Raspberry Pi LESSON 28: Controlling a Servo on Raspberry Pi with Python

In this lesson we will show you how to precisely control a Servo using the Raspberry Pi. First, for the small servo I am using, I have verified that it is safe to drive from the 5 volt pin (physical pin 2) on the Raspberry Pi. It is possible to damage your Raspberry Pi by drawing too much current out of a pin. So, if you are not sure about the current requirements of your Servo, it is best to power it from a 5 Volt source other than a Raspberry Pi pin. You can still control it from the Raspberry Pi if you use a common ground, but just get the power (red wire) from an external source. For my small servo, I can safely power it from Raspberry Pi physical pin 2.

The second point is that to control the servo, you have to use Pulse Width Modulation. So, I STRONGLY recommend that you go through LESSON 27 if you have not already. Lesson 27 shows you how to use PWM on the GPIO pins on the Raspberry Pi. If you are up to speed on PWM, this lesson will go a lot easier.

So, with that out of the way, we are ready to hook up our servo. For my servo, the ground wire is Black, the Red wire is 5 volt, and the yellow wire is the control line. If you are using a different servo, you will need to read the instructions to see what the color code is for your three wires, but it is likely similar to mine. The sketch below shows how you should hook the servo up. Notice I have the 5V hooked to the Pi physical pin 2, the servo ground hooked to the Pi physical pin 9, and the servo control line hooked to the Pi physical pin 11.

Raspberry Pi Servo
Servo Connected to Raspberry Pi GPIO Pins

Now with the Servo hooked up, we are ready to try and control it. For this example, we will work in the Python shell. To enter the python shell, open a Terminal window, and at the command prompt type:

$sudo python

It is important to include sudo, as the Raspberry Pi only allows access to the GPIO pins to the superuser. Hence, you need to enter the python shell as a superuser. When you type the command above, you should be moved into the Python shell, and should see the python shell prompt of >>>.

We are now ready to control the servo. We must first import the RPi library. These first steps should be familiar if you did LESSON 27.

>>>import RPi.GPIO as GPIO

Now we need to tell the Pi what pin numbering scheme we want to use. I like to use the physical pin numbers (See LESSON 25 for a diagram). So, we need to issue the command:


Now we need to tell the Pi that physical pin 11 will be an output pin:


The servos position is controlled by the pulsewidth of a 50 Hz PWM signal. Hence, we need to turn the PWM sequence on at 50 Hz. Note that for a 50 Hz signal, the Period of the signal is 1/50=.02 seconds, or 20 milliseconds. Keep this Period in mind as we will come back to it later. We start by creating a PWM object on Pin 11 with a 50 Hz signal with the command:


We can now start the pwm sequence by giving a command to specify the DutyCycle of the signal. Before we do this, we need to talk a little bit about how servos work. A typical servo wants to see a frequency of 50 Hz on the control line. The position it moves to depends on the pulse width of the signal. Most servos behave roughly as such, but you will need to tweak these numbers for your particular servo. Typically, the servo will go to the full left position when it sees a pulse width of 1 millisecond, it will go the middle position when it sees a pulse width of 1.5 millisecond, and it will go to the full right position when it sees a pulse width of 2 millisecond. Note however, that on the Raspberry Pi we do not specify a pulse width, but we specify a DutyCycle. So, we can use the following relationship:

DutyCycle =PulseWidth/Period

Remember that Period = 1/frequency, so:

DutyCycle = PulseWidth/(1/frequency) = PulseWidth * frequency

The PulseWidth that will give us a full left position is 1 milllisecond. We now calculate the applied DutyCycle to give us the desired position:

DutyCycle = PulseWidth*frequency=.001 *50 = .05 = 5%

So, for a 50 Hz signal, if we set the DutyCycle to 5, then we should see the servo move to the full left position. Similarly, if we set DutyCycle to 7.5, we should get the middle position, and if we set it to 10 we should be in the full right position. You can get all the intermediate positions by linearly scaling between 5 and 10. Note that these values will vary between brands, and between individual servos, so play around with your servo to get it calibrated. We are now ready to apply a command to position the servo. If we want the servo in the full left position, we should set the DutyCycle to 5%. We do that with the command:


This will start the PWM signal, and will set it at 5%. Remember, we already specified the 50 Hz signal when we created the pwm object in our earlier commands. Now if we want to change the position, we can change the DutyCycle. For example, if we want to go to the middle position, we want a DutyCycle of 7.5, which we can get with the command:


Now if we want the full right position, we want a duty cycle of 10, which we would get with the command:


Remember, it is not DutyCycle that actually controls servo position, it is PulseWidth. We are creating DutyCycles to give us the desired PulseWidth.

Now, play around with your particular servo and then find the specific DutyCycles that lead to full left and full right positions. For my servo, I find that full left is at DutyCycle=2, and full right is at DutyCycle=12. With these values, I can create a linear equation that will give me any angle I want between 0 and 180. This will make the Raspberry Pi behave much more like the simple and intuitive operation of the Arduino.

To do the linear equation I need two points. Well, I know that for a desired angle of 0, I should apply a DutyCycle of 2. This would be the point (0,2). Now I also know that for a desired angle of 180, I should apply a DutyCycle of 12. This would be the point (180,12). We now have two points and can calculate the equation of the line. (Remember, play with your servo . . . your numbers might be slightly different than mine, but the methodology below will work if you use your two points)

Remember slope of a line will be:

m=(y2-y1)/(x2-x1)=(12-2)/180-0)=10/180 = 1/18

We can now get the equation of the line using the point slope formula.



y = 1/18*x + 2

Putting in our actual variables, we get

DutyCycle = 1/18* (DesiredAngle) + 2

Now to change to that position, we simply use the command:


I hope this makes sense. Watch the video as I step you through it carefully. If the writeup above does not make sense, hopefully the video will clear things up.

35 thoughts on “Raspberry Pi LESSON 28: Controlling a Servo on Raspberry Pi with Python”

  1. Hello,

    Thanks you very much for this tutorial, it’s great !!

    But i have one question, after set the angle to 1, I want to set angle to 2 or up to 10, it isn’t move. I use towerpro sg90, I think it is not as accurate as your, Right ?

    Thanks you

    1. Make sure you do the math to find what values lead to your sevo hitting the end points. Your servo might be different, so you have to play with the numbers and the equation.

      1. Ok I see it. I must modifi this line my servo :


        I’ll try.

        Thanks you

  2. Ok I see it, i found the correct parameters 🙂
    So I have one more question.
    Did you know how to control the rotation speed of servo using python or another language ? I want speed up or slow rotation.

    Thank you

    1. You can slow down by putting time delays in the program. You can only go so fast, as the servos dont move very fast. Delays will slow things down effectively.

  3. can i do the same procedure to control dynamixel ax 12?
    or it requires breadboard and uln2803? if not, could you please show me how to do it.

    Thank you

  4. It was suggested elsewhere that most of the GPIO pins of the Pi can’t be made to have reliable enough timing for PWM, but that there is special hardware support for precise PWM timing on a special pin. Is your setup really reliable enough?

    1. These are lessons for high school students to introduce them to PWM concept using the Pi. They are not meant to be reliable, but meant to illustrate a concept to first time programmers.

  5. Paul greetings from Mexico Hello !!!! Thank you very much for sharing your knowledge, I have seen your lessons raspberry pi to the number 29 and I liked very much, especially because you explain in great detail and also because my native language is not English and I could understand all right =)

    In particular I have a doubt in Lesson 28 about servo, and it is not clear to me this line

    for i in range (180,0, -1)

    I do not understand the working fine and I think the servo does not reach its pocision 0° when it returns from de 180°, although when I test for command line and it arrives ok to the pocision 0° and 180°

    Could you explain?

  6. Hello,

    thank you so much for this tutorial, it helped me so much, worked almost perfect, I had just a problem with the servos, I don’t know what I did wrong but, when I do a loop for the servo go from 0 to 180 degrees, depending on the speed the servos tremble a lot, if increase the servo speed, he tremble a little less. I used the pwm in 50hz as you said, I have a 6v font to power all my servos, do you have any idea what can I do to solve this problem?


  7. Hi there.
    How can I solve the jitter? Are there any good solutions for this?
    Greetz from Holland.

    1. There are some low quality servos that have jitter. Make sure you have a good servo. Next, make sure you have adequate power for the servo . . . I would not try and power it from the R. PI pins.

      1. can i control servo motor using motion sensor and raspberry pi 3.that can detect motion the duty cycle of servo can change by the motion

  8. Im having a bit of a problem, I ran through your video doing exactly as you did but when I executed the line pwm.start(5) it restarts the Pi.
    What am I doing wrong?

  9. I have recently bought a raspberry pi car that uses a servo to turn and I haven’t been able to get it working. I can get it moving backwards and forwards but not turn. It uses servoblaster which I don’t properly understand, I have a list of instruction that came with the car but when I run the program I doesn’t turn. You couldn’t help could you?

  10. Hi
    can anyone tell me how to program the servo motor in” Matlab” language…
    Thanks in advance 🙂

  11. I’m trying to run servos AND a servo input motor controller from the PI. I’ve used Arduino for this and LOVE the servo library. Since you also do Arduino, I’m sure you’ve seen this. Does the PI have an equivalent for the servo library? I have been looking at all of the examples for servo control (Servo Blaster, etc) and it seems really complicated… plus I can’t get them to work properly..

    Suggestions for the simplest SERVO control scheme that will drive my Sabertooth 2×12 controller and a standard servo?

  12. if I use my servo the same HS-422 hitec my screen starts doing weird stuff and my keyboard goes from azerty to qwerty any solution’s for it?

    1. Try powering servo NOT from pi. Just connect control line to pi, and power from external source. Be sure to have common grounds.

  13. Dear Tech Boy!

    I love your lessons! As a software engineer by heart and profession I have no problems to understand programming issues. But I’m a beginner at electronic engineering, so my question is: why can I calibrate my TowerPro SG-90 with the pulse width different to 1-2 ms as by specification? I thought it should work according to spec. I have RPi 3B and SG-90 and I cannot reach full 180 degree by using 1-2 ms. I need to go down to 3,3% and up to 12,5% which exceeds the spec.

    I have checked on the oscilloscope and the pulse width from RPi is perfectly fine.

  14. hi
    great lresson!
    i wan to know can i rotate the servo for a angle more than 180 .
    i mean from extre left positon 0 degree i want to go 200 degree
    will this work??
    reply asap


  15. Please regard the file to lesson 27 of Raspberry PI With Linux Lessons.

    I’m not able to run this file.

    Congratulations with the very good job done so far with all lessons to Arduino, Python, Raspberry and Beaglebone Black.

    I have learn a lot with your lessons.

  16. You state ” I have verified that it is safe to drive from the 5 volt pin (physical pin 2) on the Raspberry Pi.” I have been able to find the current draw of the servo that I have, but have not been able to find a document about specification of physical pin 2 of the Raspberry Pi 3. Do you have a link to such a document?

  17. how to control servo motor using pir motion f detect he can move and he can go back f no motion detect raspberry pi board.

Comments are closed.