Why I want to control a servo motor with the Raspberry Pi is explained below. I want to install an ultrasonic sensor on the front and back of my robot car. My problem is that the sensors have an opening angle of 15 degrees. To get a better overview what is going on in front or behind the robot car I want to turn the ultrasonic sensors a little bit to the left and to the right. The idea is to be able to turn the ultrasonic sensors about 35 degrees to the left and right. To be able to turn the sensors I need a servo motor like this, which is also used in model making. I decided to use small 9g micro servo motors. They are cheap and fit well into my Raspberry Pi robot.
The picture shows my robot car from the front with a servo motor and attached ultrasonic sensor.
The components I used in this article to connect the servo motors to a Raspberry Pi are the following
I used the step-down converters to supply the Raspberry Pi with +5V and to supply the servo motors with +5V. Unfortunately it is not possible to connect the servo motors directly to the Raspberry Pi. In this case the Raspberry Pi does not supply enough current and would surely suffer damage.
The components needed to build the robot car can be found as an overview under the following link: Raspberry Pi Roboter-Auto Komponentenübersicht
Introduction Servo Motors
There is a wide range of servo motors for different applications. But they all have one thing in common: they have three cables for connection. One for the +5V, one for the ground GND and one for the PWM signal. Depending on the PWM signal the servo moves from one position to the other. It is very easy to control a servo with the Raspberry Pi. You just have to connect the servo to an external power source (+5V / GND) and normally the yellow cable of the servo motor has to be connected to the GPIO pin of the Raspberry Pi which is configured aks OUT / PWM. Please check the manufacturer’s specifications of the servomotor which cable is for what. Furthermore the external power source and the Raspberry Pi must have a common ground. Otherwise the servo motor will probably not move correctly.
The following picture shows two different servo motors. Both can be connected with the Raspberry Pi to the PWM out pin. The power supply of the servomotor must be provided by an external power source.
Connect the servo motor to the Raspberry Pi
To use the program described in the next chapter the servo must be connected to the Raspberry Pi as follows
- GPIO 12: This pin generates the PWM signal for the first servo motor. The yellow cable is connected here
- GPIO 18: This pin generates the PWM signal for the second servo motor. The yellow cable is connected here
- The red cable is connected to the +5V of the external power source.
- The black cable is connected to the ground (GND) of the external power source
- Please note that the grounding of the external power source must be connected to the grounding of the Raspberry Pi’s. Otherwise the servomotor may not move properly.
Servo motor – Python program
I wrote a small program in Python to control the two servo motors of the robot car. It is just an example which explains how to control servo motors and how they move from left to center to right and back again. The program can be adapted to individual needs.
The program can be downloaded here: servo motor python test program
Related articles – Servo controller
But if you want to control several servo motors with one Raspberry Pi, and you want to do this with high precision, you need a servo controller. In the following articles I will present a servo controller that is inexpensive and works perfectly with the Raspberry Pi.
- 16-Kanal PCA9685 servo controller – Part 1 Introduction and Setup
- 16-Kanal PCA9685 servo controller – Part 2 Software Installation and Test
The video shows my current robot car as a prototype which is equipped with two servo motors and two ultrasonic sensors.