简体   繁体   中英

Why 2nd PIR sensor is always HIGH?

I an getting a constant HIGH from 'inputPintwo' on the serial monitor. When 'inputPin' goes HIGH the relay is triggered and works properly because 'inputPintwo' is also HIGH (all the time).

I have a Very similar setup to: 2 PIR motion sensors +Arduino

I am not using pin 0 or 1 like the above answered question. I have replaced the sensor with a different one, in case it was bad hardware. I also unplugged the sensor and it still reads HIGH. The jumper is on retriggering on both sensors.

    int ledPin = 13;
    int inputPin = 2;
    int inputPintwo = 4;
    int pirState = LOW;
    int val = 0;
    int valtwo = 0;
    #define RELAY1  7

    void setup() {
      pinMode(ledPin, OUTPUT);
      pinMode(inputPin, INPUT);
      pinMode(inputPintwo, INPUT);
      pinMode(RELAY1, OUTPUT);
      Serial.begin(9600);
    }

    void loop(){
      val = digitalRead(inputPin);
      valtwo = digitalRead(inputPintwo);

      if (val == HIGH && valtwo == HIGH) {
        digitalWrite(ledPin, HIGH);
        if (pirState == LOW) {
          Serial.println("Motion detected!");
          pirState = HIGH;
          Serial.println("Light ON");
          digitalWrite(RELAY1,1);
          delay(500);
          digitalWrite(RELAY1,0);
          delay(500);
          digitalWrite(RELAY1,1);
          delay(500);
          digitalWrite(RELAY1,0);
          delay(500);
          digitalWrite(RELAY1,1);
          }
        }
      else {
        digitalWrite(ledPin, LOW);
        if (pirState == HIGH){
          Serial.println("Motion ended!");
          digitalWrite(RELAY1,0);
          pirState = LOW;
          Serial.println("Light OFF");

        }
      }
    }

I expect both sensors to go HIGH only when motion is detected, which will cause the relay to go on and off several times, then stay on until the timer runs out on the sensors.

To identify the problem I recommend you to start with checking the hardware. You will need voltmeter/multimeter.

  1. Double check if you are interfacing the sensor properly (check datasheet). Didn't you forget to connect eg the pull-down resistors?
  2. Check power supply voltage on sensors – is the voltage within manufacturer specifications?
  3. Check breadboard connections if you are using one.
  4. Check sensor output behaviour (voltage), if there is or is not a movement. Is the voltage constant or not? Constant voltage means that PIR sensor is NOT working properly. Before performing of this test disconnect output from Arduino input.

If everything seems OK or you do not have voltmeter, try to disconnect the PIR sensor and connect a wire between Arduino pin 4 and ground. Does digitalRead(inputPintwo) return LOW? If yes, you know that reading of the pin state works fine.


Below please see some recommendations related to your code:

  • use #define directive or static const int variable type to define Arduino pins as you do it with relay output pin RELAY1.

Example:

#define LED_PIN 13
#define INPUT_PIN 2
#define INPUT_PINTWO 4

or

static const int ledPin = 13;
static const int inputPin = 2;
static const int inputPintwo = 4;
  • In your case, where you are only interested in digital value (LOW/HIGH), use built pull-up resistor on the input pins. Thus the log. voltage level on the floating input pin is defined (HIGH). If you do not use pull-up resistors, voltage can be either log. 0 (LOW) or log. 1 (HIGH), what can lead to strange program/state machine behaviour

To activate pull-up resistors in the input pins, use

pinMode(inputPin, INPUT_PULLUP);
pinMode(inputPintwo, INPUT_PULLUP);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM