How To Interface PIR Sensor With Arduino
Hello Arduinians... Ever thought of detecting motion via your Arduino.... if yes, then this one's for you. In this tutorial, We're interfacing PIR sensor today with Arduino. Let's gets started!
Components Required For This Tutorials
- 1 * Arduino Board
- 1 * PIR Motion Sensor(you can buy from here)
- Breadboard
- Jumper wires
- Battery
What is PIR motion sensor & how it works?
Basically, a PIR stands for "Passive Infrared Sensor". The sensor works on the principle of infrared waves' detection those are being emitted by human bodies. They are undetectable to us but can be discovered by the devices designed for such special purposes. I'm going to go just a little deep, enough to make you understand the internal operation.
When a body moves in front of a PIR sensor, the temperature of that place changes where the body has moved. If the body makes a further movement, the composition of infrared waves being received by the PIR from that region changes and since the reading is not the same as last reading, it generates a signal for the microcontroller and we work on it accordingly.
Circuit Diagram
We can easily interface PIR motion sensor with Arduino-like our other sensor. But it all depends on our logic of the code, how efficiently do we want to detect motion through it.
A PIR has 3 pins, just like any other sensor, Vcc, Signal & Ground pin. A very simple circuit of interfacing PIR motion sensor with Arduino is shown below
Circuit |
PIR Motion Sensor |
Source Code
Here is a very simple code for this tutorial which only detects if there is ANY motion and triggers an LED for confirmation.Write below code into Arduino IDE and compile it.
int pirSensor=5; // connect sensor at pin 5
int led=13; // connect led at pin 13
void setup()
{
Serial.begin(9600); // initialize serial
pinMode(led, OUTPUT); // initialize led as output
pinMode(pirSensor, INPUT); // initialize sensor as input
}
void loop()
{
int sensorValue = digitalRead(sensor); // read sensor value
Serial.println(sensorValue); // print sensor value
delay(100); // delay 100 miliseconds
if(sensorValue > 600)
{ digitalWrite(led,HIGH); }
}
Now you can connect the signal pin to any analog pin also but for that, you'll have to check what values do you get on the serial monitor and which value do you wish to use as a threshold for triggering any LED, buzzer, etc.
Note that the sensor first takes some time for its calibration and after that it starts detecting motion. If any motion is detected, it triggers the LED on Arduino board and keeps searching for the motion for a certain amount of time. It only settles low if there is no further motion detected for that given search period (which can be changed in code). Giving a search window to your sensor, makes it's working more efficient and there are better chances for you to get non-erroneous functioning.
I too did both codes and both worked fine for me. Here's a snap!
Demo |
So that's an end to our tutorial for this time. Hope you enjoyed reading. If you liked this tutorial then don't forget it to share with your friends. I'll be back with more..... till then #happyDIYing
If you have any question or query or feedback regarding this tutorial then leave a comment below and we will solve those as soon as possible.
http://www.ebay.in/usr/4d_innovations?_trksid=p2053788.m1543.l2754
The vendor's offering interesting and working hardware at quite handy rates. Go give it a try! :)
Comments
Post a Comment