Arduino Motion Detector


Using Parallax Passive Infrared Motion Sensor #555-28027

Arduino Motion Detector Using Parallax Passive Infrared Motion Sensor #555-28027

Parallax’s Passive Infrared Motion Sensor, #555-28027, is a small board you can easily add to your Arduino project. I put together a sketch using this device together with a 16×2 LCD for the read out. The code is based on two other sketches I found; one for LCD and the other for processing the input from the sensor.

The circuit is powered by 3.3V on the Arduino.

This code prints an alert along with the hours, minutes, and seconds since initialization on the LCD. It also prints out to the serial port/USB that you can read from your computer if you don’t have an LCD.

#include
	
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
	
/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
unsigned int calibrationTime = 30;
unsigned long current_millis_value = 0;
unsigned long previous_millis_value = calibrationTime * 1000;
unsigned long m = 0;
unsigned int seconds = 0;
unsigned int minutes = 0;
unsigned int hours = 0;
char msg[32];
	
//the time when the sensor outputs a low impulse
long unsigned int lowIn;         
	
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;  
	
boolean lockLow = true;
boolean takeLowTime;  
	
int pirPin = 10;    //the digital pin connected to the PIR sensor's output
int ledPin = 13;
	
/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  lcd.begin(16,2);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);
	
  //give the sensor some time to calibrate
  Serial.print(\"calibrating sensor \");
  lcd.print(\"calibrating sensor \");
  for(int i = calibrationTime; i > 0; i--){
    lcd.clear();
    lcd.print(i);
    Serial.print(\".\");
    delay(1000);
  }
  Serial.println(\" done\");
  lcd.clear();
  lcd.print(\"Ready\");
  Serial.println(\"SENSOR ACTIVE\");
  delay(50);
}
	
void loop(){
	
  current_millis_value = millis();
  m += current_millis_value - previous_millis_value;
  seconds += m / 1000;
  m = m % 1000;
  minutes += seconds / 60;
  seconds = seconds % 60;
  hours += minutes / 60;
  minutes = minutes % 60;
  hours = hours % 24;
  previous_millis_value = current_millis_value;
	
  if(digitalRead(pirPin) == HIGH){
    digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
    if(lockLow){
      //makes sure we wait for a transition to LOW before any further output is made:
      lockLow = false;
      Serial.println(\"---\");
      Serial.print(\"Motion detected at \");
      Serial.print(millis()/1000);
      Serial.println(\" sec\");
      lcd.clear();
      lcd.setCursor(0, 0);
      sprintf(msg,\"%d:%02d:%02d %s\", hours, minutes, seconds, \"Alert!\");
      //sprintf(msg,\"%s%d%s\", \"Alert at \", (millis() - pause)/1000, \" sec\");
      lcd.print(msg);
      delay(50);
    }
    takeLowTime = true;
  }
	
  if(digitalRead(pirPin) == LOW){
    digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state
	
    if(takeLowTime){
      lowIn = millis();    //save the time of the transition from high to LOW
      takeLowTime = false;    //make sure this is only done at the start of a LOW phase
    }
    //if the sensor is low for more than the given pause,
    //we assume that no more motion is going to happen
    if(!lockLow && millis() - lowIn > pause){
      //makes sure this block of code is only executed again after
      //a new motion sequence has been detected
      lockLow = true;
      Serial.print(\"motion ended at \");   //output
      Serial.print((millis() - pause)/1000);
      Serial.println(\" sec\");
      lcd.clear();
      lcd.setCursor(0, 1);
      sprintf(msg,\"%2d:%02d:%02d %s\", hours, minutes, seconds, \"Reset\");
      //sprintf(msg,\"%s%d%s\", \"Reset at \", (millis() - pause)/1000, \" sec\");
      lcd.print(msg);
      delay(50);
    }
  }
}

To put the project together, I used the following components:

  • Breadboard
  • Parallax Passive Infrared Motion Sensor, #555-28027
  • 10K Pot
  • NMTC-S16205DRGHS Gray Reflective Microtips LCD Character Display Module (16×2)
  • Arduino Duemilanove

Connections:

  1. Arduino 3V to Breadboard +
  2. Arduino GND to Breadboard GND
  3. Parallax Motion Sensor + to Breadboard +
  4. Parallax Motion Sensor GND to Breadboard GND
  5. Parallax Motion Sensor Data to Arduino Pin 10
  6. 10k Potentiometer legs to Breadboard GND and Breadboard +
  7. LCD Pin 1 to Breadboard GND
  8. LCD Pin 2 to Breadboard +
  9. LCD Pin 3 to 10k Pot Wiper (center)
  10. LCD Pin 4 to Arduino Pin 12
  11. LCD Pin 5 to Breadboard GND
  12. LCD Pin 6 to Arduino Pin 11
  13. LCD Pin 11 to Arduino Pin 5
  14. LCD Pin 12 to Arduino Pin 6
  15. LCD Pin 13 to Arduino Pin 7
  16. LCD Pin 14 to Arduino Pin 8

Be prepared for the coming food shortage.