How to use the PIR Motion Sensor with Arduino.

pir motion sensor with arduino

PIR is short for passive infrared which describes how the PIR sensor operates; passive means the device doesn’t emit any energy. Basically, the sensor detects infrared radiation emitted by anything that is warm, including a person.

HC-SR501 PIR Sensor Module Overview.

The HC-SR501 PIR motion sensor has a dome is called a Fresnel lens that focuses the infrared radiation towards the  pyroelectric sensor in the middle of the module.

The module has just three pins, a Ground and a VCC for powering the module and an output pin which gives high logic level if an object is detected. Also it has two potentiometers. One for adjusting the sensitivity of the sensor and the other for adjusting the time the output signal stays high when object is detected. This time can be adjusted from 0.3 seconds up to 5 minutes.

pinout of the sensor

The module has three more pins with a jumper between two of them. These pins are for selecting the trigger modes.

There are two trigger modes:

  • Repeat mode : In this position the HC-SR501 will continue to output a HIGH signal as long as it continues to detect movement.
  • No-Repeat mode: In this position the output will stay HIGH for the period set by the TIME potentiometer adjustment.
  • Connecting the PIR sensor to Arduino.

    pir sensor with arduino

    NOTE: The HC-SR501 requires some time to acclimatize to the infrared energy in the room. This takes from 30 to 60 seconds when the sensor is first powered up.
    In addition the sensor has a “reset” period of about 5 or 6 seconds after making a reading. During this time it will not detect any motion.

    CODE

    This code is basically for keeping track of the state of the input at pin2 where the sensor is connected to know if it is HIGH or LOW. We have included an led that lights when an object is detected. You can include other security devices like an alarm.

    int led = 13;                // the pin that the LED is atteched to
    int sensor = 2;              // the pin that the sensor is atteched to
    int state = LOW;             // by default, no motion detected
    int val = 0;                 // variable to store the sensor status (value)
    void setup() {
      pinMode(led, OUTPUT);      // initalize LED as an output
      pinMode(sensor, INPUT);    // initialize sensor as an input
      Serial.begin(9600);        // initialize serial
    }
    void loop(){
      val = digitalRead(sensor);   // read sensor value
      if (val == HIGH) {           // check if the sensor is HIGH
        digitalWrite(led, HIGH);   // turn LED ON
        delay(100);                // delay 100 milliseconds 
        if (state == LOW) {
          Serial.println("Motion detected!"); 
          state = HIGH;       // update variable state to HIGH
        }
      } 
      else {
          digitalWrite(led, LOW); // turn LED OFF
          delay(200);             // delay 200 milliseconds 
          if (state == HIGH){
            Serial.println("Motion stopped!");
            state = LOW;       // update variable state to LOW
        }
      }
    }