1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PIR with Arduino - detects only first motion

Discussion in 'Programming/Internet' started by Shijil T, Oct 8, 2018.

  1. Shijil T

    Shijil T Guest

    The following is an Arduino sketch for 3 PIR sensors. Two PIRs can activate 1 buzzer and show status in an LCD. The third PIR activates a relay.

    The problem is only the first motion gets detected and the "L" indicator stays on. After that there is no activity.

    The same Arduino Uno board is working fine with other sketches. PIR sensors are also working. The sketch seems to be working on simulation as well.

    Link to simulation

    "L" LED is the LED close to the 13th pin. above the TX,RX LEDs. The PIRs are set for the lowest time, sensitivity medium. The issue exists even without connecting the PIRs/relay. There is one beep from the buzzer, it happens 4-5 seconds after turning the board on, with that the "L" led goes on - forever.

    The LCD also has an issue, it shows filled blocks on the first row, nothing else.

    I tried connecting only one PIR and 1 buzzer to the board.

    What is wrong?

    #include <LiquidCrystal.h>

    unsigned long time;
    LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
    int pir1Pin =9;
    int pir2Pin =10;
    int pir3Pin =11;
    int buzzerPin =8;
    int relayPin = 12;
    int backlightPin = 13;
    long previousMillis = 0;
    long interval = 1000;

    void setup() {
    lcd.begin(16, 2);
    pinMode(pir1Pin, INPUT);
    pinMode(pir2Pin, INPUT);
    pinMode(pir3Pin, INPUT);
    pinMode(relayPin, OUTPUT);
    pinMode(buzzerPin, OUTPUT);
    pinMode(backlightPin, OUTPUT);
    digitalWrite(relayPin,HIGH);
    delay(1000);
    digitalWrite(relayPin,LOW);
    Serial.begin(9600);
    }

    void loop() {
    if(digitalRead(pir1Pin)== HIGH) {
    digitalWrite(backlightPin,HIGH);
    lcd.setCursor(0, 0);
    lcd.print("Pir 1 Active");
    beep();
    } else {
    lcd.setCursor(0, 0);
    lcd.print("Pir 1 is off");
    digitalWrite(backlightPin,LOW);
    }
    if(digitalRead(pir2Pin)== HIGH) {
    digitalWrite(backlightPin,HIGH);
    lcd.setCursor(0, 1);
    lcd.print("Pir 2 Active ");
    beep();
    } else {
    lcd.setCursor(0, 1);
    lcd.print("Pir 2 is off");
    digitalWrite(backlightPin,LOW);
    }
    if(digitalRead(pir3Pin)== HIGH) {
    while(digitalRead(pir3Pin)==HIGH) {
    digitalWrite(relayPin,HIGH);
    }
    } else {
    digitalWrite(relayPin,LOW);
    }
    }

    void beep() {
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    for (int i=0; i<500; i++) {
    digitalWrite(buzzerPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(buzzerPin, LOW);
    delayMicroseconds(500);
    }
    }
    }

    Login To add answer/comment
     

Share This Page