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, automated appliances, etc. 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.
What are the stuff required to do this project?
- 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, a 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 arduino. Thus, an interrupt on the arduino is triggered. And we define what the arduino should do as it detects an intruder. Here, we are creating an alarm sound through a piezo buzzer. Therefore, 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 signals. The source code for this project at the end of this tutorial will show you how to do this.
Step 1: Connecting the PIR arduino motion sensor
The connections required for interfacing the arduino motion sensor and the piezo buzzer is really simple. Connect the motion sensor to arduino as per the following connection diagram. Connect the Vcc and GND of the sensor to arduino’s 5V and GND pins. Next, connect the Output signal pin of the motion sensor to arduino’s digital pin no 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 arduino’s GND pin. And the positive terminal of the buzzer (red wire) to arduino’s digital pin no 9.
Step 2: 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 from 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 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 posses, it’s sensitivity and delay time could be adjusted. So, adjust it correspondingly till it reaches the sweet spot.
Here, whenever the PIR motion sensor detects a person, it outputs a digital HIGH signal to 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 for creating the alarm sound on the piezo buzzer or speaker. It does this by writing PWM signals of varying period 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.
So, use this arduino motion sensor to build burglar alarm systems, home automation systems or any simple gadget which prevents people from getting into your room ;) Watch the sensor in action in the video below: