electronicsfan123
Published © GPL3+

Interfacing Arduino uno with PIR motion sensor

In this basic tutorial we are going to see how to interface Arduino uno with PIR motion sensor ( HC-SR501 ). So let's start !!!

BeginnerProtip21,473
Interfacing Arduino uno with PIR motion sensor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
5 mm LED: Red
5 mm LED: Red
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit diagram

Image

Image

Code

Code

C/C++
const int led = 9; // Led positive terminal to the digital pin 9.              
const int sensor = 5; // signal pin of sensor to digital pin 5.               
const int state = LOW;            
const int val = 0;                 

void setup() { // Void setup is ran only once after each powerup or reset of the Arduino board.
  pinMode(led, OUTPUT); // Led is determined as an output here.    
  pinMode(sensor, INPUT); // PIR motion sensor is determined is an input here.  
  Serial.begin(9600);      
}

void loop(){ // Void loop is ran over and over and consists of the main program.
  val = digitalRead(sensor);   
  if (val == HIGH) {           
    digitalWrite(led, HIGH);   
    delay(500); // Delay of led is 500             
    
    if (state == LOW) {
      Serial.println(" Motion detected "); 
      state = HIGH;       
    }
  } 
  else {
      digitalWrite(led, LOW);
      delay(500);             
      
      if (state == HIGH){
        Serial.println("The action/ motion has stopped");
        state = LOW;       
    }
  }
}

Credits

electronicsfan123

electronicsfan123

5 projects • 8 followers

Comments