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!