loading
Welcome to the next installment of Arduino Basics!

This instructable will teach you how to use a PIR sensors with the arduino, and we wil build a simple motion detector!


Step 1: Supplies

For this instructable you will need the following:

1 arduino (with protoshield to make life easy)
1 LED of any color
1 PIR sensor from Parallax (you can find these at most radio shacks)
Solid wire to hook it up
<p>int ledPin1 = 13; // choose the pin for the LED</p><p>int ledPin2 = 12;</p><p>int ledPin3 = 11;</p><p>int ledPin4 = 10;</p><p>int inputPin = 9; // choose the input pin (for PIR sensor)</p><p>int inputPin2 = 8;</p><p>int buzzerPin = 7;</p><p>int switchPin = 6; //pin for door sensor1</p><p>int switchPin2 = 5; // pin for door sensor2</p><p>int pirState1 = LOW; </p><p>int pirState2 = LOW;</p><p>int val = 0; </p><p>int val2 = 0;</p><p>int magnetState = LOW;</p><p>int magnetState2 = LOW;</p><p>int magnetValState = 0;</p><p>int magnetValState2 = 0;</p><p>void setup() {</p><p> pinMode(ledPin1, OUTPUT); // declare LED as output</p><p> pinMode(ledPin2, OUTPUT); // declare LED as output</p><p> pinMode(ledPin3, OUTPUT);</p><p> pinMode(inputPin, INPUT); // declare sensor 1 as input</p><p> pinMode(inputPin2, INPUT); // declare sensor 2 as input</p><p> pinMode(buzzerPin, OUTPUT);</p><p> pinMode(switchPin, INPUT);</p><p> pinMode(switchPin2, INPUT);</p><p> digitalWrite(switchPin, HIGH);</p><p> digitalWrite(switchPin2, HIGH);</p><p> Servo1.attach(servoPin);</p><p> Serial.begin(9600);</p><p>}</p><p>void loop() {</p><p> val = digitalRead(inputPin); // read input value</p><p> if (val == HIGH) { // check if the input is HIGH</p><p> digitalWrite(ledPin1, HIGH); // turn LED ON</p><p> magnetValState = digitalRead(switchPin);</p><p> if (digitalRead(switchPin) == HIGH) {</p><p> digitalWrite(ledPin2, HIGH);</p><p> delay(1500);</p><p> digitalWrite(ledPin2, LOW);</p><p> delay(1500);</p><p> digitalWrite(buzzerPin, HIGH);</p><p> Serial.println(&quot;Door brake!&quot;);</p><p> }</p><p> if (pirState1 == LOW) {</p><p> // we have just turned on</p><p> Serial.println(&quot;Motion detected!&quot;);</p><p> // We only want to print on the output change, not state</p><p> pirState1 = HIGH;</p><p> }</p><p> }</p><p> // check sensor 2 after delay</p><p> val2 = digitalRead(inputPin2);</p><p> if (val2 == HIGH) {</p><p> digitalWrite(ledPin3, HIGH);</p><p> magnetValState2 = digitalRead(switchPin2);</p><p> if (digitalRead(switchPin2) == HIGH) {</p><p> digitalWrite(ledPin4, HIGH);</p><p> delay(50);</p><p> digitalWrite(ledPin4, LOW);</p><p> delay(50);</p><p> digitalWrite(buzzerPin, HIGH);</p><p> Serial.println(&quot;Door brake!&quot;);</p><p> }</p><p> if(pirState2 == LOW) {</p><p> // we have just turned on</p><p> Serial.println(&quot;Motion from sensor 2 detected!&quot;);</p><p> // We only want to print on the output change, not state</p><p> pirState2 = HIGH;</p><p> }</p><p> } else {</p><p> digitalWrite(ledPin1, LOW); // turn LED OFF</p><p> delay (50);</p><p> digitalWrite(ledPin2, LOW); // may be already</p><p> //playTone(0, 0);</p><p> delay(50);</p><p> digitalWrite(ledPin3, LOW); // turn LED OFF</p><p> delay (50);</p><p> digitalWrite(ledPin4, LOW); // turn LED OFF</p><p> delay (50);</p><p> if (pirState1 == HIGH) {</p><p> // we have just turned of</p><p> Serial.println(&quot;Motion ended!&quot;);</p><p> // We only want to print on the output change, not state</p><p> pirState1 = LOW;</p><p> }</p><p> if (pirState2 == HIGH) {</p><p> // we have just turned of</p><p> Serial.println(&quot;Motion ended!&quot;);</p><p> // We only want to print on the output change, not state</p><p> pirState2 = LOW;</p><p> }</p><p> }</p><p>}</p><p>someone please help me,how can i combine two pir sensor with two door sensor? im trying this coding,but only sensor 2 detected...</p>
<p>resistor needed across the LED?</p>
I Want to make a Human / living being detector, not a motion detector. How to do that and what should be the changes in code ?
<p>I made it but the sensor doesn't seem to work, probably something to do with these dials on top. Other than that, it was a geat and siple project</p>
<p>If you are referring to the tiny circles on the PIR dome, they are Fresnel lenses that help focus the IR on the sensors. *As I understand it,* any movement of a heat source causes the focus to shift over the 2 sensors built into the device. That change is what the device detects/signals.</p>
<p>good..................</p>
Can PIR motion sensor and arduino nano work together?
<p>Hi,</p><p>I have been wondering a few things.</p><p>1 does it output only high or low or is there a way to read a numeric value?</p><p>2 is it possible to use two pirs and follow the movement according to the 2 pirs, so if more right reading it looks more to the right and left same story.</p><p>If anyone knows a pointer for me to look in it is very much appreciated.</p><p>Keep up the good work. </p>
<p>I utilized my on board SMD led pin 13. Nice instructions.</p>
<p>PIR sensor implemented for alarm car alarm system</p><p></p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/30-adUbNlBo" width="500"></iframe></p>
<p>Simple. Fun. I like the buzzer idea.</p>
<p>can u help me sir?</p>
What does pirstate=HIGH mean in the code?
<p>please can you send me a code i am using a pir</p>
<p>I am useing an aurdwendo i need a code can you please help me </p>
<p>hai in my PIR sensor the output pin always goes high even when there is <br>no motion,I also altered the sensitivity and delay,but there is no <br>change! any one help me plzz!..</p>
<p>i am starting arduino please help me to interface sensors</p>
<p>hello when I change the &quot;long unsigned int pause = 5000;&quot;<br><br>I do not see any change in how long the led stays on. <br><br>In fact regardless of how I long set it, it will only stay on for 2 seconds. </p>
<p>pir.pde:98:37: error: invalid operands of types 'long unsigned int' and 'int()' to binary 'operator-'</p><p>This error is presented whenever i tried to verify/upload this code.. Help Me! T_T</p>
<p>Great info! thanks</p><p>Next time, and for other sensors connection you can use <a href="http://www.circuito.io" rel="nofollow">Circuito.io</a>, it's a free tool to generate schematics and code</p>
<p>Great work!<br>I give a &quot;stars&quot; rating of this Instructable at 1.2 GreggaWatts!</p>
<p>Hello, I want the pin sensor does not stop detect, which is always detecting the movement because the light goes out even with people close to him. THANK YOU</p>
<p>You have no idea how much work you just saved me. I owe you one.<br>I will use your code as the starting point to activate an LED art installation at University of Houston.</p>
<p>Ty for this Instructable</p>
<p>Thank you for sharing this simple but effective Instructable. I had trouble with the PIR sensor before, but this tutorials seems to work just fine!</p>
<p>Thanks for the post!</p>
<p>How can you code an LED strip instead of a single LED?</p>
<p>go on to youtube and look up (led strip with arduino) it should be the first one that pops up will be from MAKE its a duel tutorial in the vid. so youll need to watch it for a min. they have a really good tutorial about how to do that youll just need to do once in stead of three times all that youll have to change is to use the wire that would turn on the led in this tutorial and use as the wire you hook up to the resistor on the other tutorial i hope this helps and good luck</p>
<p>go on to youtube and look up (led strip with arduino) it should be the first one that pops up will be from MAKE its a duel tutorial in the vid. so youll need to watch it for a min. they have a really good tutorial about how to do that youll just need to do once in stead of three times all that youll have to change is to use the wire that would turn on the led in this tutorial and use as the wire you hook up to the resistor on the other tutorial i hope this helps and good luck</p>
<p>Hi, </p><p>I'm working on an arduino project that could keep an eye on my dog while I'm not home, can use an PIR sensor to detect any unusual movement based on the frequencies it sends ?</p><p>thank you </p>
<p>RESPECTED SIR</p><p>hi </p><p>i have made according to your instruction but afer the callibrating process it automatically says motion is dected even if there is no motion plz help.</p>
<p>I made it, so easy and so usable. I've an idea to add a buzzer.</p>
I want a code to activate the PIR sensor just if a motion exists for 25 secs continuously
<p>Usually on PIR sensors (like the one in the link) there are trim pots, so you can adjust the amount of time you want to keep something on (25 seconds) by turning the trim pot clockwise or counterclockwise.</p><p>http://www.ebay.com/itm/281519724893?_trksid=p2059210.m2749.l2649&amp;ssPageName=STRK%3AMEBIDX%3AIT</p>
<p>what happen when we turning the trim pot to clockwise? what about counterclockwise ? can i have a coding to make the led stay high when there is a movement and low when the pir detects no motion at all.</p>
<p>Hello Frenzy,<br><br>What did you use for a sketch in the Arduino.</p><p>Thanks.<br><br>Allen in Dallas</p>
<p>how do i change the led to an 5v relay? so that when its movement the relay is high. and good project :)</p>
<p>It's simple. On a 5 volt relay (and any relay for that matter) there are typically 3 pins: VCC, IN and Ground. Connect the &quot;In&quot; pin on your relay (signal) to whatever output pin you selected on your Arduino . In this demonstration, it's pin 13. Done and done! :-)</p>
yhanks ;)
<p>Franzy, just wanted to say that this was great, quick, and easy to use. I managed to get this working on a Spark core. Used your code (just changed the pin #'s to pirPin 0, ledPin 1), and worked like a charm. Thanks a ton!</p>
<p>Hi, If there is complete darkness will the PIR still work?</p>
<p>Yes, PIRs work off of an infrared grid setup. Think of it like looking for changes in &quot;heat&quot; across a bunch of zones (not 100% accurate but close enough to get the point of how it works). Light is irrelevant.</p>
I like to move it move it
Very good tutorial, made it and also added smoke alarm. Now it's time for the next step in my project, getting notification when triggered. Any links to tutorials to do this? I've read in the comments something about pushing box. Any examples or instructions maybe?
<p>Thanks for sharing this! Very helpful for me.</p>
Yo utilice un sensor PIR como interruptor para el ba&ntilde;o. Las manos h&uacute;medas son peligrosas, cuando no se tiene interruptor diferencial. Tiene una aprobaci&oacute;n de 8/10 en mi casa, se resisten al cambio. Saludos
how do you get this to send a txt message? <br>I have made a motion activate camera and I am looking to get it to send me a text message or email when a picture is taken...any thoughts? <br>thanks
<p>I know this was posted quite some time ago, but in case you are still wondering about how to get a txt or email, you will need an ethernet controller. With an ethernet controller, you can utilize the internet and send either an email or a txt message. This is done with the arduino, the ethernet shield, the sensor for triggered input, and an external program to interface with the signal from the arduino. I use pushing box. Hope this helps.</p>

About This Instructable

324,788views

450favorites

License:

Bio: I'm a full stack web developer focusing on security and privacy.
More by frenzy:Cómo hacer una placa iluminada con teclas de un teclado Una funda para tu dispositivo, hecha de un traje de buceo Dead Drop inalámbrico 
Add instructable to: