Arduino - PIR Sensor

    0

    0

    Sohail Gulam

    unoarduino

    PIR sensors allow you to sense motion. They are used to detect whether a human has moved in or out of the sensor’s range. They are commonly found in appliances and gadgets used at home or for businesses. They are often referred to as PIR, "Passive Infrared", "Pyroelectric", or "IR motion" sensors.

    #define pirPin 2
    int calibrationTime = 30;
    long unsigned int lowIn;
    long unsigned int pause = 5000;
    boolean lockLow = true;
    boolean takeLowTime;
    int PIRValue = 0;
    void setup() {
       Serial.begin(9600);
       pinMode(pirPin, INPUT);
    }
    void loop() {
       PIRSensor();
    }
    void PIRSensor() {
       if(digitalRead(pirPin) == HIGH) {
          if(lockLow) {
             PIRValue = 1;
             lockLow = false;
             Serial.println("Motion detected.");
             delay(50);
          }
          takeLowTime = true;
       }
       if(digitalRead(pirPin) == LOW) {
          if(takeLowTime){
             lowIn = millis();takeLowTime = false;
          }
          if(!lockLow && millis() - lowIn > pause) {
             PIRValue = 0;
             lockLow = true;
             Serial.println("Motion ended.");
             delay(50);
          }
       }
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.