Thursday, November 21, 2013

PIR sensor - Motion sensor




Yet another sensor! This time it's a PIR (Passive InfraRed) sensor: it reacts to infrared light emitted by warm objects or beings. It is called passive since it does not emit anything, just signals whenever it sense a variation of infrared light in its range.

Technical data (from the seller):

Range: 3m - 7m
Angle: 140° (approx.)
Trigger state duration: 3s - 200s
Power supply: 4.5V - 15V DC current
Output signal: 3V (alarm status), 0V (normal state)
Power consumption: <60uA (normal state)
Working temperature: [-15°C,+70°C]
Trigger mode: repeated sensing/non-repeated sensing





Pin out

Looking at the above image pins are as follows (left to right) :



- GND

- OUT signal

- VCC





Configuration and initialization



Several parameters can be configured on this sensor. First of all, its range. This can be changed using the orange potentiometer on the left of the above figure. Second, the duration of the alarm signal when a motion event is triggered. This is regulated by the orange potentiometer on the right and ranges from 5s to 200s. Call this parameter DURATION_TIME. Finally, the yellow jumper can be used to select the trigger mode. There are two modes :


0-0-X : REPEATED SENSING
when an alarm is triggered (OUT pin goes HIGH), the signal is maintained HIGH until no further movement is detected, independently of the duration time selected by the potentiometer.

X-0-0 : NON REPEATED SENSING
when an alarm is triggered (OUT pin goes HIGH) and the signal is maintained HIGH only for the duration indicated by the duration potentiometer, then it goes LOW. It will turn to HIGH when another movement is detected.

At power-up the sensor performs an initialization sequence which consists of sending the OUT pin HIGH for a duration equal to DURATION_TIME. Then, the OUT pin is set to LOW, the sensor takes the default ambient image which will be compared to in all future sensing operations. This last process takes about 15s. The seller advices to make no movement during the initialization process.

It is important to select correct DURATION_TIME. Indeed, since the MCU knows that the sensor won't change its state for at least this duration, it is free execute some other task in the meanwhile.


The test program


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/****************************************************************************
   PIRsensor : test program for PIR sensor module

   Author: Enrico Formenti
   Permissions: MIT licence
   
   Remarks:
    - OUT pin is connected to digital pin 2 of Arduino, change this 
      if needed
    - DELAY times depend on the type of module and/or its 
      configuration.
*****************************************************************************/

#include <Serial.h>

// OUT pin on PIR sensor connected to digital pin 2
// (any other digital pin will do, just change the value below)
#define PIRSensorPin 2

// This the the amount of time that the OUT pin remains high after
// a mouvement has been triggered
#define DURATION_DELAY 5000

// This is the amount of time that the sensor takes to initialize
// Remark that normally the total initialization time is
// given by INIT_DELAY+DURATION_DELAY
// check your sensor datasheet for the correct values
#define INIT_DELAY 17000

void setup() {
  pinMode(PIRSensorPin, INPUT);
  delay(DURATION_DELAY+INIT_DELAY);
}

void loop() {

  if(digitalRead(PIRSensorPin)) {
    Serial.println("Movement deteced");
    
    // do something else at least for DURATION_DELAY
    delay(DURATION_DELAY);
  }
  else
    Serial.println("Nothing being detected…");
}

Here is the test program using the Mazduino Library (you can find it on github here):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*************************************************************************************
   PIRsensor : test program for PIR sensor module

   Author: Enrico Formenti
   Permissions: MIT licence
   
   Remarks:
    - OUT pin is connected to digital pin 2 of Arduino, change this if needed
***************************************************************************************/

#include <Serial.h>
#include <PIR.h>

// OUT pin on PIR sensor connected to digital pin 2
// (any other digital pin will do, just change the value below)

#define PIRSensorPin 2

PIR myPIR(PIRSensorPin);

void setup() {
  myPIR.begin();
}

void loop() {

  if(myPIR.getStatus()) {
    Serial.println("Movement detected");
    
    // do something else at least for the delay between two successive
    // readings
    delay(myPIR.getDurationDelay());
  }
  else
    Serial.println("Nothing being detected…");
}

No comments :

Post a Comment