Motion Detection with PIR sensors and Arduino

Motion Detection with PIR sensors and Arduino

Security these days is premium, much emphasizing is now being placed on it. PIR (passive infrared) sensors have been around for a while, hobbyists, electronic lovers, students etc have been using PIR sensors for projects based especially on security.

PIR sensors allow you to sense motion, almost always used to detect whether a human has moved in or out of the sensors range. They are small, inexpensive, low-power, easy to use and don’t wear out. For that reason they are commonly found in appliances and gadgets used in homes or businesses. They are often referred to as PIR, “Passive Infrared”, “Pyroelectric”, or “IR motion” sensors.

pir sensor - steinacoz
pir sensor – steinacoz

PIRs are basically made of a pyroelectric sensor (which you can see below as the round metal can with a rectangular crystal in the center), which can detect levels of infrared radiation. Everything emits some low level radiation, and the hotter something is, the more radiation is emitted. The sensor in a motion detector is actually split in two halves. The reason for that is that we are looking to detect motion (change) not average IR levels. The two halves are wired up so that they cancel each other out. If one half sees more or less IR radiation than the other, the output will swing high or low.

Along with the pyroelectic sensor is a bunch of supporting circuitry, resistors and capacitors. It seems that most small hobbyist sensors use the BISS0001 (“Micro Power PIR Motion Detector IC”), undoubtedly a very inexpensive chip. This chip takes the output of the sensor and does some minor processing on it to emit a digital output pulse from the analog sensor.

pir sensor labeled- steinacoz
pir sensor labeled- steinacoz

In these tutorial, we are going to implement a small system that beeps alarm/buzzer on detecting motion within its range. Almost all the materials for this project can be gotten from our store, here is the list:

We will be assuming that the PIR sensors will be placed at different locations or facing different directions. Connect the VCC of the PIR sensors to the 5volts pin of the Arduino, the out pin of the PIR sensors to pin 2 and 3 of the Arduino respectively and the GND to the GND of the Arduino. The LEDs should be connected to pins 13 and 12 of the Arduino respectively. The piezo buzzer goes into pin 10 of the Arduino. Finally, the GSM module RXD pin connected to Arduino TXD pin (GSM module not really included).

 

pir sensor circuit arduino- steinacoz
pir sensor circuit arduino- steinacoz

Copy and paste this code into your Arduino IDE:

//code by ugwumsinachi nnadi - steinacoz.com

int ledPin = 13; // choose the pin for the LED
int ledPin2 = 12; // choose the pin for the LED


int inputPin = 2; // choose the input pin (for PIR sensor)
int inputPin2 = 3;


int pirState = LOW; // we start, assuming no motion detected
int pirState2 = LOW; // we start, assuming no motion detected


int val = 0; // variable for reading the pin status
int val2 = 0; // variable for reading the pin status


int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)

String PIR; 


void setup() { 
  pinMode(ledPin, OUTPUT); // declare LED as output 
  pinMode(ledPin2, OUTPUT); // declare LED as output 
  
  
  pinMode(inputPin, INPUT); //declare sensor as input
  pinMode(inputPin2, INPUT);

  
  pinMode(pinSpeaker, OUTPUT);
  Serial.begin(9600);
}


void loop(){
  val = digitalRead(inputPin); // read input value
  val2 = digitalRead(inputPin2);
  val3 = digitalRead(inputPin3);

  if (val == HIGH) { // check if the input is HIGH
    PIR = "PIR1";
    digitalWrite(ledPin, HIGH); // turn LED ON
    playTone(300, 300);
    delay(150);
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      sendSMS(PIR);

      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    playTone(0, 0);
    delay(300);
    if (pirState == HIGH){

      // we have just turned of
      Serial.println("Motion ended!");

      pirState = LOW; 
    } 
  } 

  //4 pir 2
  if (val2 == HIGH) { // check if the input is HIGH
    PIR = "PIR2";
    digitalWrite(ledPin2, HIGH); // turn LED ON
    playTone(300, 300);
    delay(150);
    if (pirState2 == LOW) {
      // we have just turned on
      Serial.println("Motion detected in pir2!");
      sendSMS(PIR);

      // We only want to print on the output change, not state
      pirState2 = HIGH;
    }
   } else {
    digitalWrite(ledPin2, LOW); // turn LED OFF
    playTone(0, 0);
    delay(300);
    if (pirState2 == HIGH){
      // we have just turned of
      Serial.println("Motion ended in pir2!");

      pirState2 = LOW; 
    } 
  } 



}

// duration in mSecs, frequency in hertz

void playTone(long duration, int freq) {
 duration *= 1000;
 int period = (1.0 / freq) * 100000;
 long elapsed_time = 0;
 while (elapsed_time < duration) {
  digitalWrite(pinSpeaker,HIGH);
  delayMicroseconds(period / 2);
  digitalWrite(pinSpeaker, LOW);
  delayMicroseconds(period / 2);
  elapsed_time += (period);
  }
}

void sendSMS(String pir){
   //Send SMS
  Serial.print("AT+CMGF=1\r"); 
  //Phone number you want to send the sms
  Serial.print("AT+CMGS=\"+91xxxxxxxxxx""\r"); 
  //Text to send                             
  Serial.print("Intruder Alert from " + pir + " - Someone At poultry door\r");
  //sends ctrl+z, end of message    
  Serial.write(0x1A); 
}

The GSM module is just to send SMS to the user whenever motion is detected by the PIR sensor.

4 thoughts on “Motion Detection with PIR sensors and Arduino

  1. NIce and brilliant work from you….
    but u know this is for those that have knowledge about what arduino means even if they want to practice they might buy arduino seedo instead of uno… nonetheless it would have been greater if you carry us along maybe recording a video on how to set them up.

    I know i should use this but i prefer raspberry pi, pir sensor, pi camera, wifi dongle, female to female pins and some software like putty instead of gsm module. even coupled with facial recognition by uploading the faces to amazon db or dropbox db

    Nice work though. i love it

    1. Thanks for the feedback, we are working round the clock on including videos to our tutorials and great to have tech guys like you already.

  2. Pingback: Cialis pills

Leave a Reply

Your email address will not be published. Required fields are marked *