2
votes

I am testing out the HC-SR501 PIR sensor on arduino. I tried a simple code tutorial online

int buzz = 13;
int pir = 2;

int value = 0;
int pirState = LOW;

void setup() {
  pinMode(buzz, OUTPUT);
  pinMode(pir, INPUT);
  Serial.begin(9600);
}

void loop() {
  delay(5000);
  value = digitalRead(pir);
  if (value == HIGH) {
    digitalWrite(buzz, HIGH);
    if (pirState == LOW) {
      Serial.println("Motion Detected!");
      pirState = HIGH;
    }
  } else {
    digitalWrite(buzz, LOW);
    if (pirState == HIGH){
      Serial.println("Motion Ended!");
      pirState = LOW;
    }
  }
}

This works, however, I'm trying to initialize it to a LOW output. When I first turn on the board, it initially gives me a high output, and so the buzzer activates instantly, even though I placed it away from myself. The serial prints out Motion Detected. I tried adding a delay, however it still gives out a HIGH output afterwards. Anyone knows how to solve this?

Thank you!

1
Does it go LOW eventually, and if so, how long does that take?ocrdu
@ocrdu yes it goes LOW eventually, after the set delay time on the PIREcho

1 Answers

1
votes

The pinMode sets the pin as an output, but the default state is LOW, so there should be no problem with it.

However, pin 13 is wired to onboard LED. And the onboard LED is also used by bootloader to signal its activity after the reset. You should check other pins but 13.