raspberry pi

Raspberry Pi Servo Motor for camera controlling

how to control servo motor with Raspberry Pi Python programming

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

WebCam:

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:

TOP10 Best Raspberry Pi Kits

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:

#! / usr / bin / python3
# Servo .py file
import spidev
import time
import sys
import pigpio
mot = sys. argv [1] # 3.4
dir = sys. argv [2] # +100 / -100
mot1 = 3
mot2 = 4
pi = pigpio .pi () # connect to local Pi
pi. set_mode (mot1, pigpio. OUTPUT)
pi. set_mode (mot2, pigpio. OUTPUT)
if dir == "home":
pi. set_servo_pulsewidth (int (mot), 1500)
f = open ("mot" + mot + ". txt", "w")
f. write ("1500")
f. close ()
else:
f = open ("mot" + mot + ". txt", "r")
lastPos = f. read ()
print (lastPos)
f. close ()
newPos = int (lastPos) + int (dir)
if newPos> = 2000 or newPos <= 1000:
print ("end position reached!")
print (newPos)
sys. exit ()
else:
pi. set_servo_pulsewidth (int (mot), int (newPos))
f = open ("mot" + mot + ". txt", "w")
f. write (str (newPos))
f. close ()

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.

sudo apt - get install motion

After the package has been installed, you will find the file in the etc / motion / folder

motion.conf. Open it e.g. with nano:

sudo nano / etc / motion / motion.conf

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:

# Image width (pixels).
# Valid range: Camera dependent, default: 352
width 320
# Image height (pixels).
# Valid range: Camera dependent, default: 288
height 240

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:

# Let motion regulate the brightness of a video device
# (default: off). The auto_brightness feature uses the brightness
# option as its target value. If brightness is zero,
# auto_brightness will adjust to average brightness value 128.
# Only recommended for cameras without auto brightness
auto_brightness 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:

# The mini - http server lists to this port for requests

# (default: 0 = disabled)

webcam_port 8081

You will find the following two parameters from line 409. Change them as follows:

# Maximum framerate for webcam streams (default: 1)

webcam_maxrate 15

# Restrict webcam connections to localhost only (default: on)

webcam_localhost off

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:

<table width = "300" border = "0" align = "center">

<tr>

<td> <img src = "http: // IP of the Raspberry Pi: 8081"

width = "320" height = "240" align = "center"> </ td>

</ tr>

</ table>

<div align = "center">

<? php

// Evaluation of the get parameters

if (isset ($ _GET ['horizontal'])) {

$ command1 = $ _GET ['horizontal'];

$ val = shell_exec ("python servo.py 4 $ command1");

}

elseif (isset ($ _GET ['vertical'])) {

$ command2 = $ _GET ['vertical'];

$ val = shell_exec ("python servo.py 3 $ command2");

}

?>

<div align = "center">

<a href =" <? php print ($_SERVER ['PHP_SELF ']); ?>? vertical = 100 "

class = "knopf_off" style = "width: 100 px"> high </a>

<br>

<a href =" <? php print ($_SERVER ['PHP_SELF']); ?>? vertical = home "

class = "knopf_on" style = "width: 100 px"> vertical center </a>

<br>

<a href =" <? php print ($_SERVER ['PHP_SELF ']); ?>? vertical = -100 "

class = "knopf_on" style = "width: 100 px"> Down </a>

<br>

---------------

<br>

<a href =" <? php print ($_SERVER ['PHP_SELF']); ?>? horizontal = -100 "

class = "knopf_on" style = "width: 100 px"> Links </a>

<br>

<a href =" <? php print ($_SERVER ['PHP_SELF']); ?>? horizontal = home "

class = "knopf_on" style = "width: 100 px"> Horizontal middle </a>

<br>

<a href =" <? php print ($_SERVER ['PHP_SELF']); ?>? horizontal = 100 "

class = "knopf_off" style = "width: 100 px"> Right </a>

</ div>

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:

sudo touch mot3.txt

sudo touch mot4.txt

sudo chmod 666 mot *. txt

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

servo.py

mot3.txt

mot4.txt

servocam.php

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.

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

Your email address will not be published. Required fields are marked *

Back to top button