Every mad scientist’s lab, or teenager’s secret room, needs advanced protection against intrusion by rogue agents or siblings. You should likely consider getting a Passive infrared (PIR) sensor for you. PIR sensors accept you to detect when anyone is in the room.
This sensor is the same you will find in most modern security systems, garage door, automatic light switches, openers and similar applications where the working of some electrical device is required only in the existence of humans.
How PIR Motion Sensor Works?
If you did not know, all objects with a temperature above Absolute Zero (0 Kelvin / -273.15 °C) release heat energy in the form of infrared radiation, including human bodies. The object release more radiation according to body temperature. PIR sensor is uniquely create to detect such levels of infrared radiation. It include two main parts: A Pyroelectric Sensor and A special lens called Fresnel lens which focuses the infrared signals onto the pyroelectric sensor.
A Pyroelectric Sensor has two rectangular slots that made of a material that allows the infrared radiation to pass. There are two separate infrared sensor electrodes, one is responsible for producing a positive output and the other one is for negative output. The reason is that we are looking for change in IR levels and not setting IR levels. The two electrodes are wired up so that they cancel each other out. If one half sees more or less IR radiation than the other, the output will wave high or low. if there is no movement around the sensor; both slots detect the same amount of infrared radiation, resulting in a zero output signal. So, sensor is idle. But when a hot body like a human or animal passes; it first cut off one half of the PIR sensor, which causes a positive differential change between the two halves. When the hot body leaves the sensing area, the reverse happens, whereby the sensor generates a negative differential change. The corresponding pulse of signals results in the sensor setting its output pin high.
HC-SR501 PIR Motion Detector
For most of our Arduino projects we need to detect when a person has entered or left the area, HC-SR501 PIR sensors is great choice. It is low power and low cost. It is easy to interface with and are insanely popular among hobbyists. HC-SR501 PIR sensor has three output pins VCC, Output and Ground as shown in the figure below. It can be powered by any DC voltage from 4.5 to 12 volts because it has a built-in voltage regulator, mostly 5V is used. Other than this, there are a couple options you have with your PIR. Let’s check them out.
There are two potentiometers on the board to adjust a couple of parameters:
⦁ Sensitivity– This sets the maximum distance that motion can be detected. It ranges from 3 meters to approximately 7 meters. The topology of your room can affect the actual range you achieve.
⦁ Time– This sets how long that the output will remain HIGH after detection. At minimum it is 3 seconds, at maximum it is 300 seconds or 5 minutes.
Finally the board has a jumper (on some models the jumper is not soldered in). It has two settings:
⦁ H– This is the Hold/Repeat/Retriggering In this position the HC-SR501 will continue to output a HIGH signal as long as it continues to detect movement.
L– This is the Intermittent or No-Repeat/Non-Retriggering In this position the output will stay HIGH for the period set by the TIME potentiometer adjustment.
HC-SR501 PIR Sensor Pinout
The HC-SR501 has a 3-pin connector that interfaces to the outside. The connections are as follows:
VCC is the power supply for HC-SR501 PIR sensor. It is connected to the 5V pin on the Arduino.
Output pin is a 3.3V TTL logic output. LOW indicates no motion is detected, HIGH means some motion has been detected.
GND should be connected to the ground of Arduino.
Using PIR Sensor as a standalone unit
One of the reasons of HC-SR501 PIR sensor being extremely popular is the fact that HC-SR501 is a very flexible sensor that is capable all on its own. By interfacing it to some micro controllers like an Arduino you can expand upon its versatility even further. In our experiment we will use the HC-SR501 on its own to show how useful it is by itself.
The wiring is very simple. Batteries are connected to VCC and GND of the sensor and a small Red LED is connected across output pin through a 220Ω current limiting resistor.
Once you give power to the circuit, you have to wait for 30-60 seconds for the PIR to adjust to the infrared energy in the room. The led maybe blink for once. Wait until the LED is off. Then move around in front of it, waving a hand, etc, to see the LED light up!
Wiring – Connecting PIR Sensor to Arduino UNO
Now we completely understand the PIR, how it works. We can begin wiring up with Arduino.
The connections is very simple. The PIR work as Digitally output. So you have to connect the output wire with the digital pin #2 of Arduino. Than connect the Vcc pin of PIR with the 5V of Arduino. Connect GND pin with the GND of Arduino.
Arduino Code
int ledPin = 13; // choose the pin for the LED
int inputPin = 8; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW)
{
Serial.println("Motion detected!"); // print on output change
pirState = HIGH;
}
}
else
{
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH)
{
Serial.println("Motion ended!"); // print on output change
pirState = LOW;
}
}
}
The result on Serial Monitor is