LED turning on, when it should be off

Evening all,

Not sure if this is done to my coding or my circuit set up.

I've got a PIR set up in a circuit so when it detects motion an LED comes on and then goes off when no motion detected.

Now sometimes it works absolutely fine on/off as it's meant to. Other times the LED stays on and I'm not to sure why. Any assistance would be greatly appreciated. Please be gentle, coding is not my day job I just do this as a hobby/fun. It's a little messy.

const int PIR = 2;
const int ledPIR =3;
int ledPIRflag=0;

void setup() {
pinMode(PIR,INPUT);
pinMode(ledPIR,OUTPUT);
digitalWrite(ledPIR,LOW);
}

void loop() {
if (digitalRead(PIR)==LOW) {
if (ledPIRflag==0) {

   ledPIRflag=1;                  
   digitalWrite(ledPIR,HIGH);     
  }                              
else {      
   ledPIRflag=0;                  
   digitalWrite(ledPIR,LOW);      
}
}

Try:

pinMode(PIR,INPUT_PULLUP);

We really need to see the schematic of the PIR.

Is that 10 Ω ?

What is pin 7 and 8 ?

What Arduino are you using ?

The PIR is at work, I'll get details in the morning.

That was meant to be 100Ω
Should be pins 2 & 3 (sorry forgot to change them on the drawing)
Arduino is Mega 2560

FYI:

The switch S3 connection in the above schematic is recommended wiring for switches.

You turn on the internal pull-up resistor as shown and look for a LOW on the input pin for switch (PIR) closure.

Thanks Larry, that's very helpful!!!

I'll change them around and see what the outcome is.

Assumption here is the PIR is a dry contact; need to see the PIR schematic.


Maybe (with the Arduino disconnected) check with a DMM to see what the output is, contact or voltage or open collector ?

So I measured across the contacts and the output is dry. Simple open or close with no voltage.

That’s ideal then.