Arduino Basics: PIR Sensors




Posted in TechnologyArduino

Introduction: Arduino Basics: PIR Sensors

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

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


  • Spotless Contest

    Spotless Contest
  • Microcontroller Contest

    Microcontroller Contest
  • Space Challenge

    Space Challenge

We have a be nice policy.
Please be positive and constructive.





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



What does pirstate=HIGH mean in the code?

1 reply

it means pir sensor ill on, after its detected something

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?

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

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);




void loop() {

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


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

magnetValState = digitalRead(switchPin);

if (digitalRead(switchPin) == HIGH) {

digitalWrite(ledPin2, HIGH);


digitalWrite(ledPin2, LOW);


digitalWrite(buzzerPin, HIGH);

Serial.println("Door brake!");


if (pirState1 == LOW) {

// we have just turned on

Serial.println("Motion detected!");


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);


digitalWrite(ledPin4, LOW);


digitalWrite(buzzerPin, HIGH);

Serial.println("Door brake!");


if(pirState2 == LOW) {

// we have just turned on

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


pirState2 = HIGH;


} else {

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

delay (50);

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

//playTone(0, 0);


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!");


pirState1 = LOW;


if (pirState2 == HIGH) {

// we have just turned of

Serial.println("Motion ended!");


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

resistor needed across the LED?


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 ?


Can PIR motion sensor and arduino nano work together?


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.

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

PIR sensor implemented for alarm car alarm system

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

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

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

i am starting arduino please help me to interface sensors

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.

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

Great info! thanks

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