Burglar Alarm with PIR Motion Sensor


A friend at C# Corner asked me about PIR Motion Sensors and how they work. Recently I got one for one of my projects. As soon as I got it the first thing I thought of was to write an article about it. Working with Sensors is always amazing. I am always excited when I run my first Sketch with a new type of Sensor and they never fail to impress me. This Sensor is one of them, it is so small yet powerful. When somebody walks into my room it notifies me, isn’t that cool? For a demonstration, I made a Burglar Alarm System with this. Its detecting range is up to 6 meters. It plays a buzzer and lights a LED when it detects an intrusion in my room. Let us understand PIR Motion Sensors and how they work and how to create some cool stuff like this. Here is a short demo video of a Burglar Alarm.
PIR Motion Sensor
A passive infrared sensor (PIR sensor) is an electronic sensor that measures infrared (IR) light radiating from objects in its field of view.” It is also referred to as a PIR, "Passive Infrared", “PID”, "Pyroelectric", or "IR motion" sensors. These are small and inexpensive and operate on low power. PIR sensors are most commonly used to detect motion as if someone moves in or out within the Sensor’s range.
PIR sensor
Figure 1: PIR sensor
PIR Motion Sensor
Figure 2: PIR Motion Sensor
How it Works
Everything emits some radiation whether human, animal or any object passing within the sensor’s range, the temperature at that point in the sensor's field of view will rise from room temperature to body temperature and then back again. The sensor will detect the change in Infrared Radiation. PIR Sensors are basically made of a “Pyroelectric sensor” that can detect levels of infrared radiation. “Pyroelectricity” means heat that generates electricity, hence these sensors convert changes in Infrared Radiation to a change in output voltage. These sensors usually have a small plastic covering that is actually the lens that increases its sensing range. The plastic lens has may multiple facets to focus the infrared energy onto the sensor. Each individual facet is a Fresnel lens. PIRs do not emit any Infrared and unlike other active sensors, they do not send anything out hence they are called Passive Sensors. This image will help you to understand how a PIR sensor works.
Working of PIR Sensor
Figure 3: Working of PIR Sensor
Figure 4: PIR Sensor without Cover lens
For this project, we will make a Burglar Alarm System. It will detect an intrusion or any fire outbreak in your home. Let us see how it works in action.
Grove - PIR Motion Sensor
In this article, I will use Grove’s PIR Motion Sensor and will show how it works with Intel Galileo Gen1. You can reproduce the same example with Galileo gen2 or Arduino boards. This sensor has a Grove compatible interface, you need to just connect it through the Base Shield and start programming for it. If you are unfamiliar with Grove Sensors then please go through my article Grove Starter Kit With Intel Galileo Gen 2: Getting Started. This sensor has a detection range of a max of 6 meters but by default, it is 3 meters, you can increase or decrease it by attaching a Potentiometer to the circuit.
Other Specifications are:
  • Grove compatible interface
  • Voltage range: 3V–5V
  • 2.0cm x 4.0cm twig module
  • Detecting angle: 120 degree
  • Detecting distance: max 6m (3m by default)
  • Adjustable detecting distance and holding time
Since we are using Grove’s Sensors we don’t need to worry much about the connections or polarity. Here I have placed the Base Shield on my Galileo Gen 1 and connected the PIR Sensor to pin D2 and I also hooked up an LED to pin D4 and a Buzzer to pin D3 so when motion is detected it will alert us.
Base Shield on Galileo Gen 1 and connected the PIR Sensor to pin D2
Figure 5:  Connections for the Burglar Alarm
  1. void loop()   
  2. {  
  3.     if(isPeopleDetected())   //if it detects the moving people  
  4.         turnOnBuzzer();  
  5.     else  
  6.         turnOffBuzzer();  
  7. }  
  8. void turnOnBuzzer()  
  9. {  
  10.        digitalWrite(LED,HIGH);  
  11.     for (int i = 0; i < length; i++)   
  12.     {  
  13.         if (notes[i] == ' ')  
  14.         {  
  15.             delay(beats[i] * tempo); // rest  
  16.         }  
  17.         else  
  18.         {  
  19.             playNote(notes[i], beats[i] * tempo);  
  20.         }  
  22.         // pause between notes  
  23.         delay(tempo / 2);  
  24.     }  
  25. }  
  26. void turnOffBuzzer()  
  27. {  
  28.   digitalWrite(LED,LOW);      
  29.   digitalWrite(speakerPin,LOW);  
  30. }  
Figure 6: Code
The code is self-explanatory. When a motion is detected we are sending output to the Buzzer and LED.
I am including complete working code with this article, you are free to download it and can play around with that.
Other Project Ideas with PIR Sensor
Thief-guarding System.
Occupancy sensors: If somebody is in the room it will turn on the lights.
Motion-activated outdoor flood lights (definitely will save electricity).
Automatic Doors.