Problem with HC-SR501 PIR Motion Sensor

So I uploaded this code for a HC-SR501 PIR Motion Sensor on an Arduino Uno:

int ledPin = 13;                // LED 
int pirPin = 2;                 // PIR Out pin 
int pirStat = 0;                   // PIR status
void setup() {
 pinMode(ledPin, OUTPUT);     
 pinMode(pirPin, INPUT);     
 Serial.begin(9600);
}
void loop(){
 pirStat = digitalRead(pirPin); 
 if (pirStat == HIGH) {            // if motion detected
   digitalWrite(ledPin, HIGH);  // turn LED ON
   Serial.println("Hey I got you!!!");
 } 
 else {
   digitalWrite(ledPin, LOW); // turn LED OFF if we have no motion
 }
}

And when I open the Serial Monitor it keeps on printing “Hey I got you!!!” uncontrollably even when it doesn’t detect anything. Also the LED on pin 13 also stays on, oblivious of the fact that the Motion Sensor isn’t actually detecting anything.

I’ve connected the

  • Gnd to Gnd
  • Output to Digital pin 2
  • VCC to 5V

I can’t figure out what’s wrong, maybe someone with more experience can? :confused:
Oh, and by the way, I’m using the Arduino IDE 1.8.12

The link for where I got the code is PIR Motion Sensor: How to Use PIRs w/ Arduino & Raspberry Pi - Arduino Project Hub

A little help would be appreciated. Thanks in advance!!!

PS: Sorry if someone’s asked this question before; I couldn’t find this particular problem posted anywhere.

perhaps: pinMode(pirPin, INPUT_PULLUP ) ;

instead of: pinMode(pirPin, INPUT ) ;

Edit: perhaps not. I just found this: https://components101.com/hc-sr501-pir-sensor It appears that the sensor output is HIGH when triggerd, LOW otherwise. I wrongly assumed it to be open collector output.

Edit2. Remove the sensor. Put a 10K pull down resistor between pin 2 and ground. Touch a wire between pin 2 and Vcc to simulate the action of the sensor detecting something.

6v6gt: Edit2. Remove the sensor. Put a 10K pull down resistor between pin 2 and ground. Touch a wire between pin 2 and Vcc to simulate the action of the sensor detecting something.

Should I connect the resistor with the Gnd of Arduino or the Motion Sensor. Or Vcc of the Motion Sensor with Pin 2? But that would make no sense since the motion sensor is not getting any power.

Could anybody send, like, a diagram or something?

PS: I'm a newbie and as you may have noticed this is my first post so -- Cut me some slack?

I see my edit 2 may have been confusing for you. The code looks right so the problem is probably the sensor. But to confirm that, the idea is to take the sensor completely out of circuit. The action of the sensor will be simulated by using a wire to momentarily apply 5volts to the Arduino digital pin 2 where the sensor would have been connected. But you also then need a resistor (say 10k) between the Arduino ground and its digital pin 2.

My edit should have been better as: Remove the sensor. Put a 10K pull down resistor between pin Arduino 2 and ground. Touch a wire between Arduino pin 2 and the Arduino 5 volt pin to simulate the action of the sensor detecting something.

Indeed, the code does work (the LED on pin 13 turns on and the serial monitor prints "Hey I got you!!!") when i simulate the Motion Sensor but how to integrate the Motion Sensor itself with the hardware? Shouldn't the code work with the Motion Sensor as well?

OK. Good. You appear to have confirmed that the code does indeed work as it should. The conclusion is that either the motion sensor is faulty or, more likely, there is something about its behaviour which you have not understood.

To integrate the sensor with the hardware, remove the resistor and restore the connections which you described in the OP.

Is the sensor the same as the link in #1 ?

Experiment with the position of the jumper and the time and sensitivity controls. Also, these PIR sensors need some stabilisation time after being switched on. Probably try setting the time as short as possible and make sure the sensor is well covered for some minutes before uncovering it to see the results.

Seems like I've got it working! It will take some figuring out to tune the potentiometers on the sensor but hopefully I can get it done! Thanks for the help!!!

Normally the first thing you do with HC-SR501 sensors is to turn the 'time' pot fully anticlockwise for the shortest 'on' time (~3sec). Then the Arduino can take care of timing. Leave the 'sens' pot in the middle. It does NOT control distance.

The sketch is better written as a state machine. Do something if the PIR output CHANGES. Wrote a quick example (untested).

Be patient, and sit very still. These sensors are very sensitive. And not only to human heat sources. Leo..

const byte ledPin = 13; // build-in LED
const byte pirPin = 2; // PIR output
boolean pirState, lastPirState;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  pirState = digitalRead(pirPin);
  if (pirState != lastPirState) { // if changed
    if (pirState) { // if LOW to HIGH
      digitalWrite(ledPin, HIGH);
      Serial.println("Hey I got you!!!");
    }
    else { // if HIGH to LOW
      digitalWrite(ledPin, LOW);
      Serial.println("All quiet");
    }
    lastPirState = pirState; // remember
  }
}

Wawa: The sketch is better written as a state machine. Do something if the PIR output CHANGES. Wrote a quick example (untested).

I used the code and it worked! Thanks a lot!

Quick Question: How do I change the range and time of the sensor using Arduino?

Edit: I know this is not related to the topic but I couldn't find the answer online - Are 2 3.7V 1000mAh lithium-ion batteries sufficient to power an Arduino Uno and how long will they last?

Can't change the range. It's about 5-7metres with that Fresnel lens. You can only influence the 'ignore' time with the sensitivity pot, to reduce false triggers.

Burn time of the LED, or an added external light, is just another few lines of code. You should go through all the examples that come with the IDE, and pay special attention to the BlinkWithoutDelay example.

Two LiPo batteries in series (~7.4volt) can be connected to the DC socket. The Uno draws about 50mA, so theoretically about 20 hours on two 1000mAh cells. Can ignore the PIR sensor in the calculations. It draws less than 0.1mA. Leo..