Arduino – (Very) Basic motion tracking with 2 PIR sensors

Took me a little while to get started but I’ve managed to wire 2 PIR infrared sensors with an Arduino to sense motion either on the left or on the...

Took me a little while to get started but I’ve managed to wire 2 PIR infrared sensors with an Arduino to sense motion either on the left or on the right side. The result will trigger an LED to represent each PIR sensor then I also added in a servo to be controlled – so it turns left when triggered by the left sensor and so on.

First have a look at my previous tutorial – Arduino PIR motion detector circuit. Now we’re going to use 2 of them and this isn’t that different from just using one of them. However this is only a first attempt so I’ve not calibrated anything properly, for instance we can’t ascertain velocity or true direction of movement because these sensors aren’t that precise and have a wide range of detection. But hey, it’s a start!

Arduino PIR Motion Sensor Circuit Parts

2x 220 Ohm resistor (Red, Red, Brown, Gold)
2x 10K Ohm resistor (Brown, Black, Orange, Gold)
2x PIR sensor
1x Servo (has to need no more than 5v supply)
2x LED
Arduino Deumilanove w/ ATMEGA328
Breadboard / Prototyping board
Jumper/ Connector wires
Optional 9V DC power supply or use the USB power for the Arduino
You will also need a soldering iron and solder if you use the same PIR as myself.
Some sort of  temporary adhesive to hold the sensors in place.

Arduino Infrared Motion Detector Circuit

So you’ll see that its really just a lot of wires. The PIRs I’ve soldered on the wires and on their output pin there’s a 10K Ohm resistor for each going between them and the positive rail on the breadboard. The LED’s are the same layout as the basic blink tutorials, 220 Ohm resistor between the positive pin (the longer one) and the Arduino pin.

Arduino-2PIR-motion-tracker

PIR Motion Sensor Arduino Code

This isn’t too bad. Basically we set 2 sensors and have a few if statements to do shit based on if they turn on or off, so essentially they’re treated just like a couple of switches.

/*
LUCKYLARRY.CO.UK - 2 3pin PIR sensors to track basic motion.
 
We have 1 sensor for left, 1 for right.
 
The left sensor is triggered, the LED for the left comes on and the servo moves until no motion is detected.
The same happens if the right sensor is triggered.
 
If both sensors detect motion then its likely the object may be between the 2 but given the field of detection
its not going to be precice. Enjoy!
 
*/
 
#include                                                 // Include servo library, you can get it from http://www.arduino.cc/playground/ComponentLib/Servo
Servo myservo;                                                    // Create a servo object
int pos = 0;                                                      // Variable to store the servo position in degrees
int pinPIRleft = 4;                                               // left infrared sensor, digital pin 4
int pinLEDleft = 8;	                                          // left LED, digital pin 8
int pinPIRright = 2;                                              // right sensor, digital pin 2
int pinLEDright = 10;                                             // right LED, digital pin 10
 
void setup() {
  pinMode(pinLEDleft, OUTPUT);                                    // set LEDs as outputs
  pinMode(pinLEDright, OUTPUT);
  pinMode(pinPIRleft, INPUT);                                     // set sensors as inputs
  pinMode(pinPIRright, INPUT);
  myservo.attach(9);                                              // set the servo to digital pin 9
}
 
void loop() {
 
  if (digitalRead(pinPIRleft) == LOW) {                           // if left detects motion
    digitalWrite(pinLEDleft, HIGH);                               // turn on LED
    if ((pos < 180) && (digitalRead(pinPIRright) == HIGH)) {      // if less than 180 degrees and the right sensor is off then move servo
      pos += 1;                                                   // increment servo degrees by +1
      myservo.write(pos);                                         // write the position to the servo
      delay(15);
    }
  } else {
    digitalWrite(pinLEDleft, LOW);                                // otherwise turn off LED and no servo movement
  }
 
  if (digitalRead(pinPIRright) == LOW) {
    digitalWrite(pinLEDright, HIGH);
    if ((pos >= 1) && (digitalRead(pinPIRleft) == HIGH)) {
      pos -= 1;
      myservo.write(pos);
      delay(15);
    }
  } else {
    digitalWrite(pinLEDright, LOW);
  }
 
  if ((digitalRead(pinPIRleft) == LOW) && (digitalRead(pinPIRright) == LOW)) {
    // do something here if both sensors detect movement.
  }
}

And here it is in action:

Well, you could add a 3rd sensor to validate that an object is in front of both sensors, you’ll see that I’ve angled my sensors to try and work this with just 2 sensors. Perhaps limiting the field of detection for each sensor might also be a good thing to make it more precise.

This content is published under the Attribution-Noncommercial-Share Alike 3.0 Unported license.

Technorati Tags: , , , , , , , ,

  1. Arduino – motion triggered camera
  2. Controlling a Servo with Arduino
  3. Arduino + Processing – Make a Radar Screen – Part 3: Visualising the Data from Sharp Infrared Range Finder
  4. Arduino + Processing: Make a Radar Screen to Visualise Sensor Data from SRF-05 – Part 2: Visualising the Data
  5. Arduino + Processing – Make a Radar Screen to Visualise Sensor Data from SRF-05 – Part 1: Setting up the Circuit and Outputting Values
  6. Arduino – IR remote/ intervalometer for Nikon D80 DSLR (that means timelapse photography yarrr!)
  7. Arduino – Redefining the TV Remote
  8. Arduino – Control a DC motor with TIP120, potentiometer and multiple power supplies
  9. 3 LED Crossfade with PWM and Arduino
  10. Using Processing to Send Values using the Serial Port to Arduino