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


Turn Your PC Into a Motion Sensing Security Device with .NET

Thanks to new types of hardware that can easily be programmed using .NET, you can create a motion sensing application that can be deployed for security—or just for fun.

n my inaugural article on how to integrate your .NET applications with external devices, I showed how to display system information using an LCD display. In this article, I will dive into the world of sensors. Sensors are interesting devices because they are the "eyes and ears" of your computer and can provide detailed information about the surroundings, such as temperature, lighting detection, proximity measurement, and so on.

For this article, I will be using two types of sensors: a passive infrared (PIR) sensor and an ultrasonic sensor.

The PIR Sensor from Parallax (see Figure 1) is a low-cost sensor ($7.95) that is able to detect motion by using the principle of infrared radiation. It can detect motion up to a range of 20 feet. Its connection is a simple three-pin connector—GND, 5VDC, and a signal line. When motion is detected, the signal line will register a 1, and will fall to 0 when the motion stops. It is ideal for detecting movements.

The second sensor, the PING))) Ultrasonic Sensor (see Figure 2), is much more interesting and it is able to provide precise measurement of distance ranging from 2 cm to 3 meters. It works by emitting a short ultrasonic burst and then measures the time it takes the burst to bounce back when it hits an object. By timing this process, it is able to calculate the exact distance between the sensor and the object. The PING))) sensor costs $24.95. Like the PIR sensor, the PING))) sensor has a three-pin connector—'GND, 5VDC, and a signal line. The signal line will return the distance measured in pulses (more about pulses later in the article).

Figure 1. The Parallax PIR sensor is shown.
Figure 2. The PING))) ultransonic sensor is shown.

Connecting to the PC
Unlike the LCD module that I discussed in my last article, the two sensors in this article cannot be directly connected to the RS-232 serial port of the PC. PC serial ports works on RS-232 voltages and serial communication, while the PIR and PING))) sensors run at 5V (TTL) and uses Pulse-Trigger and Pulse-Width to trigger. Hence, you need a microcontroller to connect to these two sensors; the microcontroller can return the results back to the PC via a serial connection.

For this purpose, I used the BASIC Stamp 2 (BS2) Module , also from Parallax. The BS2 is a microcontroller that runs at 20MHz and can execute approximately 4000 instructions per second. The BS2 costs $49.

You also need a board to house the BS2 module. I used Parallax's USB Board of Education (BoE) Development Board ($65;see Figure 3).

Figure 3. Parallax's BASIC Stamp 2 (BS2) Module and the USB Board of Education Development Board are shown.
Figure 4. Locate the servo ports on the BoE.

The BoE board has two versions: serial and USB. I recommend that you get the USB version as this saves you the trouble of buying a USB-to-serial adapter if you do not have a serial port on your computer (this especially applies to notebooks). Technically, the USB version is the same as the serial version; when you connect the USB cable to the BoE and your PC, you will notice that it is actually a serial port connection (the BoE performs a serial-to-USB conversion internally). To the .NET programmer, this is good news as we can now communicate with the BoE using serial connections (via the SerialPort class in .NET Framework 2.0).

Tip: If cost is a concern, consider the Parallax HomeWork Board, which has the Basic Stamp 2 built onto the BoE board. The HomeWork Board can be purchased in a 10-pack for $400, which is ideal if you have a few friends willing to share the pack. The price of $40 a piece is way cheaper than the combined cost of $114 for the BS2 and the BoE board.

Connecting the Sensors

Figure 5. The BoE board has color coding, which you can use to make a connection to the PIR sensor.
Figure 6. Make the connections for the PING))) sensor.

The BoE board contains four servo ports that drive servo motors. Each servo port (see Figure 4) is a three-pin connector—GND, 5VDC, and a signal line. Hence you can also connect your sensors to the servo ports. The servo ports are labeled 12 to 15.

Author's Note: For the servo port jumper (see Figure 4), be sure to set it to Vdd. This will ensure that only regulated voltages are supplied to the servo ports. Failure to do so may cause damage to your sensors.

There are two ways of connecting the sensors to the BoE board. For the PIR sensor, I will use an LCD extension cable to connect one end of the cable to the PIR sensor and the other end to the servo port. Align the connector to the color coding shown on the board (see Figure 5) and connect it to servo port number 14 (you can connect to any of the other three if you choose; but I will use port 14).

Figure 7. The connected BoE board and the two sensors are shown.
For the PING))) sensor, I will connect it to the built-in breadboard on the BoE. Using jumper wires, connect the points as shown in Figure 6. For the PING))) sensor, you can directly plug its three-pin connection onto the breadboard. Specifically, GND connects to Vss, 5V connects to Vdd, and SIG connects to P15 (pin 15). The completed connection is shown in Figure 7.

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