IAN LANG ELECTRONICS

That's the bunny on the right there. It's known to Maplin as the KU60

and if you click on the picture it'll take you to the web page for it. It works on 12V but it's unbearably loud at nine. There's five wires and if you connect the green and the yellow ones it produces a shrill two-tone sound that is deeply annoying and unmissable unless you are profoundly deaf. The red and black are positive and negative, and the red one is what you connect to Vin and then you power the Arduino board with a 9V supply.

The Idea is not so far removed from the LED

setup earlier. When the input to pin 2 goes high, the

output from pin 8 follows suit and vice versa. There follows a short

film showing the basic idea, and then it's over the page for the analysis.

Before delving into this topic full-tilt, it might be best to describe exactly what a PIR sensor is and does. So for the first part of this article, we go over to Clever Clive and the Sciency Misits for an explanation of the principles of infra-red radiation.

 

All objects emit infra-red radiation and the warmer or colder the object the different the wavelength it emits. Objects only stop emitting when their temperature reaches absolute zero, which is 0 Kelvin or -273.15 degrees centigrade. The wavelengths emitted can be as short as 0.74 and as long as 300 micrometres. Humans at a normal body temperature (37 degrees centigrade) emit infra-red (IR) at around 12 micrometres.

 

PIR stands for Passive Infra Red and what this means is that the device does not put out any beam of infrared of its own. What it does is to analyse the "normal" state of whatever it is pointing at by looking at the pattern of infrared in the field of view. if that pattern changes abruptly, say by somebody walking across it and emitting IR at a different wavelength to what is considered normal, the pattern breaks and the change is detected by the sensor. The IR is reflected onto a pyroelectric sensor (PS)  and the associated circuitry behind it is used to assess the normal and detect a deviation to the normal.

Using a PIR Sensor

Go Back pir pirsideon

Specifically in this article I'm going to be using this little beast on the left. In real life it's about an inch square, and that plastic dome there is a series of Fresnel (pronounced Frennell) mirrors designed to focus IR waves on to the PS whilst keeping the visible spectrum and higher out.

 

It's called the SPD61 and it's available from Oomlout; clicking on the picture on the left takes you to their UK web page for it.

The module takes a 5V supply and puts out 3.3V at the signal wire when it detects movement and sinks it to 0V when it doesn't. This means you can use it with a transistor switch or indeed to provide a digital signal to a microcontroller, just like we're about to do here. You'll notice that on one side there's a variable jumper; what that does is to switch between timed output and output on detection. On the other side there's a small potentiometer. That acts with the timed output, and turning it one way gives output for about one second, turning it the other for about twenty seconds. At the moment it does not matter which you use as we are going to look for an output, regardless of the time it lasts.

pirschem

Above  is the schematic for attaching it to your Arduino as given by Oomlout, and a second one drawn by me showing the whole connection to activate an LED when motion is sensed, and it really is as easy as it looks. There is a convoluted code supplied by Oomlout, but a less complex form is the one following. Copy, paste and upload to your board (when you've wired it up) the following:

 

 

boolean sensorstate;

void setup(){

  pinMode(8,OUTPUT);

  delay  (60000);

}

 

void loop(){

 

  sensorstate=digitalRead(2);

  if (sensorstate==1) {digitalWrite(8,HIGH);}

  else{digitalWrite(8,LOW);}

}

 

This is the basic code needed to get the sensor working. Assuming all has gone well, here's what will happen:

 

For 1 minute after uploading, nothing happens.

 

After 1 minute, if  any movement is detected, the LED will come on, and then go off again. It's very sensitive and so the movement only needs to be slight. I suggest in testing that you stand behind the PIR sensor, and walk round to the front of it, or slowly move your hand round the front. When no movement is detected the LED will stay off.

 

If the LED stays on for a long time, then turn the potentiometer anti-clockwise to reduce the time it stays on.

 

 

Let's canter through the code:

 

boolean sensorstate;

 

sensorstate is the variable that we are going to use to monitor the output of the PIR sensor. There are only two states it can be in, high or low, (1 or 0) and so the boolean gets assigned a value of either 1 or 0 too.

 

void setup(){

  pinMode(8,OUTPUT);

  delay  (60000);

}

 

Pin 8 gets set to an output so no resistance is encountered to the flow of current. The LED will shine only dimly if we do not do this. The delay of one minute gives the PIR sensor ample opportunity to acclimatise to the normal state.

 

 

 

void loop(){

 

  sensorstate=digitalRead(2);

  if (sensorstate==1) {digitalWrite(8,HIGH);}

  else{digitalWrite(8,LOW);}

}

 

The above is how the state of the sensor affects the state of the LED. If the sensor output, which becomes the input at pin 2 is high (1) pin 8 is switched to high, the LED gets a current and shines. If not, pin 8 is switched low, the LED gets no current and stays dim.

 

 

The usual application for such a device is in the field of security. Typically it turns on lights and alarms, and so in the next few steps we are going to make it activate a siren when motion gets detected.

 

Now, a blinky light is all very well, but what if you aren't looking at the light when it goes off? You'll not get much of a warning from that. What we need to do is make a big noise. We're going first of all to look at a pin on your arduino board that we have not previously considered in these pages, which is Vin.

 

 

 

pirschem2

Although above I've shown the R2, if you've got the R3 it's in the same place, on the Mega it's next door to A0 too and if you're using a mini you don't have this function; you have to distribute from your supply input. You can't use a Fio for this game unless you regulate the supply voltage to the board and tap off before regulation to the siren.

 

Speaking of the siren, what shall we use? We could of course create a frequency at one of the pins. Yep. then we could run that through an amplifier, and out through a loudspeaker. We could do all that. We aren't though, because what we are going to do is do it the lazy way and use a module from Maplin.

More Lobes

In that video I mentioned lobes. Lobes are the points at which the detector can detect motion and to learn more click here: