Sunday, September 25, 2011

Motion Detection with Digital Infrared Motion Sensor (PIR)

The usefulness of this sensor allows you to detect motion. They often refer to this as a PIR sensor, "Passive Infrared", "pyroelectric", or "IR motion" sensor. / (Digital infrared motion sensors)

PIR is basically made ​​of a pyroelectric sensor (which you can see above as the round metal can with a rectangular crystal in the middle), which can detect infrared radiation levels. Everything emits some low-level radiation, and panas.Jika more radiation is emitted. Sensor detector will see the size of the IR than the other, and the output will swing high or low



SPECIFICATION:

     Type: Digital
     Supply Voltage :3-5V
     Current: 50μA
     Working temperature: 0 ℃ -70
     Output level (HIGH): 4V
     Output level (LOW): 0.4V
     Detect angle: 110 Degree
     Detect distance: 7 meters
     Size: 28mm × 36mm
     Weight: 25g


PIR sensor reading

PIR sensor connects to the microcontroller is really simple. PIR acts as a digital output so that all you need to do is use a pin to flip high (detected) or low (undetectable).

Power PIR to 5V and ground is connected to ground. Then connect the output to a digital pin. In this example we will use the pin 2.



The code is very simple, and basically just keep track of whether the input to pin 2 is high or low. So that it prints a message when the movement has been started and stopped.

/*
 * PIR sensor tester
 */
 
int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
 
void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
}
 
void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

No comments:

Post a Comment