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
1.2GHz Quad-Core, Broadcom BCM2836, 1GB Speicher, VideoCore, LAN, HDMI, 4xUSB 2.0,microSDHC-Cardreader, Bluetooth, W-LAN

CPU-Typen: 1.2GHz Quad-Core 64bit
Chipsatz: Broadcom BCM2837
Externe Anschlüsse: 4x USB 2.0, LAN, HDMI, microSD/SDHC/SDXC-Cardreader, 40pin GPIO, Audio 3.5mm
Netzwerk: 10/100Mbit Fast Ethernet
Grafik:
SODIAL(R) bis 3A Step-Down Buck KIS3R33S Modul 5V 5V USB-Ausgang DC Converter 7V-24V
Preis: --
0 neu0 gebraucht
* SODIAL ist ein eingetragenes Markenzeichen. Nur SODIAL autorisierte Verkaeufer duerfen unter SODIAL-Listing verkaufen.
SODIAL(R) bis 3A Step-Down Buck KIS3R33S Modul 5V 5V USB-Ausgang DC Converter 7V-24V
Beschreibung:
Umwandlungswirkungsgrad: 96% (MAX).
Schaltfrequenz: 340KHz.
Restwelligkeit: 30mV (max).
Legen Regelung: ?? 0,5%.
Voltage Regelung: ?? 2,5%.
Operating Temperatur: -40 ?? C bis +85 ?? C.
With berstromschutz.
IN-Modul oben (Eingang negativ) angedeutet, IN + (positive Eingang), OUT-(negativer Ausgang), OUT + (positiver Ausgang) muss rechts angeschlossen werden, da sonst Sch?den am Modul hren.
Dieses Modul verfgt ber Kurzschluss-Schutz, automatische 5A Stromschutz .
Lieferumfang:1PCS 7 V DC-24V bis 3A Step-Down Buck KIS3R33S Modul 5V
Hinweis: Wegen der Lichtstrahlung und Bildschirme koennte der Farbeton in den Fotos sowie des tatsaechlichen Produkts etwas unterschiedlich sein. Der erlaubte Messfehler ist 1-3cm.
Raspberry Pi NoIR Camera Module V2 - 8MP 1080P30
Price: $25.00
12 new from $22.610 used
The Raspberry Pi Camera Module v2 is a high quality 8 megapixel Sony IMX219 image sensor custom designed add-on board for Raspberry Pi, featuring a fixed focus lens.
Microsoft LifeCam HD-3000 Webcam - Black (T3H-00011), 720p HD 16:9 Video Chat, Skype Certified
Price: $29.12
89 new from $19.314 used from $21.41
Your video is worth 1,000 texts - Your picture is worth 1,000 texts. This true HD camera gives you bright and colorful video, and it’s so easy to post that your parents could do it.
RB-65PG Pan And Tilt Kit With Short "U" Servo Bracket (Silver)/The Advantages Of Reliable Operation, Strong Adaptive Ability And Low Electromagnetic Interference.
Price: $39.88
1 new from $39.880 used
2-DOF RB-65PG pan and tilt kit is a cost-effective, positioning-precise and lightweight kit which can be used both inside and outside. It features moving vertically and horizontally. Not only can the attached camera record images, but recognize images and implement positioning tracking. With an IR sensor or Ultrasonic sensor, it forms an all-in-one detecting device to be able to sense the change of ambient surroundings by which the robot can detect and avoid obstacles. By the Arduino controller, a color searchlight can also be added to complete a new work.



Packing List:

2 x RB-65PG Servo Motor
1 x Alluminium Alloy Multi-Purpose Servo Bracket
1 x Aluminum Offset Servo Bracket
1 x Bearing
1 x Bearing Spring Washer and Screw
1 x Screw and Nut Kit
J-deal® Pyroelectric Infrared PIR Motion Sensor Detector Module Hc-sr501 (5pcs)
Price: $11.00
You save: $0.49 (4 %)
2 new from $6.590 used

PCB Dimensions: Approx. 32 x 24mm
Screw hole distance 28mm, screw aperture 2mm
Sensor lens size: (diameter): 23mm (default)
Operating voltage: DC5V to 20V
Static power consumption: 65 microamps
Level output: 3.3V, low 0V
Delay time: adjustable (0.3 seconds to 18 seconds)
Lumen: 350-550Lm (approx.)Blockade Time: 0.2 seconds
Trigger Mode: L can not be repeated, H can be repeated default value H
Trigger Mode: L can not be repeated, H can be repeated default value H
Spotlight no shadow
Energy saving and environmental friendly, high quality, effective and low heating, long service life
Suitable for office, dance hall, bar, exhibition or home useSensing range: less than 120 degree cone angle of less than 7 meters
Operating temperature: -15 to +70 degrees

(Visited 66 times, 1 visits today)