How to Control Servo Motors with a Raspberry Pi
2021-02-10 | By Maker.io Staff
Interfacing servo motors can be an essential task in many projects, like building a custom robot. There are several methods one can employ to interface servo motors with a Raspberry Pi. This article introduces two of them and explains how servo motors work. This knowledge should enable the reader to control servos with different microcontrollers or even a simple 555 timer-based circuit.
How do servo motors work?
Usually, servos have three inputs and two of them supply power to the motor located inside the plastic body. The third input controls how much the servo turns.
Usually, the input signal is pulse-width modulated (PWM) and its frequency doesn’t change over time. Instead, the servo motor rotates to a certain angle when the duty cycle of the PWM input signal changes. The servo used in this example can turn 180 degrees in total (90 degrees in each direction). When the duty cycle is 5% (a one-millisecond long high pulse), the servo rotates to its minimum position. If the duty cycle changes to 10% (which is a two-millisecond long high pulse), the servo rotates all the way to the right. Duty cycles in between correlate to different angles between -90° and +90°. However, these numbers might vary for different servos and between manufacturers. Therefore, it’s always necessary to consult the datasheet of the servo.
Note that the Raspberry Pi shouldn’t supply the voltage to the servo. Instead, employ an external power supply to drive the servo motor because the Raspberry Pi’s GPIO pins might be unable to supply enough power to make the motor turn at an acceptable rate.
How to control a servo motor with a Raspberry Pi?
To make a Raspberry Pi control a servo motor, connect the +5 V and GND lines of the servo to an external power supply and the remaining signal wire to any I/O pin of the Raspberry Pi.
Don’t forget to connect any GND pin of the Raspberry Pi to the ground of the power supply as well.
Schematic diagram available on Scheme-It.
As mentioned above, servos typically expect a PWM signal that tells them where to turn. Therefore, there are several methods to rotate a servo to a specific angle. One method is to use the gpiozero library that contains a Servo module. You can use pip to install the library on a Raspberry Pi. If the rpi.gpio package isn't installed yet, download it as well. Otherwise, the gpiozero library won’t work:
sudo apt-get install python3-rpi.gpio sudo pip3 install gpiozero
Once that’s done, it’s then possible to execute the following script:
from gpiozero import Servo from time import sleep servo = Servo(25) try: while True: servo.min() sleep(0.5) servo.mid() sleep(0.5) servo.max() sleep(0.5) except KeyboardInterrupt: print("Program stopped")
As you can see, this library allows you to quickly turn the servo to one of three predefined positions. This simple python script first imports the required libraries. Then, it defines that the servo is connected to pin 25 before starting an infinite loop in which the servo gets rotated to its minimum, mid, and maximum position. In between angle changes, the script waits for half a second.
The library can, however, also instruct the servo to turn to any position between the servo’s minimum and maximum value by directly setting the servo’s value to a number between -1 and 1. A value of negative one corresponds to the minimum position of the servo, and one represents the servo’s maximum rotation:
from gpiozero import Servo from time import sleep servo = Servo(25) val = -1 try: while True: servo.value = val sleep(0.1) val = val + 0.1 if val > 1: val = -1 except KeyboardInterrupt: print("Program stopped")
This simple script works similarly to the previous one. It, however, directly sets the servo’s value in each loop iteration instead of making the servo rotate to one of the three predefined positions.
Note that this library only represents one way of controlling a servo with a Raspberry Pi. Similarly, you could also utilize a different library, such as pigpio, and directly manipulate the PWM output signal that goes to the servo to make it turn to a specified position.
An alternative method for controlling many servos
Any of the Raspberry Pi’s numbered I/O pins are capable of controlling servos. However, if you want to control even more servos or your project requires the GPIO pins for other things, then it’s also possible to employ a servo controller board.
These modules usually communicate with a microcontroller or similar device over I2C, so it’s possible to control many servos utilizing only two GPIO pins of the Raspberry Pi. There are, however, many different types of modules that you can buy and they often work very differently. Therefore, you should consult the datasheet of a module to learn how to use it.
Summary
Unlike regular DC motors, servos typically cannot not rotate a full 360 degrees. Instead, they turn between a certain minimum and maximum angle. A pulse width modulated input signal with a constant frequency controls the rotation. The duty cycle of that signal directly corresponds to the angle of the motor. A servo rotates towards its maximum position when the duty cycle of the input signal increases. The Raspberry Pi can directly control many servos at once via software. Each numbered GPIO pin can manage a servo. It is, however, not recommended to power the servos with the Raspberry Pi. Instead, they should connect to an external power supply. Servo controller boards are a good alternative for controlling a large number of servos at once.
Recommended Reading
Servomotor Control with Limits and Variable Speed
How to put a servo to work in an Arduino environment – Maker.io Tutorial | DigiKey
Servo Motors – Tech Basics | DigiKey Electronics
How to Build a Face Tracking Pan Tilt Camera with OpenMV | DigiKey
![TechForum](http://www.digikey.com/-/media/Images/Footer/TechForum2.png?la=en&ts=9efa75a7-28fd-48d1-bcf2-3300f3edfa4e)
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum