PIR light switch with 2 sensors?

Greetings All,

I am using an ATTiny85 to run a PIR light switch. I've got the coding down (I think) for one sensor, but can anyone tell me what I need to add so it can be triggered by 2 different sensors in an either/or setup?

#define PIR0_PIN 0
#define PIR1_PIN 1
#define RELAY_PIN 2

void setup() {
  pinMode(PIR0_PIN, INPUT);
  pinMode(PIR1_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
}

void loop() {
  if (digitalRead(PIR0_PIN) == HIGH) {
    digitalWrite(RELAY_PIN, HIGH);
    delay(800);
    } else {
    digitalWrite(RELAY_PIN, LOW);
  }

}

Have you tried duplicating the code for the second sensor?

Please indicate your desired outcome:

I just meant that the Relay Pin goes high (for 800ms) if either of the PIR Pins go high. It doesn't need to be both. It's for a set of stairs lighting. The stairs aren't straight up and down, so I need a sensor at the bottom and one at the top.

Can be done many ways, like simply copying your condition for the second PIR:

 else if (digitalRead(PIR1_PIN) == HIGH) {
    digitalWrite(RELAY_PIN, HIGH);
    delay(800);
    } else {
    digitalWrite(RELAY_PIN, LOW);
  }

and inserting that between first condition and else.

Once the delay(800) is done, the loop restarts and if the PIR output is still high (very likely), the relay will remain activated.

Your code keeps the relay ON as long as the PIR output is HIGH, which is typically several seconds (depending on its builtin setup potentiometer). A simple test will confirm this.

If what you want is the relay to turn on only when a presence is detected and the PIR goes from LOW to HIGH, for just 800mS, you have to modify the code to detect when this change happens

Adding a second PIR is very simple, so I would focus on fixing this first.

That's a very specific, and very short, time!

Sorry gang! 800 is a typo. I meant 8000ms. The sensors, according to the documentation, have a high time of 3.2 seconds. So I think I'm good on the delay time.

How's this look?

#define PIR0_PIN 0
#define PIR1_PIN 1
#define RELAY_PIN 2

void setup() {
  pinMode(PIR0_PIN, INPUT);
  pinMode(PIR1_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
}

void loop() {
  if (digitalRead(PIR0_PIN) == HIGH) {
    digitalWrite(RELAY_PIN, HIGH);
    delay(8000);
  }else if (digitalRead(PIR1_PIN) == HIGH) {
    digitalWrite(RELAY_PIN, HIGH);
    delay(8000);
    } else {
    digitalWrite(RELAY_PIN, LOW);
    }
}

Try it. :wink:

#define PIR0_PIN 0
#define PIR1_PIN 1
#define RELAY_PIN 2

void setup() {
  pinMode(PIR0_PIN, INPUT);
  pinMode(PIR1_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
}

void loop() {
  if (digitalRead(PIR0_PIN) == HIGH or digitalRead(PIR1_PIN) == HIGH) {
    digitalWrite(RELAY_PIN, HIGH);
    delay(8000);
    digitalWrite(RELAY_PIN, LOW);
  }
}

Like you probably found out, your code (even for the first PIR) doesn't work.
That's because after delay you do nothing. Instead you need to return the relay to LOW state.

1 Like

Did you choose PIR sensors that cannot both see the heat source at the same time?

I'm sure they could, but there is a full 180 degree turn in the stairs separated by drywall. So due to obstructed view, the lower sensor cannot see what the upper sensor sees and vice versa.

That's totally ok. Arduino can check 100 PIR sensors in a fraction of second.
Your code is ok if you just write the relay LOW after delay.

ok, then you need to make your code remember which sensor turned on first and you do that be adding code to each sensor to turn on it's memory bool if the other sensor's memory bool is not on.
Then when you turn the light off, reset each memory bool to false.