Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Building an Enhanced Security System with a Web Cam and a Servo

Once you crack the surface of what's possible using .NET to control hardware devices you may find yourself quickly sucked in to this kind of programming. In this article, learn to control a Web cam mounted on a servo to create a sophisticated monitoring application.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Date: 1/31/2018 @ 2 p.m. ET

n my previous article: "Teach Your Old Web Cam New Tricks: Use Video Captures in Your .NET Applications," I showed you how to integrate your Webcam into your Windows application. And in my last couple of articles on DevX.com, I showed you a few ways to control external electronic devices from within your .NET applications. And so, equipped with this knowledge, I am next going to show you how you can build an enhanced security system using a Web cam mounted on a servo, which can be controlled to turn the Web cam in any direction you desire. The system I am building in this article can be deployed at home or at the office to monitor the surroundings.

In this application, you will:

  • learn how to control a servo using a microcontroller
  • learn to program a microcontroller using the PBASIC language
  • learn how to interface a .NET Windows application with a microcontroller so that it can control how the servo turns

What is a Servo?
A servo is basically a motor, except that it allows you to position the output shaft at any specific position you desire. Servos are very useful in robotics and are often found in toys such as remote controlled cars, airplanes, robots, etc.

There are two main types of servos you can use for this project—a standard servo or a continuous rotation servo. A standard servo only allows a limited angle of rotation, while a continuous rotation servo allows the servo to rotate continuously in either direction. For this article I chose a standard servo from vendor Parallax.

Connecting the Servo
Like the two sensors I discussed in my last article, the servo I used cannot be directly connected to the serial port of the PC. Hence, you need a microcontroller to serve as an adapter between the servo and the serial port of your PC. For this purpose, I used the BASIC Stamp 2 (BS2) Module ($49), also from Parallax. The BS2 is a microcontroller that runs at 20MHz and executes roughly 4000 instructions per second. You also need a board to house the BS2 module. I used Parallax's USB Board of Education (BoE) Development Board ($65). You will control the servo directly by writing code that runs on the BS2. And in this case, the language for programming the BS2 is PBASIC (Parallax's version of the BASIC programming language).

To connect the servo to the BoE, use three jumper wires and connect as follows (see Figure 1):

  • Connect the black cable (Ground) to the connector labeled Vss.
  • Connect the red cable (5V) to the connector labeled Vdd.
  • Connect the white cable (signal line) to the connector labeled P15.

Figure 1. Connec the servo to the BoE.
Figure 2. Mount your Web cam on top of the servo.

Author's Note: Please refer to my previous article: “Teach Your Old Web Cam New Tricks: Use Video Captures in Your .NET Applications” for more information on how to connect the BoE to the computer.

Not surprisingly, you also need to mount a Web cam on the horns of the servo. Figure 2 shows my Web cam mounted on the Parallax Standard Servo.

Understanding How Servo Works
A servo is controlled by pulsing its signal line. Pulsing means toggling the voltage of a device by setting it to high for a certain amount of time before setting it to low again. A standard servo can rotate both in the clockwise direction as well as in the anti-clockwise direction and my servo is capable of 180 degree rotation.

The duration of the pulse determines the direction that the servo turns. If the duration of the high pulse (also called the pulse width, see Figure 3) is about 1.5ms, the servo will stay in the middle of the 180 degrees rotation, that is, in the 90 degree position (also known as the neutral position). If it receives a pulse width of less than 1.5ms (such as 1.3ms), then it will turn slightly clockwise. Likewise, it turns slightly anti-clockwise if it receives a pulse width of more than 1.5ms (such as 2ms).

Figure 3. The length of the high point in the pulse train determines the direction of the servo's rotation.
In order to use the BS2 to control the servo, the PBASIC language supports the PULSOUT function, which generates a pulse. The PULSOUT function has the following syntax:

PULSOUT Pin, Duration

The Pin parameter specifies the pin of the BS2 chip that is connected to the signal line of the servo. The Duration parameter specifies the pulse width. The pulse width is in multiples of 2µs (micro-seconds). Hence, to send a pulse width of 1.5ms (milliseconds) to pin 15, you would need the following arguments:

PULSOUT 15, 750

To get 1.5ms, you multiply 750 by 2µs (two micro-seconds), which is 750 x 2 x 10-6. This gives a total of 1.5ms (milliseconds). To turn the servo clockwise, you can issue something like the following:

PULSOUT 15, 550

To turn it anti-clockwise, the argument looks like this:

PULSOUT 15, 850

The "clockwise-most" duration is 190 and the "anticlockwise-most" position is 1180. So, to turn the web cam as far as possible to the right you would issue:

PULSOUT 15, 190

Likewise, to turn it all the way in the other direction, issue the arguments like this:

PULSOUT 15, 1180

Author's Note: Actually, in real-life testing, the servo that I have uses a pulse duration of 685 (and not 750) for the neutral position. For discussion purposes, I am going to assume that 750 is the pulse duration for positioning the servo at the center. Check out your own servo for the pulse duration at the neutral position.

One important thing to note is that when you issue a single command like "PULSOUT 15, 190", the servo does not turn all the way to the final position. Depending on where the current position of the servo is, it may take a few pulses to get to the final position. To do so, repeat the command in PBASIC using an infinite loop, like this:

Start: PULSOUT 15, 190 PAUSE 20 GOTO Start

Here, the PAUSE command simply inserts a delay of 20ms, between the pulses. This delay will affect how smoothly the servo turns. The smaller the delay, the smoother the servo turns. If the delay is too long, then the servo tends to jerk. However, the lower the delay, the more pulses are needed to turn the servo to its intended position. Typically, for a delay of 10ms (the lowest recommended), it does not take more than 40 pulses to move the servo from one end to the other. According to the specification of the Parallax Standard Servo, a pulse delay of 10 to 40ms works well. But the number of pulses needed to get the servo to reach its final destination is dependent on the pulse delay as well as its current position.

To summarize, here are two important points you need to know:

  • The pulse duration indicates the direction you want the servo to turn
  • The pulse delay affects how smoothly the servo turns

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date