How to use the Motion Sensor with Arduino

How to use the Motion Sensor with Arduino

PIR Motion Sensor for Arduino

INTRODUCTION

A passive infrared (PIR) sensor is an electronic sensor that measures infrared light radiating from objects in its field of view which can be used to detect movement in an area in front of the sensor.

APPLICATIONS

PIR sensor can detect animal/human/object movement within range, which is determined by the spec of the specific sensor. They are commonly used in security alarms and automatic lighting applications.

WIRING DIAGRAM

VCC – 5V

OUT – Digital Pin 2

GND - GND

PIR Motion Sensor for Arduino

 

 

CODE

 

int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status and detecting change 
 
void setup() {
        
  pinMode(inputPin, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
}
 
void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
      // turn LED ON
    if (pirState == LOW) {
      
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    
    if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

END RESULT

Open serial monitor and place an object in front of the sensor to detect its motion.

ADDITIONAL RESOURCES

https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor/using-a-pir-w-arduino

https://randomnerdtutorials.com/arduino-with-pir-motion-sensor/

http://www.instructables.com/id/PIR-Motion-Detector-With-Arduino-simple-and-Easy-D/

https://howtomechatronics.com/tutorials/arduino/how-pir-sensor-works-and-how-to-use-it-with-arduino/

https://maker.pro/arduino/tutorial/how-to-build-an-arduino-powered-motion-sensor-alarm

https://www.wired.com/2012/09/using-motion-detectors-with-an-arduino/