PIR sensor

My led always light up and the com3 always show me the motion detected.
The code that I use to test the pir sensor is

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

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

boolean lockLow = true;
boolean takeLowTime;

int LED= 4;
int PIR =2;

/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(PIR, INPUT);
pinMode(LED, OUTPUT);
digitalWrite(PIR, LOW);

//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}

////////////////////////////
//LOOP
void loop(){

if(digitalRead(PIR) == HIGH){
digitalWrite(LED, HIGH); //the led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}

if(digitalRead(PIR) == LOW){
digitalWrite(LED, LOW); //the led visualizes the sensors output pin state

if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}

Must turn the 'time' pot fully anti-clockwise, and leave it there...
Leave the 'sensitivity' pot in the middle (where it originally was).

Now the code is in control of the timing instead of the PIR sensor.
Leo..

After i adjusted, it doesnt work for me the led always light up and the com 3 only show motion detected at 30 sec.

Looks like you have the sensitivity pot on minimum, and the time pot in the middle.
Read post#1 again.

The code uses pin2 for the PIR, but the PIR is connected to pin10?
So what is it.

Test PIR with this simple sketch.
LED should go off just a few seconds after movement has stopped.
Leo..

const byte pirPin = 10;
const byte ledPin = 13; // onboard LED

void setup() {
}

void loop() {
  digitalWrite (ledPin, digitalRead (pirPin)); // LED follows PIR output
  delay(100);
}

Sorry the coding I provided is not the latest for the pin.
I have changed the led to pin 4 and my pir to pin 10
and i have followed the suggestion you provided in the fist post.
and the coding you provided me to test the pir sensor.
Yes. the problem is going same where the led bulb is still on where we can know that the pir sensor is always detected the motion but there is no motion there.

Is it is the problem for the coding? or the pir sensor spoil(I just bought today)? or the connection of wire

//coding

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

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

boolean lockLow = true;
boolean takeLowTime;

const byte ledPin= 4;
const byte pirPin =10;

/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);

//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}

////////////////////////////
//LOOP
void loop(){

if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}

if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state

if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}

Sorry I have solved with the problem. (I dont know where i have adjusted)
And the com3 have showed the second for motion detected and the motion ended. But, the problem having now is i waving in front the pir sensor the bulb will light up and then will off. It is the connection part got error? or the arduino board have spoiled?

Never use a LED without current limiting resistor.
Just plugging in a LED. as you do, will eventually fry the pin (or the LED if you're lucky).

It seems you want to light a LED for a certain amount of time after motion is detected.
And display when motion has started/stopped.
I would re-write the code according the "StateChangeDetection" example that comes with the IDE.

Try this (untested) sketch.
Leo..

const byte pirPin = 10;
const byte ledPin = 13;
byte calibrationTime = 10; // 10-60 sec
boolean pirState, lastPirState;
unsigned long ledStartTime;
const unsigned long burnTime = 60000; // LED 'on' time in milliseconds

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Serial.print("calibrating sensor ");
  for (int i = 0; i < calibrationTime; i++) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println(" done\nSENSOR ACTIVE");
}

void loop() {
  pirState = digitalRead(pirPin); // read pin
  if (pirState != lastPirState) { // if changed
    if (pirState) { // if HIGH
      ledStartTime = millis();
      Serial.print("motion detected at ");
      Serial.print(millis() / 1000);
      Serial.println(" sec");
    }
    else { // if LOW
      Serial.print("motion ended at ");
      Serial.print(millis() / 1000);
      Serial.println(" sec");
    }
  }
  lastPirState = pirState; // update
  if (millis() - ledStartTime < burnTime) digitalWrite(ledPin, HIGH);
  else digitalWrite(ledPin, LOW);
}

I think you need to change the motion sensor's configuration. See this instruction to know about configuration of motion sensor and how to use ut

IoT_hobbyist:
I think you need to change the motion sensor's configuration.

Usually not needed. Just set the time delay to minimum, so the code is the only one in control of time.

IoT_hobbyist:
See this instruction to know about configuration of motion sensor and how to use ut

First thing I noticed on that page is the incorrect explanation of the sensitivity pot.
That pot does NOT control sensitivity/distance (as they explained), but the time it takes for the sensor to react.
Sort of a false positive eliminator.
Leo..

i have the same issue, also got mine yesterday. could it have been a faulthy badge

I was tried many way. (add resistor) And the code you shared to me.
Last night im still able to view the com 3 changes (the motion detected and not detected).
But this morning i try again the com 3 doesnt show anythings at stuck at the calibrating there, and he light bulb keep light up.