PIR Sensor Gives False High in 55 Minutes (Can't find a way to solve)



  • Hi,

    I have built a system to identify motions and fire alerts. System can send a SMS in case if it's identify such alerts.

    System consist of two parts,

    Part 1: Armed and Disarmed using a PIN (Using Arduino Uno)

    This Module Consists with 4*4 PIN PAD, I2C Module, 2 LEDs and Arduino UNO This part works fine

    Part 2: Sensor Systems and SMS System

    This module consists of Arduino NANO, MQ135 Gas Sensor, PIR Sensor, SIM 800L Module, Tamper Switches This part also works, but in every 55 minutes it generates a false motion alert and I could not troubleshoot the issue.
    Schematics is attachedalt text

    Codes as Below,
    Part 1: Test6-LCD_Intigrated_Working_Temper.ino

    #include <Keypad.h>
    #include<EEPROM.h>
    #include <Wire.h> // Library for I2C communication
    #include <LiquidCrystal_I2C.h> // Library for LCD
    char password[4];
    char pass[4],pass1[4];
    int i=0;
    int k=0;
    char customKey=0;
    byte col = 0;
    const byte ROWS = 4; //four rows
    const byte COLS = 4; //four columns
    char keys[ROWS][COLS] = {
    
    {'1','2','3','A'},
    
    {'4','5','6','B'},
    
    {'7','8','9','C'},
    
    {'*','0','#','D'}
    
    };
    
    
    byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
    
    byte colPins[COLS] = {13, 12, 11, 10}; //connect to the column pinouts of the keypad
    
    //initialize an instance of class NewKeypad
    
    Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
    void setup()
    {
      lcd.begin();
    
      Serial.begin(9600);
      pinMode(4, INPUT);      // Tamper Signal
      pinMode(3, OUTPUT); //Armed 
      pinMode(2, OUTPUT); //Disarmed
    
      lcd.setCursor(0, 0);
      lcd.print ("--SECURE ROOM!--");
      Serial.print("Server Room");
      lcd.setCursor(0, 1);
      lcd.print ("PLEASE ENTER PIN");
      Serial.print("\n*********Keypad Lock********* ");
      delay(2000);
      Serial.println("\n*********Enter Ur Passkey:*********");
    
      for(int j=0;j<4;j++)
        EEPROM.write(j, j+49);
      for(int j=0;j<4;j++)
        pass[j]=EEPROM.read(j);
    }
    
    void Armed(){
    
      digitalWrite(3, HIGH);
      digitalWrite(2, LOW);
      Serial.println("\n***System is Armed***");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("--SYSTEM ARMED--");
      lcd.setCursor(0, 1);
      lcd.print("-SENSORS ACTIVE-");
      delay (2000);
      lcd.noBacklight();
      i=0;
      customKey=0;
    
      }
    
    void change()
    {
      int j=0;
     lcd.backlight();
      Serial.println("\n*********UR Current Passk*********");
      lcd.clear(); 
          lcd.setCursor(0, 0);
          lcd.print("   CURRENT PIN   ");
    
      while(j<4)
      {
        char key=customKeypad.getKey();
        if(key)
        {
          pass1[j++]=key;
    
          Serial.print(key);
          lcd.setCursor(j+6, 1);
          lcd.print(key);
    
    
        }
        key=0;
      }
      delay(500);
    
      if((strncmp(pass1, pass, 4)))
      {
    
    
        Serial.println("\n*********Wrong Passkey*********");
        lcd.clear(); 
        lcd.setCursor(0, 0);
          lcd.print("  UNAUTHORIZED  ");
    
        Serial.println("\n*********UNAuthorized Access*********");
          lcd.setCursor(0, 1);
          lcd.print(" ACCESS  DENIED ");
        delay(10000);
      }
      else
      {
        j=0;
    
    
        Serial.println("\n*********Enter New Passk: *********");
          lcd.clear(); 
          lcd.setCursor(0, 0);
          lcd.print(" ENTER NEW PIN! ");
    
        while(j<4)
        {
          char key=customKeypad.getKey();
          if(key)
          {
            pass[j]=key;
    
            Serial.print("*");
            EEPROM.write(j,key);
            lcd.setCursor(j+6, 1);
            lcd.print(key);
            j++;
    
          }
        }
    
        Serial.println("*********Done*********");
        lcd.clear(); 
          lcd.setCursor(0, 0);
          lcd.print("----UPDATED!----");
        delay(1000);
      }
    
    
      Serial.println("\n*********Enter Ur Passk: *********");
      lcd.clear(); 
    
          lcd.setCursor(2, 0);
          lcd.print ("SECURE ROOM!");
          lcd.setCursor(0, 1);
          lcd.print ("PLEASE ENTER PIN");
          lcd.noBacklight();
      customKey=0;
    }
    
    
    void tamper(){
      Serial.println("\n***System is Tampered***");
      lcd.clear();
      lcd.setCursor(2, 0);
      lcd.print("--TAMPERED--");
      lcd.setCursor(0, 1);
      lcd.print("WARN COMPROMISED");
      delay (2000);
      lcd.backlight();
      i=0;
      customKey=0;
    
      }
    
    void loop()
    { 
      Serial.println(digitalRead(4));
      customKey = customKeypad.getKey();
      if(customKey=='A')
        Armed();
    
        if(customKey=='C')
        change();
       /* 
        if(digitalRead(4)==LOW)
        tamper();
     */
      if (customKey)
      {
        lcd.backlight();
        password[i++]=customKey;
    
        Serial.print("*");
        lcd.setCursor(0, 0);
        lcd.print("    PIN INPUT    ");
        lcd.setCursor(0,1);
        lcd.print("       ----     ") ;
        lcd.setCursor(i+6, 1);
        lcd.print("*");
    
    
          }
    
    
    
    
      if(i==4)
      {
        delay(200);
        for(int j=0;j<4;j++)
          pass[j]=EEPROM.read(j);
        if(!(strncmp(password, pass,4)))
        {
    
          Serial.println("\n*********Passkey Accepted*********");
        lcd.clear(); 
        lcd.setCursor(0, 0);
        lcd.print("---AUTHORIZED---");
        lcd.setCursor(0, 1);
        lcd.print("SYSTEM  DISARMED");
    
          delay(1000);
          digitalWrite(2, HIGH);
          digitalWrite(3, LOW);
          i=0;
    
        }
        else
        {
    
          Serial.println("\n*********Access Denied*********");
          lcd.clear(); 
          lcd.setCursor(0, 0);
          lcd.print("  UNAUTHORIZED  ");
          lcd.setCursor(0, 1);
          lcd.print(" ACCESS  DENIED ");
    
          delay (5000);
    
          Serial.println("\n*********Enter Passkey: *********");
          lcd.clear(); 
          lcd.setCursor(0, 0);
          lcd.print ("--SECURE ROOM!--");
          lcd.setCursor(0, 1);
          lcd.print ("PLEASE ENTER PIN");
          lcd.noBacklight();
    
          i=0;
    
        }
      }
    }
    

    Part 2: Test7_Tested_Tamper_Smoke_PIR_GSM.ino.ino

    #include <SoftwareSerial.h>
    SoftwareSerial mySerial(6, 5); //SIM800L Rx Tx
    char msg;
    String textMessage;
    
    int smokeA0 = A0;            // MQ135 Analog Input
    int led = 13;                // the pin that the LED is atteched to
    int priState = LOW;             // by default, no motion detected
    int TampSignal = 11;       // Tamper 5V Signal
    int sensorThres = 150; //Smoke Air Quality Threshold
    int pretamper = LOW;
    bool premotion = false;
    bool prefire = false;
    
    void setup() {
      pinMode(led, OUTPUT);      // initalize LED as an output
      pinMode(3, INPUT);    // initialize sensor as an input
      pinMode(TampSignal, OUTPUT); // Tamper Drive Signal as Output
      pinMode(10, INPUT);    // Tamper Alert
      Serial.begin(9600);        // initialize serial
      mySerial.begin(9600);
    }
    
    
    void SmokeSMS()
    {
      Serial.println("Warning!! Secure Room Fire Detected");
      mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
      delay(1000);  // Delay of 1000 milli seconds or 1 second
      mySerial.println("AT+CMGS=\"+xx\"\r");
      delay(1000);
      mySerial.println("Warning!! Secure Room Fire Detected");
      mySerial.println((char)26);// ASCII code of CTRL+Z
      delay(1000);
      mySerial.println("AT+CMGD=2");
    
      delay(5000);
    
      Serial.println("Warning!! Secure Room Fire Detected");
      mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
      delay(1000);  // Delay of 1000 milli seconds or 1 second
      mySerial.println("AT+CMGS=\"+xx2\"\r");
      delay(1000);
      mySerial.println("Warning!! Secure Room Fire Detected");
      mySerial.println((char)26);// ASCII code of CTRL+Z
      delay(1000);
      mySerial.println("AT+CMGD=2");
    
    }
    
    void MotionSMS()
    {
      Serial.println("Warning!! Secure Room Motion Detected");
      mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
      delay(1000);  // Delay of 1000 milli seconds or 1 second
      mySerial.println("AT+CMGS=\"xx\"\r");
      delay(1000);
      mySerial.println("PIN 3 HIGH");
      mySerial.println((char)26);// ASCII code of CTRL+Z
      delay(1000);
      mySerial.println("AT+CMGD=2");
    
      delay(5000);
    /*
      Serial.println("Warning!! Secure Room Motion Detected");
      mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
      delay(1000);  // Delay of 1000 milli seconds or 1 second
      mySerial.println("AT+CMGS=\"xx2\"\r");
      delay(1000);
      mySerial.println("Warning!! Secure Room Motion Detected");
      mySerial.println((char)26);// ASCII code of CTRL+Z
      delay(1000);
      mySerial.println("AT+CMGD=2");
    */
    
    }
    
    void TamperSMS()
    {
    
    
      Serial.println("Warning!! System Tamper Detected");
      mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
      delay(1000);  // Delay of 1000 milli seconds or 1 second
      mySerial.println("AT+CMGS=\"xx2\"\r");
      delay(1000);
      mySerial.println("Warning!! System Tamper Detected");
      mySerial.println((char)26);// ASCII code of CTRL+Z
      delay(1000);
      mySerial.println("AT+CMGD=2");
    
      delay(5000);
    
      Serial.println("Warning!! System Tamper Detected");
      mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
      delay(1000);  // Delay of 1000 milli seconds or 1 second
      mySerial.println("AT+CMGS=\"xx\"\r");
      delay(1000);
      mySerial.println("Warning!! System Tamper Detected");
      mySerial.println((char)26);// ASCII code of CTRL+Z
      delay(1000);
      mySerial.println("AT+CMGD=2");
    
    }
    
    
    void loop() {
      int analogSensor = analogRead(smokeA0);
      digitalWrite(TampSignal, HIGH); // Drive Signal Always High
      //val = digitalRead(3);   // read sensor value
      //  ReceiveMessage(); //System Recive Messages
      //Serial.print("Smoke Level:");
      //Serial.println(analogSensor);
      Serial.println(digitalRead(3));
      if (analogSensor > sensorThres  ) {           // check if the sensor is HIGH and Armed Status
        digitalWrite(led, HIGH);   // turn LED ON
        Serial.print("\nSmoke Alert....!!!");
        SmokeSMS(); // Smoke SMS Alert
        delay(100);                // delay 100 milliseconds
    
      }
    
      else if ( priState == LOW && digitalRead(3) == HIGH && digitalRead(7) == HIGH ) {
    
        digitalWrite(led, HIGH);   // turn LED ON
        Serial.println("Motion detected!");
        MotionSMS(); //Motion SMS
        priState = HIGH; // Previous Motion Status set to High
    
        delay(15000);
    
    
      }
    
    
      else if ( digitalRead(7) == HIGH && digitalRead(10) == LOW )
      {
    
        digitalWrite(led, HIGH);   // turn LED ON and Temper Alarm On
        Serial.println("One of the TIP Switches being Trigered");
        if ( pretamper == LOW ) {
          TamperSMS(); // Tamper SMS
          pretamper  = HIGH;
          delay(15000);
        }
    
      }
    
    
    
      else {
        digitalWrite(led, LOW); // turn LED OFF
        digitalWrite(11, HIGH); // Tamper Signal
        delay(200);             // delay 200 milliseconds
        pretamper = LOW;
        //Serial.print("\n.....Normal.....");
    
        if (priState == HIGH) {
          Serial.println("Motion stopped!");
          priState = LOW;       // update variable state to LOW
    
        }
      }
    
    }
    

    I am loosing my mind because I could not find the problem of getting false alert in every 55 minutes.

    I am very grateful any one can help.


  • Mod

    @vinod-amarathunga where is mySensors in your setup? This is a troubleshooting forum for MySensors.
    Don't crosspost your question over the internet: https://www.google.com/search?q=Vinod+Amarathunga+fssg



  • @vinod-amarathunga
    On PIN 7 ( Pin 3 from your Leonardo) = do you have constant "HIGH" output ?
    else if ( priState == LOW && digitalRead(3) == HIGH && digitalRead(7) == HIGH ) {



  • Simplify your code maximally.
    Remove PIR and UNO and connect D3 and D7 to correct levels.
    Remove SoftwareSerial and all SMS function.
    Only check serial debug messages for 4-5 hours run.
    If it will work, then little by little add other functions.
    My tip is problem with combination HW serial and SoftwareSerial.

    https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html



  • @badmannen

    Yes Correct, it's constant HIGH. unless any tamper switch triggered.



  • @yveaux

    Sorry my mistake. The question you posted is a solved one this is a separate issue. Can I know how to use MySensors ?



  • @vinod-amarathunga my question is more if you measured it?
    Also as mentioned. Simplify your code. Brake up your if statements and step it through or debug print it.

    Then you can go back to your fancy complicated if statements when you now it is working 😄



  • After tons of research found a way to solve this issue.

    This is common for ordinary PIR sensors when we use the 5V out from Arduino. Therefore we have to separate the power for PIR and the Arduino using a Opto Coupler and some transistors to smooth the signal. Now the solution works fine.

    I am posting here to help someone stuck with same issue.![alt text](PIR Isolation.jpg image url)


  • Mod

    @Vinod-Amarathunga If this solves your problem then why do you need the transistors & optocoupler? I would expect powering the PIR VIN from the separate 5V supply and tying the GND of the separate supply to the GND of the Arduino would work just as well. Did you try that?


Log in to reply
 

Suggested Topics

  • 3
  • 1
  • 5
  • 2
  • 6

15
Online

11.4k
Users

11.1k
Topics

112.7k
Posts