Arduino Basics: PIR Sensors

372,765

464

67

About: I'm a full stack web developer focusing on security and privacy.

Intro: 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!

6 People Made This Project!

Recommendations

  • Electronics Tips & Tricks Challenge

    Electronics Tips & Tricks Challenge
  • Plastics Contest

    Plastics Contest
  • Optics Contest

    Optics Contest

67 Discussions

0
None
montagneblue

10 months ago

Hi

Good explanation, program works fine.

But its better to use 330 ohms in series with the LED
thanks to the atmel controller the circuit survives, With other controllers you are less Lucky.

best regards

Gerard

0
None
ooha

2 years ago

What does pirstate=HIGH mean in the code?

1 reply
0
None
Otniel96ooha

Reply 1 year ago

it means pir sensor ill on, after its detected something

0
None
MariaDabukke

1 year ago

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?

0
None
Graham Mantel

1 year ago

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

0
None
NurulF13

1 year ago

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...

0
None
DavidB298

1 year ago

resistor needed across the LED?

0
None
AbD3

1 year ago

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 ?

0
None
shakthia1

2 years ago

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

0
None
Batman008

2 years ago

Can PIR motion sensor and arduino nano work together?

0
None
AndriesS

2 years ago

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.

0
None
chrisjlionel

2 years ago

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

0
None
TuffM

2 years ago

PIR sensor implemented for alarm car alarm system

0
None
YosefM1

2 years ago

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

0
None
YosefM1

2 years ago

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

0
None
chandrashekhar k.gc

2 years ago

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!..

0
None
BilalA57

2 years ago

i am starting arduino please help me to interface sensors

0
None
DavidB552

2 years ago

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.

0
None
faizvalensi

2 years ago

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

0
None
DorT1

2 years ago

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