PIR Motion Sensor – Arduino – Home Automation


This tutorial will teach you how to use arduino and PIR Motion sensor for doing awesome projects.
By Using PIR Motion sensor you can do projects like switch on the Lights when person entries the room/house. PIR Motion sensor is very important in Home Automation. If you use this sensor then you can save some electricity.
I’m showing you a simple example where PIR Sensor will be connected to arduino and when some motion is detected then the default arduino LED will be on and when the motion is not detected for more then 10 sec then the LED will e turned off.

This project can be extended by using Relay to switch on Light , Fans etc….
In fact in next tutorial i’ll show how to use Relay is integrated to PIR Sensor. Lets stop this theory and start doing practically.

Things to be done:

  • I’ll show How PIR Motion sensor looks.
  • I’ll show how Arduino looks for who are new to Arduino.
  • I’ll explain Connection between arduino and PIR.
  • I’ll explain the code.
  • More important i’ll give link for code – Github.

PIR Motion Sensor










Arduino UNO



Interface between Arduino and PIR Motion Sensor

There are only connection it’s very easy. The PIR sensor output is digital.
The output is HIGH when the motion is detected.
The output is LOW when the motion is not detected.

VCC of PIR -> VCC(5v) of Arduino
GND of PIR -> GND of Arduino
Out of PIR -> pin3 of Arduino


int pirPin = 3; //the digital pin connected to the PIR sensor’s output

This is code where we define pin 3 as PIR digital input to arduino.

Variable Declaration

//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 10;

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 10000;

boolean lockLow = true;
boolean takeLowTime;

int pirPin = 3; //the digital pin connected to the PIR sensor’s output
int ledPin = 13;


void setup(){
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);

//give the sensor some time to calibrate
Serial.print(“calibrating sensor “);
for(int i = 0; i < calibrationTime; i++){
Serial.println(” done”);
Serial.println(“SENSOR ACTIVE”);


void loop(){

if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.print(“motion detected at “);
Serial.println(” sec”);
takeLowTime = true;

if(digitalRead(pirPin) == LOW){

lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() – lowIn > pause){
digitalWrite(ledPin, LOW);//the led visualizes the sensors output pin state
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print(“motion ended at “); //output
Serial.print((millis() – pause)/1000);
Serial.println(” sec”);

According to datasheet the calibration time of PIR sensor is between 10 – 60 sec.


When there is motion the LED will be on, this is shown in below image



Github Link:


Thank you. :)

Leave a Reply

Your email address will not be published. Required fields are marked *