Raspberry Pi Servo Motor control

In addition to stepper motors, there are also small and cheap servo motors. The control of Raspberry Pi servo motors is very simple and thanks to the small size and weight they are used in many areas.
Unlike stepper motors, servomotors can be controlled with a single GPIO.

In this tutorial, I’ll show an example of how to use Python to control one or more servo motors.

Required Hardware Parts

Of course, it is possible to supply the servo motor with an external power source, but this only makes sense when using several motors. For that, rechargeable batteries/regular batteries would still be needed.



In most cases, the colors of the servo are as follows and are connected to the Pi:

Black – comes to GND (pin 6) from the Pi
Red – comes to 3V3 (pin 1) from the Pi
Yellow/Orange – to a free GPIO pin (e.g., GPIO17, pin 11)

Raspberry Pi Servo Motor Anschluss

If you want to play it safe, you can set a ~ 1kΩ resistor between the data pin (yellow/orange) and the Pi. Normally this is not necessary.

If the servo motor does not rotate correctly, this may also influence the power supply of the Raspberry Pi (just look at the datasheet, what the engine consumes). In such a case an external power source makes sense (usually it is 4 to 6V).


Software for controlling the Raspberry Pi servo motors

Unlike stepper motors, servo motors don’t occupy many GPIO pins to command a movement. For this, the rotation is controlled by the length of the pulse.

The angle of the motor is set along the length of the pulse, so PWM is particularly useful, which sends repetitive signals at even intervals (the Raspberry Pi Python library must be installed).

We either start python (sudo python) or open a new script (sudo nano servomotor.py) with the following content:

import RPi.GPIO as GPIO
import time

servoPIN = 17
GPIO.setup(servoPIN, GPIO.OUT)

p = GPIO.PWM(servoPIN, 50) # GPIO 17 for PWM with 50Hz
p.start(2.5) # Initialization
  while True:
except KeyboardInterrupt:

If the servo motor shakes a bit while it is not moving, you can pause the pulse with ChangeDutyCycle(0)

For servo motors and their driver boards, the indication of the period and the duty cycle is usually made in the datasheet. In the example of a MG996R we have the following: The period is 20ms long. In addition, a duty cycle of between 5% and 10% of the period is expected. Thus the pulse duration is between 1ms and 2ms. Usually, servos can rotate between 0 and 180°. So we have to adjust the pulse length in between. A length of 1.5ms (7.5%) thus gives an angle of 90 °. And all at precisely 50x per second (50Hz).


If it bothers you that the servo (SG90) can not rotate 360 °, there is a “hack“.

32 Responses

  1. Very nicely presented, You site is the first one I have seen that puts links to show us newbies details, most sites just expect the readers know and are good at Unix based systems like Rasbian.

    What version of Python are these programs written in? i’m not familiar with the brackets but i’m learning.
    (i’m 82 yrs old and having great fun with all this Raspberry pi 3 B stuff. But the Picar V software still has be on a steep upward slope.
    Thanks again
    Dan G

  2. How do we power the servo without the pi losing significant power that it shuts down.
    I am a noob so I really don’t understand much of how rpis and servos interact but I do obviously know how a servo works and all.

  3. Excellent Training – I have more to learn.
    Just on question – When connecting the Servo to an externa l 4.5 volt battery supply, the servo does not work??
    Does the signal need to referenced to the Pi Ground signal??

    • Hi Tim, most servos should work fine with 4.5v but to be sure 5v is the standard voltage of a servo. And make sure to ground the servo with raspberrypi with external power supply. One more thing, make sure to power the positive wire seperately with external power supply while grounding it with raspberrypi.
      Hope this helps.


  4. So can the servo motor rotate in both directions at the same time like those in a stepper motor?

  5. Pingback: you can look here

  6. Pingback: https://eatverts.com/

  7. Pingback: w88.com

  8. Pingback: mindset

  9. Pingback: video transitions on inshot

  10. Pingback: choi number game

  11. Pingback: số đề con heo

  12. Pingback: purchase hydrocodone tablets no prescription overnight cheap

  13. Pingback: mơ thấy con rết

  14. Pingback: buy roxicodone 15 for sale online cheap overnight

  15. Pingback: kinh nghiem soi keo bong da

  16. Pingback: buy oxycodone 15mg for sale with no prescription overnight

  17. Pingback: mơ có người chết đánh con gì

  18. Pingback: purchase subutex online for sale near me no prescription overnight delivery cheap

  19. Pingback: order cialis online in usa canada uk australia without prescription nextday shipping

  20. Pingback: danh de online uy tin

  21. Pingback: Women's Loungewear

  22. Pingback: bitcoin era review 2020

  23. Pingback: realcouchtuner.com

  24. You should use the 5V pin instead the 3.3V pin to power the servo (red). (Really you should power it externally, but if you’re using the raspberry as the powersource then the 5V is the correct way to do it.)

  25. Why does each pixel depend on the contents of the stack from the last pixel? That makes Leibniz extremely difficult/impossible to speed up by parallelizing it or converting the kernel to a GPU shader. Only the Leibniz program depended only on the stack containing x,y,t, and nothing else.
    Adderall Pills for Sale


Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!