Arduino Motion Detector using PIR Sensor

PIR (Passive Infra Red) sensor is used to detect object’s movement in front of it. PIR sensor is made from pyroelectric sensors that can detect infrared wavelength. The sensor works by emitting infrared light towards the front, detects an object in front of it then calculates distance from infrared reflection from the object. Roughly the illustrations is shown in the figure below


PIR sensor is already as ‘turnkey’ module in the market, so you can use it directly (plug n play). The sensor is able to detect object’s movement in front of it as far as 5-7 m with a beam width (angle) 1100. The sensor module has 3 pins, namely: Vcc (+ 5V), Vout and GND (0V). As the name implies Vcc and Gnd pin connected to a voltage source. If using Arduino board, you simply connect these pins to ‘5V’ pin and ‘Gnd’ pin. While 'Vout' pin is output pin of the sensor. If there is a movement (motion) in front of the sensor, this pin will be change to logic ‘1’ (HIGH / 5V) and vice versa if there is no movement of the object in front of it, Vout pin will be steady as logic '0' (LOW / 0V). Connect the Vout pin to one pin of Arduino board, and you simply fill a small program/sketch to deal with these changes (representing automatic presence or absence of a moving object in front of PIR sensor).

Arduino - PIR Sensor Wiring Connection

Previously, wiring a connection between PIR sensor with Arduino board as follows.


Arduino - PIR Sensor Sketch Handler

I think you will be able to assemble it within 5 minutes (even less). Then open your Arduino IDE (can be downloaded at arduino.cc), and copy the listing small sketch as follows:

// Arduino motion detect with PIR sensor
// -------------------------------------
#define indicator 13 // internal LED as indicator
#define Vout 2 // PIR Vout pin
PIRstatus int = 0; // Logical status
int data = 0; // For temporary variables, use to hold the PIR data

void setup () {
pinMode (indicator, OUTPUT); // Set pin 13 as output
pinMode (Vout, INPUT); // Set pin 2 as input

Serial.begin (9600); // activate Serial comm
}

void loop () {
  
data = digitalRead (Vout); // read Vout status
if ((data == HIGH) && (PIRstatus == LOW)) {// motion detects
 digitalWrite (indicator, HIGH); // Turn on LED
 Serial.println ( "Motion detected!"); //show to Serial Monitor
 PIRstatus = HIGH; 

} Else {
  if ((data == LOW) && (PIRstatus == HIGH)) {
   digitalWrite (indicator, LOW); // Turn off LED indicator
  Serial.println ( "Motion ended!"); // show to serial monitor 
   PIRstatus = LOW;
   }
 }
}

Result

Upload sketch above by pressing the upload button or via shortkey CTRL+U, then open Serial Monitor window in Arduino IDE via Tools-Serial Monitor menu. If you do all steps above, then look at Serial Monitor window. It should be as shown below (make sure the USB cable between your laptop and Arduino remain attached).


Try to move an object in front of PIR sensor and observe the changes in Serial Monitor window. Congratulations, you have successfully detects movement of the object. Then you can utilized it for more advanced purposes.
Previous
Next Post »