PIR Motion Sensor with Arduino Uno


I am trying to get a PIR Motion sensor working on my Arduino Uno. I wrote the code as far as I could get, but at one point,I don’t know how to get the Arduino working:

If motion is detected, the PIR Motion Sensor will send a HIGH signal to the Arduino. If no motion is detected, it sends a LOW signal to the Arduino. What I want to create, is a programm that turns on a LED when I come in to the room, but this LED needs to be turned off, about 3 minutes after the signal has been low. (I have to be out of the room for around 3 minutes, when the light turns off).

I wrote this so far… Thank for your help! It’s all about the last 2 sentences I guess…

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

int previousMillis;
int pirPin = 7;
int ledPin = 13;
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);

void setup()
lcd.begin (16,2); // Type scherm.
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);

void loop()
lcd.home ();
int sensor = digitalRead(pirPin);

if(sensor == HIGH){
digitalWrite(ledPin, HIGH);


if((millis() - previousMillis) >= 1000 && sensor == LOW){

digitalWrite(ledPin, LOW);}}

Hi bjorn1906,
Well I think you need to tell us a bit more!! What PIR sensor, their not all the same or work the same. I got one of the cheap Chinese one's to work, that has 2 pots to control trigger sensitivity and ON time, and it took me some time to get it right.

Did you get any info with it, perhaps the output goes LOW, some do?

  if((millis() - previousMillis) >= 1000 && sensor == LOW)

What value does previousMillis have? where do you define it?? were you talking of 3 seconds!

But until you tell us more, we're just guessing.



Another approach... this code not TESTED, but should be mostly okay, apart from typos. It assumes you've made subroutines for "boPIRSeeingMotion" (returns true or false), "LightOn" (turns light on), "LightOff" (turns light off).

Oddly enough, I've just finished doing a project example quite like this. For a "finished and tested", but much longer "answer", see...

int iLoopsSinceNoMotion=0;


if (boPIRSeeingMotion())
   iLoopsSinceNoMotion=0;//keep putting back to zero
 }//end of then... no ; here.

else //Deal with PIR not seeing motion
   if (iLoopsSinceNoMotion==1000) {LightOff;};
}//end of else

}//end of loop()