PIR motion sensor

A Passive Infrared Detection (PIR) motion sensor is used to object movement. We can use this sensor for detecting vehicle presence. PIR sensors can be found on SeeedStudio at https://www.seeedstudio.com/PIR-Motion-Sensor-Large-Lens-version-p-1976.html, Adafruit at https://www.adafruit.com/product/189, and SparkFun at https://www.sparkfun.com/products/13285. PIR motion sensors usually have three pins: VCC, GND, and OUT.

The OUT pin is digital output on which if we get value 1, it means motion is detected. You can see the PIR sensor from SeeedStudio here:

There's another PIR motion sensor from SparkFun, the SparkFun OpenPIR: https://www.sparkfun.com/products/13968. This sensor provides analog output so we can adjust the motion detection value. You can see the SparkFun OpenPIR here:

For testing, we develop an Arduino application to access the PIR motion sensor. We will detect object motion using the PIR motion sensor. You can perform the following hardware wiring:

  • PIR Sensor VCC is connected to Arduino 5V
  • PIR Sensor GND is connected to Arduino GND
  • PIR Sensor OUT is connected to Arduino Digital 8

You can see the complete wiring here:

Now we can write a sketch program using Arduino software. To read object motion detection in digital form, we can use digitalRead(). If the program detects object motion, we turn on the LED. Otherwise, we turn it off.

Here is the complete sketch:

#define PIR_LED 13 // LED
#define PIR_DOUT 8 // PIR digital output on D8

int val = 0;
int state = LOW;
void setup()
{
pinMode(PIR_LED, INPUT);
pinMode(PIR_DOUT, INPUT);
Serial.begin(9600);
}

void loop()
{
val = digitalRead(PIR_DOUT);

if(val==HIGH)
{
digitalWrite(PIR_LED, HIGH);
if(state==LOW) {
Serial.println("Motion detected!");
state = HIGH;
}
}else{
digitalWrite(PIR_LED, LOW);
if (state == HIGH){
Serial.println("Motion ended!");
state = LOW;
}
}
}

Save this sketch as ArduinoPIR.

You can compile and upload the program to your Arduino board. To see the program output, you can open the Serial Monitor tool from Arduino. Now you test motion detection using your hand and see the program output in the Serial Monitor tool.

Next, we will use the SparkFun OpenPIR or other PIR motion sensor. Based on its datasheet, the SparkFun OpenPIR has similar pins similar to PIR motion sensors. However, the SparkFun OpenPIR sensor has an additional pin, the AOUT analog output.

This pin provides analog output as a voltage value from the object's motion level. From this case, we set the object motion-detection level based on the analog output.

For testing, we'll develop a sketch program using the SparkFun OpenPIR and Arduino. You can build the following hardware wiring:

  • OpenPIR Sensor VCC is connected to Arduino 5V
  • OpenPIR Sensor GND is connected to Arduino GND
  • OpenPIR Sensor AOUT is connected to Arduino Analog A0
  • OpenPIR Sensor OUT is connected to Arduino Digital 2

Now we'll modify the code sample from SparkFun. To read analog input, we can use analogRead() in our sketch program. Write this sketch for our demo:

#define PIR_AOUT A0  // PIR analog output on A0 
#define PIR_DOUT 2   // PIR digital output on D2 
#define LED_PIN  13  // LED to illuminate on motion 
 
void setup()  
{ 
  Serial.begin(9600);  
  // Analog and digital pins should both be set as inputs: 
  pinMode(PIR_AOUT, INPUT); 
  pinMode(PIR_DOUT, INPUT); 
 
  // Configure the motion indicator LED pin as an output 
  pinMode(LED_PIN, OUTPUT); 
  digitalWrite(LED_PIN, LOW);  
} 
 
void loop()  
{ 
  int motionStatus = digitalRead(PIR_DOUT);   
  if (motionStatus == HIGH) 
    digitalWrite(LED_PIN, HIGH); // motion is detected 
  else  
    digitalWrite(LED_PIN, LOW); 
 
  // Convert 10-bit analog value to a voltage 
  unsigned int analogPIR = analogRead(PIR_AOUT); 
  float voltage = (float) analogPIR / 1024.0 * 5.0; 
  Serial.print("Val: "); 
  Serial.println(voltage); 
} 

Save it as ArduinoOpenPIR.

You can compile and upload the sketch to your Arduino board. Open the Serial Monitor tool to see the program output. Now you can move your hand and see the program output in the Serial Monitor tool.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.63.136