Why Does My PIR Sensor Detect Motion On Start Up

Not totally sure why my Arduino code is doing this, as near as I can tell in my set up I have the LED which is activated by the PIR sensors set as "LOW" in void setup? But it seems that even if I am positioned well behind the PIR sensors they seem to kick in right away, either that or it's the indicator (ledPin) that is coded a bit off, not sure??

I'll include a video link that demos the project and I'll attach the code as it's quite long, too long to post it in the message.

Video Demo Of Surveillance Mode

KnightRiderSurveillanceModule_pde.ino.ino (11.4 KB)

  pirValue = digitalRead(pirPin) || digitalRead(pirPin2);

This is STILL useless horsecrap.

PaulS:

  pirValue = digitalRead(pirPin) || digitalRead(pirPin2);

This is STILL useless horsecrap.

That does NOT help any :wink:

Knightriderguy:
That does NOT help any :wink:

You have been told that you need TWO state variables for TWO PIRs AND you have been told how to define and value them. YOU have chosen to ignore that advice. THAT does not help any.

PaulS:
You have been told that you need TWO state variables for TWO PIRs AND you have been told how to define and value them. YOU have chosen to ignore that advice. THAT does not help any.

I was NOT told HOW to declare two state variable.... being vague does NOT help either my friend :wink:

I was NOT told HOW to declare two state variable

You were.

http://forum.arduino.cc/index.php?topic=400837.0
See replies #22 and 27.

PaulS:
You were.

PIR Sensor Code Help - Programming Questions - Arduino Forum
See replies #22 and 27.

Yeah OK but I'm still NOT clear on what goes in set up and what goes in my loop?

Knightriderguy:
Yeah OK but I'm still NOT clear on what goes in set up and what goes in my loop?

I missed that one somehow... Bob did put both parts in there.... My bad :wink:

OK so I have changed the code to this (See Attached) But my ledPin still seems to show a motion sense on startup??

.... Not unless in setup I have to tell the pir pins to be LOW like the ledPin??

UPDATE: Nope, adding this to the void setup does nothing either?

digitalWrite(ledPin, LOW);
  digitalWrite(pirPin, LOW);
  digitalWrite(pirPin2, LOW);

KnightRiderSurveillanceModule_pde.ino.ino.ino (11.5 KB)

digitalWrite(pirPin, LOW);
digitalWrite(pirPin2, LOW);

A PIR is an input device. Why are you diddling with the pullup resistor on the pin?

You want to be looking for a change in the state of the PIRs. See the state change detection example. With the appropriate initial value for prevPIRStateOne and prevPIRStateTwo, you can do nothing until motion starts or until motion ends.

Do the PIR sensors go LOW when activated or HIGH?

If LOW, your logic here is wrong

  if (pirStateOne == HIGH || pirStateTwo == HIGH){
    digitalWrite(ledPin, HIGH);
  }else{
    digitalWrite(ledPin, LOW);
  }

Secondly, does it happen after resetting the board, or just after turning it on? I think some PIR sensors detect motion as they turn on, whether or not there is motion there.

DrAzzy:
Do the PIR sensors go LOW when activated or HIGH?

If LOW, your logic here is wrong

  if (pirStateOne == HIGH || pirStateTwo == HIGH){

digitalWrite(ledPin, HIGH);
 }else{
   digitalWrite(ledPin, LOW);
 }





Secondly, does it happen after resetting the board, or just after turning it on? I think some PIR sensors detect motion as they turn on, whether or not there is motion there.

Thanks DrAzzy,
I'm using these PIR sensor:
HC SR501

I'm pretty sure they go HIGH when motion is detected, I have tried it the other way and it made no sense so I'm pretty sure they go HIGH when motion is detected.
They seem to detect motion on startup even if there is none, but then after a short time they go off and then detect motion like normal.

Knightriderguy:
Not totally sure why my Arduino code is doing this, as near as I can tell in my set up I have the LED which is activated by the PIR sensors set as "LOW" in void setup? But it seems that even if I am positioned well behind the PIR sensors they seem to kick in right away, either that or it's the indicator (ledPin) that is coded a bit off, not sure??

Every PIR sensor needs a warming-up phase of some seconds after power-on. During this warming-up phase the sensor is signalling alert without reason, just because internal warming-up is not yet finished.

If you don't jave a PIR with an internal circuit that prevents alerting during warming-up time the behaviour is pretty normal.

jurs:

Knightriderguy:
Not totally sure why my Arduino code is doing this, as near as I can tell in my set up I have the LED which is activated by the PIR sensors set as "LOW" in void setup? But it seems that even if I am positioned well behind the PIR sensors they seem to kick in right away, either that or it's the indicator (ledPin) that is coded a bit off, not sure??

Every PIR sensor needs a warming-up phase of some seconds after power-on. During this warming-up phase the sensor is signalling alert without reason, just because internal warming-up is not yet finished.

If you don't jave a PIR with an internal circuit that prevents alerting during warming-up time the behaviour is pretty normal.

Ahh I see, thanks for that, I started experimenting with a delay but not sure how long to make my delay time?

//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;


void setup()
{
  //PIR Sensor
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
  pinMode(pirPin2, INPUT);

  digitalWrite(ledPin, LOW);

  //give the sensors some time to calibrate
  for(int c = 0; c < calibrationTime; c++){
   delay(50);
  }
  //End PIR Sensor
  for(int c = 0; c < calibrationTime; c++){
   delay(50);
  }

Why not simply use a single longer delay rather than a for loop of shorter ones ?

UKHeliBob:

  for(int c = 0; c < calibrationTime; c++){

delay(50);
  }


Why not simply use a single longer delay rather than a for loop of shorter ones ?

I guess but a small part of the issue was that when I activate the Surveillance Mode box with the LED patterns on it I wanted the first sequence to start up right away, just so you know the box has been activated, but the delay time makes it wait for a little too long, If I could have the first sequence start right away until the calibration was done that might work.

Does this make more sense in the void loop?

void loop()                     // run over and over again
{
//give the sensors some time to calibrate
  for(int c = 0; c < calibrationTime; c++){
    //Play LED Sequence 1 While we wait for the Calibration Time
    sequence = 1;
    delay(1000);
  }
  
  //PIR Sensor
  pirStateOne = digitalRead(pirPin);
  pirStateTwo = digitalRead(pirPin2);

  if (pirStateOne == HIGH || pirStateTwo == HIGH){
    digitalWrite(ledPin, HIGH);
  }else{
    digitalWrite(ledPin, LOW);
  }
  
  //End PIR Sensor
  
                             
 // check if PIR Status LED is Lighted
        val = digitalRead(ledPin);
       
        if(val == HIGH) 
        {
           sequence = 2;
        }
        else
        {
          sequence = 1;
        }
//give the sensors some time to calibrate

In loop()? No!. A PIR sensor does not take time to calibrate every time you read it.

  pirStateOne = digitalRead(pirPin);
  pirStateTwo = digitalRead(pirPin2);

Why are pirStateOne and pirStateTwo global? Why does pirPin2 have a number, while pirPin does not?

  if (pirStateOne == HIGH || pirStateTwo == HIGH){
    digitalWrite(ledPin, HIGH);
  }else{
    digitalWrite(ledPin, LOW);
  }

Still seems to me like you want to do this if either pin has BECOME motion-detected, rather than IS motiondetected.

val = digitalRead(ledPin);

You just set it! Remember what you set it to.

PaulS:

//give the sensors some time to calibrate

In loop()? No!. A PIR sensor does not take time to calibrate every time you read it.

  pirStateOne = digitalRead(pirPin);

pirStateTwo = digitalRead(pirPin2);



Why are pirStateOne and pirStateTwo global? Why does pirPin2 have a number, while pirPin does not?



if (pirStateOne == HIGH || pirStateTwo == HIGH){
   digitalWrite(ledPin, HIGH);
 }else{
   digitalWrite(ledPin, LOW);
 }



Still seems to me like you want to do this if either pin has BECOME motion-detected, rather than IS motiondetected.
You just set it! Remember what you set it to.

This is why:

int pirPin = 12; // Input for HC-S501
int pirPin2 = A1;

To identify the pins the sensors are on.

And yes if any of the sensors detest motion then turn on the LED, when the LED is active the sequence changes to sequence 2 and back to 1 if none is detected / LED is off....

OK so I have changed the code to this (See Attached) But my ledPin still seems to show a motion sense on startup??

Well, if your PIRs ping when they are powered on, then either your sketch needs to deal with it or your electronics do. As Don R said: you go to war with the army you have, not with the army you'd like to have, or the army that you might have at a future time.

This is why:

That does NOT explain the naming convention, which is what I questioned.