Thursday, February 28, 2013

PIR sensor


General Description
The PIR (Passive Infra-Red) Sensor is a pyroelectric device that detects motion by measuring changes in
the infrared levels emitted by surrounding objects. This motion can be detected by checking for a high
signal on a single I/O pin.

Calibration
The PIR Sensor requires a ‘warm-up’ time in order to function properly. This is due to the settling time
involved in ‘learning’ its environment. This could be anywhere from 10-60 seconds. During this time
there should be as little motion as possible in the sensors field of view.

This is the explanation of program written in pir_test.ino
PIN connection
Vcc(PIR)--3.3V(Arduino)
GND(PIR)--GND(Arduino)
OUT(PIR)--pin7(arduino)
by:moin
creaed :@date 28-02-2013 @time 6:22


Since PIR, sends 'HIGH' signal for 3sec(vary with devices), just after sending a LOW signal for 2sec(vary with devices),
to check motion, this phenomenon of PIR is interpreted as positive motion lots of time by the compiler.
And hence cause wrong results many times. to over come this problem ,we need to specify a time condition,
so that if and only if that condition is satisfied, our compiler should consider that signal as a positive motion

to this we first need to calculate time at which PIR is sending 'LOW’ signal,
to do this I have written a if loop as shown below ,
this program calculates the time at which PIR is LOW, and prints the value to serial com .
                                   


                                    if(digitalRead(pir)==LOW){
                                      offtime=millis();//stores time at which pir pin turned low
                                      digitalWrite(led,LOW);//pir status is 
                                      Serial.print("\n offtime");
                                      Serial.println(offtime);
                                      delay(100);
                                    }


to this we first need to calculate time at which PIR is sending 'HIGH’ signal,
to do this I have written a if loop as shown below ,
this program calculates the time at which PIR is 'HIGH’, and prints the value to serial com .

                                    else if(digitalRead(pir)==HIGH){
                                      ontime=millis()-offtime;//store's time at which pir was high**
                                     digitalWrite(led,HIGH);//pir status on led
                                      Serial.print("\n ontime:");
                                      Serial.println(ontime);
                                      delay(200);
                                        }
imp:: ontime=millis()-offtime;
      here int above statement ontime is calculated by [millis()-offtime] formula,
            because millis(),gives the total time taken by compiler since the program is started.
            so if you can see in the first if loop , we are using [offtime=millis();]millis();direct to store the time at which
            PIR signaled LOW, so by applying simple math’s we can understand that the above formula can give us ontime.


Now after calculating time for which PIR is HIGH, we need check if PIR is high for minimum 5sec to confirm motion,

                                     if(ontime>condition){
                                      Serial.print("\n motion sensed");
                                      delay(1000);
                                    }
                                   
                                    if(ontime<condition){
                                      Serial.print("\n motion not sensed");
                                      delay(1000);
                                     }
Condition is initialized to 5000;
                                    long condition=5000;
this program is working as expected. and here is the program copy and paste it to arduino platform
//@dat 28-feb-2013
//by moin
*************//working sketch for pir*************************************************
#define pir 7
#define led 12
//int pir_state =LOW;
long offtime=0;//make sure it is initialized to zero**
unsigned long ontime;
long condition=5000;//should saty high to confirmmotion**
void setup(){
  pinMode(pir,INPUT);//make pir pin as input to micro controller
  pinMode(led,OUTPUT);//make led to show pir status
  Serial.begin(9600);
  //time requierd by PIR to callibrate
  Serial.print("calibrating sensor\n ");
    for(int i = 0; i < 30; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE\n");
    delay(50);
}
void loop(){
  //check if pir pin is low and store the time at which it is low

  if(digitalRead(pir)==LOW){
      offtime=millis();//stores time at which pir pin turned low
      digitalWrite(led,LOW);//pir status is  
     // Serial.print("\n offtime");
      //Serial.println(offtime);
      delay(100);
    }
    
    //check if pir pin is high and store time 
    else if(digitalRead(pir)==HIGH){
    ontime=millis()-offtime;//store's time at which pir was high**
    digitalWrite(led,HIGH);//pir status on led
    //Serial.print("\n ontime:");
   // Serial.println(ontime);
    delay(200);
  }
  //check if led was high for 5 sec**
  if(ontime>condition){
    Serial.print("\n motion sensed");
    digitalWrite(led,HIGH);
    delay(1000);
  }
   if(ontime<condition){
    Serial.print("\n motion not sensed");
    delay(1000);
  }
}
*********************************************************************************  
    


Thank you for Reading.

No comments:

Post a Comment