Raspberry Pi Servo Motor for camera controlling

(Last Updated On: April 1, 2021)

Raspberry Pi Servo Motor Camera:

Raspberry Pi Servo Motor- This project is based on Raspberry Pi, Servo Motors, and a camera for live image steaming on a web page. The two Servo motors are installed in a way that the camera movement can be controlled horizontally and also vertically.  So basically this is called as the Pan-Tilt module based on the Servo motors and Raspberry Pi.

Raspberry Pi Servo Motor
Figure 1: Webcam on a pan-tilt module

Prerequisites for the project are a so-called pan-tilt module and a Webcam (see Figure1). Backgrounds for operation and control of servomotors. The project is divided into five phases:

  • Hardware and wiring
  • Python software
  • Camera live stream
  • Web interface
  • Installation

The hardware:

The moving base of the camera is equipped with two servo motors and as a complete module for less than 20 EUR:

Amazon Purchase Links:

Pan Tilt Platform for Raspberry Pi Camera


Raspberry Pi

raspberry pi 4 4gb kit

Wireless Keyboard and Mouse for raspberry pi:

Night vision Camera for Raspberry Pi:

Oled HDMI touch display for raspberry pi:

Other Tools and Components:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

A small manual is enclosed with the module, as you have to screw the frame together yourself. However, this is not a major problem and is quick and easy. Once you’ve assembled the module, you’re ready to start connecting the servo motors. Because we are using two servo motors at the same time in this project want to use the webcam and also put a load on the motors we have a separate voltage source in the form of a battery compartment (see Figure2). Thus we ensure that the fuses of the Raspberry Pi when operating the motors not be overloaded.

Raspberry Pi Servo Motor
Figure 2: Connection diagram of the motors

The Python software:

This section deals with the Python software that provides the interface between the later web interface and the Servo motors. The code we create reads as follows:

Since the program contains some functions that are not immediately apparent, we would like to explain the code a little. After you have the code under the name servo.py the file is called with the transfer of two parameters. This is the information about which engine is addressed should be (variable mot), as well as the pulse width or the direction (Variable dir). A call to the program in which the motor on BCM pin 3 moves a little to the right turns, is done like this:

python servo .py 3 100

After completion of the project, the motor should be clicked on the web interface be moved a little further in the desired direction on one of the links. To However, it is necessary that the current position remains saved. Become this files mot3.txt and mot4.txt are created. After a change of position there will be the current position is saved in μs. This is done in the functions f.open () and f.write (). The last If query checks whether the motors move to the end position by moving again. These are at 1000 μs and 2000 μs defined. If so, the motor will not move. However, the complete system only becomes really apparent when you use the web interface have set up. Before doing this, however, it is necessary to watch the live stream of the webcam so that you can follow the image of the camera live in the browser later. We use the program motion for this.

The streaming software

A standard USB webcam provides us with the live image. Just close this to a free USB port. You can use ls / dev to check that the webcam detects and is listed as video0. motion was originally intended as software for motion detection. In the standard configuration detects motion in the image and saves images when there is movement from. In addition, motion offers a very simple and reliable live stream and numerous setting options. Since we have no movement in this project we will switch off the function later. Now install motion per apt-get.

Here we now make the necessary settings.

Resize the live stream image to a small resolution for a smooth Image. From line 72 you will find the parameters for the aspect ratio. Adjust this to 320×240 pixels:

You can also try other resolutions that your webcam supports. To however, large aspect ratios can lead to playback errors.

You should allow motion to automatically adjust the brightness values ​​of the camera image.

Change the corresponding parameter in line 112 back to on:

In the next step, deactivate the automatic image capture when motion is detected. You can find this in line 227. Change the value from on to off:

output_normal off

The next change is to enable the live stream on port 8081. Change change the value in line 400 to 8081:

With these changes, you increase the refresh rate of the live stream to 15 Images per second and allow access to the live image also from external devices. The standard setting only allows access to the image from the Raspberry Pi itself. Now save the file and exit the editor. So that motion streams the camera image directly when the Raspberry Pi is booted, it is necessary to load the vonmotion daemon directly at startup. To do this, edit the file

/etc/default/motion. This consists of just two lines, the last of which is change to yes:

start_motion_daemon = yes

Save this file and close it again. Then a Restart the Raspberry Pi:

sudo reboot

The live image is now ready for use and can be displayed in the browser in the next step will.

Raspberry Pi Servo Motor interface with web:

In this section, we assume that you have set up an Apache server and PHP. In the folder /var/www/

Create the file servocam.php and fill it with the following code:

Now adapt the third line in this file by entering the home network Enter the IP address of your Raspberry Pi. As you learned at the beginning, this will previously created Python program started with the transfer of two parameters. This is done in this PHP file. The six generated links lead the script servo.py, each with the servo GPIO (3 or 4) and a value of 100 or -100. Finally move the servo.py into the directory of the Apache server

/ var / www /.

If you did everything correctly, you can now enter the IP address of the Raspberry Pi Enter in the browser of your PC or smartphone and see the web interface including live image.

Installation of Raspberry Pi Servo Motor:

In order to be able to use the movable webcam reliably, it still needs a short setup. First, create the two text files in the / var / www / folder, in which the current position is to be saved. In the same step We give the two files the necessary rights so that they can also be accessed via the browser can be changed:

Your / var / www / folder should now contain the four files for this project:





Now open a web browser and call up the newly created web interface. To do this, enter the following in the address line of your browser: http: //IP.des. Raspberry.Pi / servocam.php. You should see the six below the live image from the webcam See links to control the motors. Now press first – before you click any other link – on Vertical Center and then to horizontal center. This will make the home is sent to the script servo.py and the two text files are saved with the value the middle position is filled, namely 1500. From now on the system is ready to start and can be used in rotated in any direction. Anyone can use the two middle links Axis can be moved back to the center position from any position. In this project we set the pulse width to the range between 1000 and 2000 limited. It is quite conceivable that the end stops of the motors are outside this range. If necessary, adjust the servo.py for the Limit value query and determine the maximum values ​​for your structure.

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply