Arduino Motion Sensor Alarm Tutorial: The Easy Way
Have you ever wanted to build a project that had could detect the presence of a person in a room? If so, then you can do this very easily using the PIR (Passive Infra Red) Motion sensor. This Arduino motion sensor can detect the presence of a person in a room. Therefore, you can build projects such as burglar alarms and automated appliances. Attach this motion sensor along with an Arduino and place it in your room to build an intruder detection system.
This tutorial will show you to interface a motion sensor with an Arduino and to use it to build a burglar alarm. This system detects the presence of an intruder in your room and sends a signal to the Arduino. The Arduino then creates an alarm sound using a buzzer to scare the intruder away.
- Arduino or an Arduino clone board (freeduino), or make your own custom Arduino board with this tutorial.
- PIR Motion Sensor.
- A piezo buzzer or an 8-ohm speaker.
- 9V Battery and connector.
- Connecting wires.
How does it work?
Here, we are using a PIR motion sensor. PIR stands for Passive InfraRed. 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 towards the infrared detector. Our bodies generate infrared heat and as a result, this gets picked up 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-7 m and is highly sensitive.
When the PIR motion sensor detects a person, it outputs a 5V signal to the Arduino. Thus, an interrupt on Arduino is triggered. 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, an alarm sound will get triggered through the buzzer.
The piezo buzzer is activated through the Arduino using PWM (Pulse Width Modulation) signals. The source code for this project at the end of this tutorial will show you how to do this.
Step 1: Connect the PIR arduino motion sensor system
The connections required to interface the Arduino motion sensor and the piezo buzzer are really simple. Connect the motion sensor to your Arduino as per the following connection diagram. Connect the Vcc and GND on the sensor to the aAduino’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.
Step 2: Upload 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 here. 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 which, 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. So, adjust it correspondingly till 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 room! Watch the sensor in action in the video below.