IAN LANG ELECTRONICS

A Perimeter Alarm

Go Back

Have you ever heard of a nightingale floor? When  carpenters made this, they laid the floorboards so that when the boards came under pressure from somebody walking on them, the nails rubbed against a jacket or a clamp and the floor "sang". The upshot was that nobody could come down a corridor undetected, at least not by the floor. The most famous example was Nijo castle in Japan, where the idea originated.

This is the electronic version. We're into some James Bond stuff here. By using two (or three as we shall see) modules and an Arduino we can make an alarm that can loudly tell you when somebody who shouldn't have has come through a door or down a hallway.

Now some purists think that modular building is somehow cheating. I say sod 'em. Modules are quick cheap and hassle free and take less fault finding than does building with discrete components, and besides we are going to supplement the modules with four of the latter.

 

The first module we are going to use is one I bought from Oomlout and it's a distance sensor, in fact the memorably titled Sharp GP2Y0A21YK. There's a picture of it below and if you click on the picture it'll take you directly to Oomlout's web page for it.

This device puts out an infrared signal and then detects how much returns and puts out an analogue voltage in accordance. It really is so simple to use; the red wire is attached to the 5V supply, the black one to ground and the white one to one of the analogue input pins of the Arduino board. You could in theory run five off the same board with no modification; a larger number if you paired or tripled up on the pins.

The return voltage is not linear, and looks on a graph more like a geometric function, but the practical upshot is that the closer an object is the more voltage is put out, up to about 10cm when it goes a bit freaky. Useless for precision measuring, but great for detecting objects.

On the right is the second module we are going to use and it is sold by Maplin; clicking on the image will bring up their webpage for it. It's a piezo buzzer housed along with some driving circuitry in a plastic frame and if you give it 12V it is unbearably loud. It's still not going to lull any babies to sleep if you give it 9V which makes it perfect for portable alarm systems as you can power it with a 9V battery. There are five wires, red for supply, black for ground and orange yellow and green for tone control:

If you pair the yellow and green and leave orange open

you get a really horrible and loud chirrup out of it. Try pairing them in turn until you find a tone you like.

Right, here comes a circuit:

 

As you can see, the distance sensor's output goes to pin A0 of the Arduino board and it's linked to ground by a 560 ohm resistor. This latter is to stop the pin going high by dint of random fluctuation and thus setting off a false alarm. If the voltage at pin A0 is high enough, we can set pin 2  high; this causes the NPN transistor to conduct and gives a path to ground for the coil of the relay which energises and creates a complete circuit path for the 9V battery. The 2k2 resistor is there to stop overcurrent at the base of the transistor, and the diode at the collector is a snubber. The relay ensures that the two supplies are isolated from each other but you can run the Arduino off  the same battery as the alarm if you wish. This is the basic system. Here's some code to make it work:

 

 

void setup(){

pinMode (2,OUTPUT);

}

 

void loop () {

 

 

  if (analogRead(A0)>100){

    digitalWrite(2,1);}

   

 

}

 

It really is that easy.

 

Now it would be nice if we could control this remotely and so break out a radio control board and transmitter like we did in the last piece. Attach one of the output wires to A5 and the other to A4 and hit your board with this code:

 

boolean onflag=false;

void setup(){

pinMode (2,OUTPUT);

}

 

void loop () {

if (analogRead(A4)>1000){

  onflag=true;}

  if (analogRead(A5)>1000){

    onflag=false;}

 

  if (analogRead(A0)>100&&onflag==true){

    digitalWrite(2,1);}

   

    if (onflag==false){

       digitalWrite(2,0);}

}

 

There's only one concept in here that will not be familiar to you by now and that's that of the variable of type boolean. A boolean variable can have only the values of true or false. The advantage? takes up half the memory space of a variable of type int.

 

When you first power up your alarm, it won't work. You have to press the on button on your transmitter. Once the alarm activates, pressing the off button will stop it. The off button will also prevent it from activating. To switch the device off completeley, I recommend an SPST key switch which you can buy from anywhere that sells electronic supplies.

The performance of the alarm is far better in the dark than it is in the light.  Play about with the value of 100 in this line:

 

  if (analogRead(A0)>100&&onflag==true){

 

Making the value greater will make the alarm less sensitive, and making it smaller will make the alarm more sensitive. You may want to consider incorporating a potentiometer to vary the value according to need.

 

Ian Lang, October 2011