A home intrusion detection setup (sort of)

And Yet It Sees

The second thing I had to deal with was vision. I could have used vision literally in my project by hooking up a camera and using the Motion [3] program to check differences between one image and the next, because I had a camera I could have taken apart. (I did eventually use it, but not for that purpose, as you will see later in Figure 7.) The problem is that I had an ultrasonic sensor, and I wanted to play with it.

The sensor is an old SeeedStudio Ultrasonic Sensor V1.0, as shown in Figure 6. Originally, I thought I would have to break out my trusty Digispark [4], assuming the sensor would deliver the distance as an analogical number back over the wire with the distance in centimeters, microns, or whatever.

Figure 6: The rather old-fashioned SeeedStudio Ultrasonic Sensor V1.0 only has three pins.

Not so. An ultrasonic sensor is much more basic than that, and your software has to do the heavy lifting. It calculates the distance in the same way you would a thunderstorm: You send a pulse over the pin to the sensor that makes the speaker emit a very short ultrasonic blast. Then, you start counting.

When the microphone picks up the echo, you can calculate the distance to the nearest object, taking into account the speed of sound. Sound travels at about 34,000cm/sec. If the sensor captures the echo, say, 0.001 seconds after it was sent, the pulse has traveled 34 centimeters. But, the ultrasonic pulse travels to the object and then bounces back, so you have to divide that figure by two to get the real distance to the object: 17cm.

Many newer ultrasonic sensors have four pins: one for power, one for ground, and then an output pin that receives the pulse from one of the Pi's GPIO pins and an input pin to capture the echo and send a HIGH signal to the Pi when it arrives. This allows you to set up the two GPIOs – one as output and another as input – at the beginning of your program, so you don't have to faff about with that later when timing is of the essence.

But, not my V1.0! The V1.0 only has three pins: The sensor pin is used for input and output, so you have to set your GPIO to output, send the pulse out, switch the pin to input, start counting, and then wait for the echo. If you're not careful about the order in which you do all of the above, you'll find that the pulse has come and gone before you start listening, and your program will loop forever waiting for a sonic pulse that's already history.

After a lot of trial and error, I managed to get things to work with a program like you can see in Listing 3.

it will print out the distance to the nearest object in centimeters, which is much cooler than using a tape measure.

Bear in mind that this program works for a sensor that has its input and output going through the same pin. If you have a more modern sensor with four pins, you will be able to separate the input and output onto two separate pins.

Mounting the ultrasonic sensor proved a challenge. I ended up carving a piece of cardboard from a box, screwing it to the plaque, and then sticking the sensor to the cardboard with Blu-Tack. You can see the result of my handiwork in Figure 7.

Figure 7: The ultrasonic sensor is stuck to the turret using Blu-Tack. The camera is screwed to the plaque using the screw that held its original casing together.

All Together Now!

The next step is making the servo and ultrasonic sensor work together. I decided that my turret would have two modes: scanning and surveillance. First, it would scan the environment, calculating the distance to objects in its 180º arch. That's what's going on in lines 25 to 36 in Listing 4. In the setup() function block of code (lines 12-23), you call the scan() function three times (I'll explain why later) and rotate the turret first counterclockwise, then clockwise, and finally counterclockwise again, measuring the distance in each position as you go.

Real-world data is notably imprecise. If the servo is not exactly in the right place when taking the measurement, the measurement will be wrong. The solution is to measure distances for each position three times, just to make sure (lines 16-18) and then average them out (line 21). The final list of measurements, along with the position at which they were taken indexed as keys, are stored in the Python dictionary construct distance.

When you're done scanning, the turret goes into surveillance mode (lines 107-114). In surveillance mode, it rotates to random positions (line 108) and checks to see whether anything has moved from its original position (line 110). If a sizable difference exists in the measurement (more than 10cm), the program takes a photo with the webcam, replaces the original value in the dictionary, and continues watching.

An Advent calendar during the Holiday season is a lovely tradition, but the calendars on the market are rather ordinary. We describe a Rasp Pi project that lets you create an innovative and unique Advent calendar.