Pir sensors don't detect motion

Hello.
For my first Arduino projects I want to have LEDs lit on my stairs when motion is detected.
I attached 2 PIR HC-SR501 sensors to my arduino. But no motion is detected, value is always LOW/0.
I tried 3 different sensors, all detect no motion.
I cheked the voltage, it is 4,9V.
I tried turning the pots in different positions.
All without result. Can they be all broken or is it my code?

int timer = 200; int timerUit = 5000; const int pirBeneden = 2; const int pirBoven = 3; int pirStatusBeneden = LOW; int pirStatusBoven = LOW;

void setup() {
Serial.begin(9600);
pinMode(pirBeneden, INPUT);
pinMode(pirBoven, INPUT);
for (int thisPin = 2; thisPin < 14; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}

void loop() {
pirStatusBeneden = digitalRead(pirBeneden);
pirStatusBoven = digitalRead(pirBoven);

if (pirStatusBeneden == HIGH) {
for (int thisPin = 2; thisPin < 14; thisPin++) {
digitalWrite(thisPin, HIGH);
delay(timer);
}
delay(timerUit);
for (int thisPin = 2; thisPin < 14; thisPin++) {
digitalWrite(thisPin, LOW);
}
}

if (pirStatusBoven == HIGH) {
for (int thisPin = 2; thisPin < 14; thisPin++) {
digitalWrite(thisPin, HIGH);
}
delay(timerUit);
for (int thisPin = 2; thisPin < 14; thisPin++) {
digitalWrite(thisPin, LOW);
}
}

delay(500);
Serial.print("Beneden: ");
Serial.println(pirStatusBeneden);
Serial.print("Boven: ");
Serial.println(pirStatusBoven);
}
<code>

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

You tried to use code tags but got it wrong

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

If having doubts about your sensor, you can measure voltage of output pin. It should switch between 3.3V and 0V from motion.
Also, use some simple demo-code to start with...

You are changing the PIR pins to OUTPUT after initially setting them to INPUT

Thanks, 6v6gt.
That solved the problem.
I have to start the for loop with 4 and not 2.
Just kept overlooking this. Thank you so much!

Even when using a number of pins with contiguous numbering I find it just as easy to put the pin numbers in an array and iterate through that to set their pinMode()s and it avoids problems such as yours

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.