Raspberry Pi – controlling servo motors

Feb 11th, 2016 | By | Category: Robot cars, Top story

This post is about controlling two servo motors with a Raspberry Pi. You can buy cheap pan & tilt kits or build one by yourself. I bought a pan & tilt kit for my robot-cars and build one kit by myself with two servo motors and a cardboard tube. To build the security-robot I used additional components like a Raspberry Pi 3, step-down converter, IR sensors and a level shifter plus wires. The list below shows all the electronic components I used to build my security-robot. But this post is not about the robot itself this post is about controlling a servo motor with a Raspberry Pi and a Python program.

The picture shows my security robot with the self-build pan & tilt kit with two servo motors in the background controlled by a Python script I developed for controlling the servo motors.

custom build security robot - Raspberry Pi Pan&Tilt kit

custom build security robot – Raspberry Pi Pan  &Tilt kit

Setup servo motors

Many servo motors are available. But all in common have three wires. One for the +5V, one for the GND and one for the PWM signal. Depending on the PWM signal the servo is moving from one to the other position. It is very easy to control a servo motor with a Raspberry Pi. You only have to connect the servo motor to an external power supply like a step-down converter (5V+ / GND). The yellow wire of the servo motor you have to connect with a GPIO pin which is configured as OUT / PWM (pulse width modulation). Please check the manufactory description of your servo motor which wire is for what. The external power supply and the Raspberry Pi should have a common ground (GND). I always use a step-down converter which has a USB-plug and on the back of the converter I solder two wires for +5 V and GND. Otherwise the servo may not move in a correct way if the servo motor and the Raspberry Pi do not have a common GND.

The figure below shows two servo motors I normally use to build a robot-arm. Both could be connected to the Raspberry Pi PWM out pin to control the movement of the motor.

Servo motors

Servo motors

Connect your servo motor to the Raspberry Pi

To use the program described in the next chapter you have to connect the two servo motors to your Raspberry Pi as follows:

  • GPIO 12: This is the PWM signal for the first servo and is connected to the third cable of your server (yellow).
  • GPIO 18: This is the PWM signal for the second servo and is connected to the third cable of your server (yellow).
  • Red cable: The red cable of your servo is connected with the +5V of your external power supply.
  • Black cable: The black cable of your servo is connected with the GND (ground) of your external power supply.
    • Have in mind that you have to connect the GND of your Raspberry Pi together with the GND of your external power supply. Otherwise the servo may not move in a correct way.

Servo Motor – Python program

I wrote a small program to control the two servos I used to build the pan & tilt kit. It is just an example which explains how to control servo motors and is only moving the servos. Feel free to change the program accordingly to your needs.

The program is available here for free download: servo motor python test program

security-robot setup

security-robot setup

Video

The video shows my security-robot I build with two servo motors and four IR sensors.

Security-Robot – component list

The electronic components I used to build my security-robot are listed below:

Raspberry Pi 3 Model B
Preis: EUR 44,00
54 neu von EUR 40,060 gebraucht
(Visited 4,001 times, 5 visits today)
Tags: , , ,

One Comment to “Raspberry Pi – controlling servo motors”

  1. Ralf says:

    Gibt es ein Programm für den
    security robot

    MfG

Leave a Comment

*