Have you ever wanted to build a project that could detect the presence of a person in a room? You can do this very easily using the PIR (passive infrared) motion sensor, and build projects like burglar alarms and automated appliances.
This tutorial will show you how to interface a motion sensor with an Arduino and use it to build a burglar alarm. This system detects the presence of an intruder in a room and sends a signal to the Arduino. The Arduino then creates an alarm sound using a buzzer to scare the intruder away.
How Does the Motion-Sensor Alarm Work?
Here, we are using a PIR motion sensor. This motion sensor consists of a fresnel lens, an infrared detector, and supporting detection circuitry. The lens on the sensor focuses any infrared radiation present around it toward the infrared detector. Our bodies generate infrared heat, and this heat is detected by the motion sensor. The sensor outputs a 5V signal for a period of one minute as soon as it detects the presence of a person. It offers a tentative range of detection of about 6–7m and is highly sensitive.
When the PIR motion sensor detects a person, it outputs a 5V signal to the Arduino and triggers an interrupt. We define what the Arduino should do as it detects an intruder. Here, we are creating an alarm sound through a piezo buzzer. When the sensor detects an intruder, it triggers an alarm sound through the buzzer.
The piezo buzzer is activated through the Arduino using PWM (pulse width modulation) signals. The source code at the end of this tutorial will show you how to do this.
Connecting the PIR Arduino Motion Sensor System
The connections required to interface the Arduino motion sensor and the piezo buzzer are very simple. Connect the motion sensor to your Arduino as per the following connection diagram. Connect the VCC and GND on the sensor to the Arduino's 5V and GND pins. Next, connect the output signal pin on the motion sensor to the Arduino's digital pin 2 (interrupt pin 0).
After hooking up the Arduino motion sensor, we have to connect the piezo buzzer to this system. To do this, connect the negative terminal of the buzzer (black wire) to the Arduino's GND pin and the positive terminal of the buzzer (red wire) to the Arduino's digital pin 9.
Uploading the Code for the Arduino Motion Sensor and Piezo Buzzer
Now we have to test the Arduino motion sensor along with the piezo buzzer to see if it works. To do this, download the code for the Arduino motion sensor project. Next, open this code in the Arduino IDE. Upload the code to your Arduino. Check if it works by opening the serial monitor (at a baud rate setting of 115200bps).
Note that the motion sensor will take a minute to get calibrated with the surroundings after you power it up. The sensor gets activated whenever you are within a 6–7 m of the radius of the Arduino PIR motion sensor. After that, the piezo buzzer will begin to make an alarm sound, and the string "Intruder detected" gets printed on your serial monitor. The sensor outputs a LOW signal (0V) after a minute when you are no longer near the radius of the PIR motion sensor. Depending on the PIR motion sensor you use, its sensitivity and delay time can be adjusted. Adjust it correspondingly until it reaches the sweet spot.
Whenever the PIR motion sensor detects a person, it outputs a digital HIGH signal to the Arduino's digital pin 2. This immediately triggers an interrupt function called intruder_detect(). You can program the Arduino to do something when it detects an intruder by editing the function intruder_detect(). Currently, it first prints the string "Intruder detected". Next, it runs a function called siren() three times. The function siren() consists of the code required to create the alarm sound on the piezo buzzer or speaker. It does this by writing PWM signals of varying periods and frequencies to the piezo buzzer.
You will also notice that there is a delay associated with the motion sensor after each detection. Depending on the sensor, you may be able to adjust this delay.
Use this Arduino motion sensor to build burglar alarm systems, home automation systems, or any simple gadget that prevents people from getting into your space! Watch the sensor in action in the video below.