Took me a little while to get started but I’ve managed to wire 2 PIR sensors to sense a movement 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 with a PIR here. 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!
Shopping List
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 power supply (here) 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.
The 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.
The sketch
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 <Servo.h> // 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:
After thoughts
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.
- Controlling a Servo with Arduino
- Arduino + Processing: Make a Radar Screen to Visualise Sensor Data from SRF-05 – Part 2: Visualising the Data
- Arduino + Processing: Make a Radar Screen to Visualise Sensor Data from SRF-05 – Part 1: Setting up the Circuit and Outputting Values
- Arduino + Processing: Make a Radar Screen – Part 3: Visualising the Data from Sharp Infrared Range Finder
- Arduino: motion triggered camera
- Arduino: IR remote/ intervalometer for Nikon D80 DSLR (that means timelapse photography yarrr!)
- Arduino: Basic Theremin meets Processing!
- Arduino: Control a DC motor with potentiometer and multiple power supplies
- 3 LED Crossfade with PWM and Arduino
- Obstacle avoidance robot – build your own larryBot










Maybe you could the digital output of the PIR board to trigger an interrupt and then use an analog pin on the Arduino wired directly to the PIR sensor to look for the positive/negative or negative/positive pulses as the object moved from right to left or left to right? Because the PIR sensor is really two sensors in one and opposing each other, you should see a pulse based on what PIR half is triggered first and then the opposite pulse as the object enters and leaves the other sensor half.
Cheers Doug, not got that far with the PIR. I think I’ll wire up the Analog and look for the change as you suggest and see what that yields. I’m going to stick the setup on my robot so it will (eventually) follow people. Have you played with many other sensors?
Just had a look at sensing the PIR detection wave, unfortunately my PIR sensors combine their outputs via the IC, so I never get the PIR wave as it were, just the combined IC output of high/low.
Hi Larry, great website!
For a project I’m looking to integrate the same system. But I don’t really get all the connections (I’m just a beginner) … Do you have a map of this? That would really help me out!
Thx a million!
sorry its midnight here, but for now I’ll try and explain where everything goes – I guess putting wiring diagrams up will help out more…
Anyway, depends on what PIR you have? I’m guessing you have 3 wires right? 2 will be for power – which you have to get the right way around otherwise they start making alot of smoke! Anyway these power cables will run into the Arduino supply via the breadboard.
So the only thing to worry about is sensing change, to do this basically you plug in the 3rd pin/wire on the PIR to a digital output on your Arduino which also has a 10k resistor connecting this 3rd pin to the +5v power supply – in my picture this is the yellow cable going to digital pin 2 – its the easiest one to figure out on my picture. You’ll see the small green wires – these just hook up the power/ ground for the components.
With PIR’s a good trick to debugging this is using some LEDs to show you when the sensor has been triggered – this is the basic LED setup, so power coming from a digital pin and 220 ohm resistor between this and the postive pin on the LED the ground/ negative going to the GND rail on the breadboard.
The 2 small orange wires just connect one half the power rails to the other half.
For the servo, the power again goes to the breadboard which is fed by the Arduino etc… but its 3rd pin again goes to a digital pin on the Arduino.
Let me know if you need the diagrams and I’ll do them tomorrow evening for you
Thx for you fast reply! Yes I got the same Pir motion sensor than the one your using…
!
I would be really thankful if you could send me the wiring diagram!
I actually don’t need a diagram with led’s cause I’m building the application into a art installation & the led’s are not required for this….
Thx a million for this info, it would really help me out! Seems to be really difficult to find people who know this things here in Belgium
Looking forward to your reply….
Here we go Philip, one sketch
hope this helps – I kept it simple just for one PIR, for two just follow the same rules.
Ok the diagram is very clear! Thx for drawing it out!!!!
, but it’s my first time I’m working on Arduino, actually I have a basic knowledge of electronics but I notice that I have loads to learn, now that I’m working on this installation I shall have no other choice than learning it! take a look at http://www.philiphenderickx.be so you can see what I normally do… Well so far I don’t know how to thank you again for your advise & help! looking forward to your reply, I guess the final reply before I get this rolling…. unless I burn down my Arduino Deumilanove!
I guess the servo stay’s in the same position digital port 9?
Since the both leds have been deleted from the installation, what should I with them in the sketch?
I know I’m terrible in this
Thx again!!!!
Would you believe that this gave me hassles as well? Thats why the LED’s are there as I just couldn’t get two sensors to work to start with!
Leave the servo on digital pin 9 – my code should take care of it.
The way I tackled all these projects is to start small and keep adding once I had one thing working. If you want me to I can expand the diagram for the servo.
With the servo, 2 pins are for power – normally black and red, the yellow or white cable is the control and this can go directly to the Arduino pin, there’s no need for a pull up resistor like the PIR’s
Cool paintings by the way! I like the site. Just for your info: I’m actually a trained as a designer, not an electronics or programming!
So keep going and persevere…
Thx! gives me hope
!!! I’ve ordered the equipment should be delivered in 4 days! will keep you updated!
But i really wanna thank you for all this information!! will send you some pictures of the work once it’s ready!
soon more news!
Cool, send me pictures when you’re done – if you get stuck give me a shout.
hey larry, I got a bit delayed, was working on an exhibition.
Finally got the tools, but doesn’t seem to work the servo moves left & right & shakes a lot… but doesn’t follow any of the movement…. the connection seems to work perfectly, so does the sketch, at least I think so, I copied the entire sketch & get no error (see picture)! could it be that the LED’s are noted inside the sketch but not in the circuit? can’t find out what’s wrong… any idea….?
Thx a million!
Philip
seems I can’t put pictures, but the Arduino control gives me the following message:Binary sketch size: 2784 bytes (of a 30720 byte maximum), the upload also seems to work… only problem is that the servo doesn’t do what he’s supposed to…???
Regards,
Philip
hmm… LED’s are just for error checking or seeing an effect, one for each sensor when triggered. It won’t matter if they’re in your circuit.
I would do 2 things. Firstly try a sketch just to make sure you can control the servo and that its wired in correctly – should be 3 wires, two for power supply and one for control. Just to check that the servo library is running. The servos need to use no more than 5 volts.
Next check the sensors – they need also have 3 wires, 2 for power, 1 for reading – but needs a pull up resistor, check that values are being read.
I did have issues with getting my sensors working, but this sounds more like servo issues, when I have problems I try to break things down to smaller circuits to make sure I’ve gotten things correct(ish). You can mail me pictures etc.. at larry@ and I can take a look.