Introduction: Arduino Basics: PIR Sensors

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

Step 2: Setup

The wiring is pretty simple, the PIR sensor has screen printed: + - out

Hook the + to 5v, - to ground and out to pin 7

The take the LED and put power to pin 8 and ground to ground.

If its confusing, take a look at the pictures!

Step 3: Code

This code is lifted from the arduino.cc site here, the code I used is also attached.


You can see from the code, the sensor first calibrates itself and then watches for movement. When it detects movement, the blue light goes on. You can watch the serial monitor to see how long the movement lasts.

Step 4: Further Projectse

After these steps you have a very simple motion detector. From here you can use the PIR sensor to trigger events (like a siren or a text message that someone is in your room).

I just chose to use it to protect my stunnaz from blue light fearing monsters. It seems to work so far....

Please post your PIR sensor projects below and stay tuned for even more Arduino Basics!

Comments

author
ooha (author)2016-03-17

What does pirstate=HIGH mean in the code?

author
Otniel96 (author)ooha2017-06-30

it means pir sensor ill on, after its detected something

author
MariaDabukke (author)2017-04-09

I have made the program like that and the sensor have been active in serial monitor. But, the LED can't be light up. Are there any solution to fix this?

author
Graham Mantel (author)2017-01-30

Thanks for the info, I will try this out, code looks good. Thanks again

author
NurulF13 (author)2016-12-02

int ledPin1 = 13; // choose the pin for the LED

int ledPin2 = 12;

int ledPin3 = 11;

int ledPin4 = 10;

int inputPin = 9; // choose the input pin (for PIR sensor)

int inputPin2 = 8;

int buzzerPin = 7;

int switchPin = 6; //pin for door sensor1

int switchPin2 = 5; // pin for door sensor2

int pirState1 = LOW;

int pirState2 = LOW;

int val = 0;

int val2 = 0;

int magnetState = LOW;

int magnetState2 = LOW;

int magnetValState = 0;

int magnetValState2 = 0;

void setup() {

pinMode(ledPin1, OUTPUT); // declare LED as output

pinMode(ledPin2, OUTPUT); // declare LED as output

pinMode(ledPin3, OUTPUT);

pinMode(inputPin, INPUT); // declare sensor 1 as input

pinMode(inputPin2, INPUT); // declare sensor 2 as input

pinMode(buzzerPin, OUTPUT);

pinMode(switchPin, INPUT);

pinMode(switchPin2, INPUT);

digitalWrite(switchPin, HIGH);

digitalWrite(switchPin2, HIGH);

Servo1.attach(servoPin);

Serial.begin(9600);

}

void loop() {

val = digitalRead(inputPin); // read input value

if (val == HIGH) { // check if the input is HIGH

digitalWrite(ledPin1, HIGH); // turn LED ON

magnetValState = digitalRead(switchPin);

if (digitalRead(switchPin) == HIGH) {

digitalWrite(ledPin2, HIGH);

delay(1500);

digitalWrite(ledPin2, LOW);

delay(1500);

digitalWrite(buzzerPin, HIGH);

Serial.println("Door brake!");

}

if (pirState1 == LOW) {

// we have just turned on

Serial.println("Motion detected!");

// We only want to print on the output change, not state

pirState1 = HIGH;

}

}

// check sensor 2 after delay

val2 = digitalRead(inputPin2);

if (val2 == HIGH) {

digitalWrite(ledPin3, HIGH);

magnetValState2 = digitalRead(switchPin2);

if (digitalRead(switchPin2) == HIGH) {

digitalWrite(ledPin4, HIGH);

delay(50);

digitalWrite(ledPin4, LOW);

delay(50);

digitalWrite(buzzerPin, HIGH);

Serial.println("Door brake!");

}

if(pirState2 == LOW) {

// we have just turned on

Serial.println("Motion from sensor 2 detected!");

// We only want to print on the output change, not state

pirState2 = HIGH;

}

} else {

digitalWrite(ledPin1, LOW); // turn LED OFF

delay (50);

digitalWrite(ledPin2, LOW); // may be already

//playTone(0, 0);

delay(50);

digitalWrite(ledPin3, LOW); // turn LED OFF

delay (50);

digitalWrite(ledPin4, LOW); // turn LED OFF

delay (50);

if (pirState1 == HIGH) {

// we have just turned of

Serial.println("Motion ended!");

// We only want to print on the output change, not state

pirState1 = LOW;

}

if (pirState2 == HIGH) {

// we have just turned of

Serial.println("Motion ended!");

// We only want to print on the output change, not state

pirState2 = LOW;

}

}

}

someone please help me,how can i combine two pir sensor with two door sensor? im trying this coding,but only sensor 2 detected...

author
DavidB298 (author)2016-10-30

resistor needed across the LED?

author
AbD3 (author)2016-10-24

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 ?

author
GrinningCheetah made it! (author)2016-06-19

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

WIN_20160619_13_32_54_Pro.jpgWIN_20160619_13_35_33_Pro.jpg
author
DHeigh (author)GrinningCheetah2016-10-17

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.

author
shakthia1 (author)2016-10-08

good..................

author
Batman008 (author)2016-09-22

Can PIR motion sensor and arduino nano work together?

author
AndriesS (author)2016-08-29

Hi,

I have been wondering a few things.

1 does it output only high or low or is there a way to read a numeric value?

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.

If anyone knows a pointer for me to look in it is very much appreciated.

Keep up the good work.

author
chrisjlionel (author)2016-08-15

I utilized my on board SMD led pin 13. Nice instructions.

author
TuffM (author)2016-08-04

PIR sensor implemented for alarm car alarm system

author
CraigC5 made it! (author)2015-07-21

Simple. Fun. I like the buzzer idea.

Photo Jul 21, 6 30 42 PM.jpg
author
abid9509 (author)CraigC52016-04-10

can u help me sir?

author
YosefM1 (author)2016-03-03

please can you send me a code i am using a pir

author
YosefM1 (author)2016-03-03

I am useing an aurdwendo i need a code can you please help me

author
chandrashekhar k.gc (author)2016-03-02

hai in my PIR sensor the output pin always goes high even when there is
no motion,I also altered the sensitivity and delay,but there is no
change! any one help me plzz!..

author
BilalA57 (author)2016-02-25

i am starting arduino please help me to interface sensors

author
DavidB552 (author)2016-02-21

hello when I change the "long unsigned int pause = 5000;"

I do not see any change in how long the led stays on.

In fact regardless of how I long set it, it will only stay on for 2 seconds.

author
faizvalensi (author)2016-02-19

pir.pde:98:37: error: invalid operands of types 'long unsigned int' and 'int()' to binary 'operator-'

This error is presented whenever i tried to verify/upload this code.. Help Me! T_T

author
DorT1 (author)2016-01-05

Great info! thanks

Next time, and for other sensors connection you can use Circuito.io, it's a free tool to generate schematics and code

author
DIY-Guy (author)2015-12-02

Great work!
I give a "stars" rating of this Instructable at 1.2 GreggaWatts!

author
miragempro (author)2015-12-01

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

author
CaptClaude (author)2015-11-24

You have no idea how much work you just saved me. I owe you one.
I will use your code as the starting point to activate an LED art installation at University of Houston.

author
Frostfel made it! (author)2015-11-21

Ty for this Instructable

Led.jpg
author
Joekevdv (author)2015-11-13

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!

author
Txeroki made it! (author)2015-11-02

Thanks for the post!

IMG_1522.JPG
author
LauraB59 (author)2015-10-11

How can you code an LED strip instead of a single LED?

author
dynasticustom (author)LauraB592015-10-27

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

author
dynasticustom (author)LauraB592015-10-27

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

author
oussgh (author)2015-10-27

Hi,

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 ?

thank you

author
kavish laxkar (author)2015-08-01

RESPECTED SIR

hi

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.

author
KomeylD made it! (author)2015-07-14

I made it, so easy and so usable. I've an idea to add a buzzer.

IMG_2652.JPG
author
sasaakra (author)2015-02-19

I want a code to activate the PIR sensor just if a motion exists for 25 secs continuously

author
GregM8 (author)sasaakra2015-02-28

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.

http://www.ebay.com/itm/281519724893?_trksid=p2059210.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

author
insyirahanani (author)GregM82015-07-07

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.

author
AllenP3 (author)2015-06-28

Hello Frenzy,

What did you use for a sketch in the Arduino.

Thanks.

Allen in Dallas

author
gbraid (author)2015-06-11

author
ibenkos (author)2015-01-25

how do i change the led to an 5v relay? so that when its movement the relay is high. and good project :)

author
GregM8 (author)ibenkos2015-02-28

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 "In" pin on your relay (signal) to whatever output pin you selected on your Arduino . In this demonstration, it's pin 13. Done and done! :-)

author
ibenkos (author)GregM82015-03-13

yhanks ;)

author
asachs01 made it! (author)2015-01-28

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!

Photo on 1-28-15 at 11.02 PM.jpgPhoto on 1-28-15 at 11.02 PM #2.jpgPhoto on 1-28-15 at 11.02 PM #3.jpg
author
AsadB (author)2014-11-30

Hi, If there is complete darkness will the PIR still work?

author
StraydogEOD (author)AsadB2014-12-22

Yes, PIRs work off of an infrared grid setup. Think of it like looking for changes in "heat" across a bunch of zones (not 100% accurate but close enough to get the point of how it works). Light is irrelevant.

author
JahFyahh (author)2014-12-17

I like to move it move it

temp_-1598877703.jpg
author
pengocha (author)2014-11-27

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?

author
Fission Chips (author)2014-10-21

Thanks for sharing this! Very helpful for me.

author
pcame (author)2014-10-16

Yo utilice un sensor PIR como interruptor para el baño. Las manos húmedas son peligrosas, cuando no se tiene interruptor diferencial. Tiene una aprobación de 8/10 en mi casa, se resisten al cambio. Saludos

temp_-1391328749.jpg

About This Instructable

356,471views

458favorites

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 tecladoUna funda para tu dispositivo, hecha de un traje de buceoDead Drop inalámbrico
Add instructable to: