Make: Projects

PIR Sensor Arduino Alarm

Build a motion-sensing alarm with a PIR sensor and an Arduino microcontroller.

PIR Sensor Arduino Alarm

In this simple project, we’ll build a motion-sensing arduino alarm using a PIR (passive infrared) sensor and an Arduino microcontroller. This is a great way to learn the basics of using digital input (from the sensor) and output (in this case, to a noisy buzzer) on your Arduino.

This arduino alarm is handy for booby traps and practical jokes, and it’s just what you’ll need to detect a zombie invasion! Plus, it’s all built on a breadboard, so no soldering required!

Download the project code here.

Steps

Step #1: Gather your parts.

PrevNext
PIR Sensor Arduino Alarm

This project requires just a few parts, and because you're using a solderless breadboard and pre-cut jumper wires, you won't need any tools at all — except your computer and USB cable to connect the Arduino.

Step #2: Wire the Arduino to the breadboard.

PrevNext
PIR Sensor Arduino Alarm
  • Connect digital input/output (I/O) pin 2 on the Arduino to row 1 on the breadboard.
  • Connect the 5V pin on the Arduino to row 2 on the breadboard, and connect a nearby ground (Gnd) pin to row 3.

Step #3: Connect your motion sensor.

PrevNext
PIR Sensor Arduino AlarmPIR Sensor Arduino AlarmPIR Sensor Arduino AlarmPIR Sensor Arduino Alarm
  • Find the Gnd (–), Vcc (+), and Out pins on the PIR sensor.
  • Plug the PIR sensor into the breadboard so that its (–) pin connects to the Gnd row, its (+) pin connects to 5V, and its Out pin connects to digital pin 2.
  • NOTE: If you have a different sensor than the one shown here, you may need to extend the sensor's pins with a stacking female header, wires, interconnect cables, etc. to fit.

Step #4: Plug in the LED.

PrevNext
PIR Sensor Arduino AlarmPIR Sensor Arduino AlarmPIR Sensor Arduino Alarm
  • Plug the LED's anode (the longer leg) into digital pin 13 on the Arduino.
  • Plug the LED's cathode (the shorter leg, and/or the leg on the flattened side of the LED base) into the adjacent ground (Gnd) pin on the Arduino.

Step #5: Connect the piezo buzzer.

PrevNext
PIR Sensor Arduino AlarmPIR Sensor Arduino AlarmPIR Sensor Arduino Alarm
  • Connect the buzzer's red wire to the Arduino's digital pin 10.
  • Connect the buzzer's black wire to the Arduino's Gnd pin (there's a spare one on the Power block of pins).
  • NOTE: These two wires can be reversed, as the polarity of the buzzer doesn't matter.

Step #6: Program the Arduino.

PrevNext
PIR Sensor Arduino AlarmPIR Sensor Arduino AlarmPIR Sensor Arduino Alarm
  • Launch the Arduino IDE software. You can download it for free here.
  • Plug the USB cable into your computer and Arduino.
  • Set the board and port settings for your Arduino board, in this case, an Arduino Uno.
  • Open the PIR Alarm sketch found at this link: https://raw.github.com/jedgarpark/Make_PIR_Sensor/master/MAKE_PIR_Sensor.pde (Arduino programs are called sketches.)
  • Upload the sketch to the Arduino.

Step #7: Test your alarm.

PrevNext
PIR Sensor Arduino AlarmPIR Sensor Arduino AlarmPIR Sensor Arduino Alarm
  • When you power up your alarm, the PIR sensor will glow an ominous red. Stand very still or leave the room while the alarm calibrates the infrared level reading for the room.
  • Now test it by moving: the buzzer will buzz and the LED will light up.
  • Be amazed! Your PIR Sensor Arduino Alarm can sense movement up to 20 feet away. No one will be stealing your yo-yo today.
  • NOTE: Might not be reliable for detecting the undead.
John Edgar Park

John Edgar Park

John Edgar Park likes to make things and tell people about it. He works in CG animation at DisneyToon Studios and writes for Make, Boing Boing, and other places online and in print. You can find him at jpixl.net and twitter @johnedgarpark -- if you like that sort of thing.


  • Goli Mohammadi

    It’s a PDF under Files at the top of the project. Have fun!

    • sanderny

      If you have the code then send me on this address email “[email protected]” please!!

    • Ethan Loewen

      where is the code. Plz HURRY

  • John Edgar Park

    Brandon, sorry for the huge delay, I didn’t see your comment until now. You can change the delay time upon startup to be longer if you need. Try adding a new line as the first one in the void setup that reads: delay(5000);
    That’ll give you five seconds before it begins.

  • Jeremy dePrisco

    I noticed when I took the code from the PDF, the formatting was off when it is copy/pasted into Arduino editor. Best to download the PDE which is on the right side of the page above.

  • Nikko Canlas

    Hi! Nice project! wanna try it too.. but the thing is, I don’t know where to download the code.. I can’t find it….. :(

  • eder

    “Were Is The Code I Can’t Find It”?

  • John Edgar Park

    Sorry about that, we’re looking into what happened. It seems when the new formatting was introduced the code was no longer included.

  • louis 0208

    Hi, nice project, im looking to do an attack sensor so its very similar to this, my components are fairly simialar, just wanted to know will they work if swtiched with the ones in the project youve used, im using a vibrations motor as the output, and a sharp PIR sensor for the input, any help or advice will be great and really appreciated.

  • Robert
  • http://www.tinkercamp.prg Blake

    The Sketch seems to have been removed from the page. Does anyone know where? I even did a search for “pdf” in the source code of this page without a result (sorry Goli Mohammadi).

  • http://onlineivan.com Ivan

    Is there code for this yet?

  • http://www.tinkercamp.prg Blake
  • Elany
  • http://Gmail Mr.MIx

    cant download this pir sensor sketch, whats the deal? someone let me know just how much of an idiot iam?

  • John Edgar Park

    Sorry for the delay in fixing the post. Here is the code, I’ll see about having it properly added to the project next.

    // Uses a PIR sensor to detect movement, buzzes a buzzer
    // more info here: http://blog.makezine.com/projects/pir-sensor-arduino-alarm/
    // email me, John Park, at [email protected]
    // based upon:
    // PIR sensor tester by Limor Fried of Adafruit
    // tone code by [email protected]

    int ledPin = 13; // choose the pin for the LED
    int inputPin = 2; // choose the input pin (for PIR sensor)
    int pirState = LOW; // we start, assuming no motion detected
    int val = 0; // variable for reading the pin status
    int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)

    void setup() {
    pinMode(ledPin, OUTPUT); // declare LED as output
    pinMode(inputPin, INPUT); // declare sensor as input
    pinMode(pinSpeaker, OUTPUT);
    Serial.begin(9600);
    }

    void loop(){
    val = digitalRead(inputPin); // read input value
    if (val == HIGH) { // check if the input is HIGH
    digitalWrite(ledPin, HIGH); // turn LED ON
    playTone(300, 160);
    delay(150);

    if (pirState == LOW) {
    // we have just turned on
    Serial.println(“Motion detected!”);
    // We only want to print on the output change, not state
    pirState = HIGH;
    }
    } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    playTone(0, 0);
    delay(300);
    if (pirState == HIGH){
    // we have just turned of
    Serial.println(“Motion ended!”);
    // We only want to print on the output change, not state
    pirState = LOW;
    }
    }
    }
    // duration in mSecs, frequency in hertz
    void playTone(long duration, int freq) {
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
    digitalWrite(pinSpeaker,HIGH);
    delayMicroseconds(period / 2);
    digitalWrite(pinSpeaker, LOW);
    delayMicroseconds(period / 2);
    elapsed_time += (period);
    }
    }

  • JesstheBess

    Would it be possible to just run this off a battery?

    • John Edgar Park

      Yes, you could run this off of batteries. I’ve done so and it works fine. I usually use 4 AA NiMH batteries.

  • Arjun patel

    can you give me pir for 8051 controller

  • Dave

    Thanks this is simple and neat. My nine year old son wanted to “invent” a device that scared off the birds under our deck so they didn’t make nests. So we used these plans. Going to try an add a camera to snap pics of it scaring off birds. Though knowing these birds they’ll just make a nest on top of it.

    • John Edgar Park

      Excellent, did you and your son build the Duck Alarm?

  • Ayupchap

    Hi, I seem to have trouble running the code it says “Cannot find anything named LOW” I have copied the code over, any idea what might be wrong?

    • Ayupchap

      Scrap that, I worked out whats wrong. Essentially I am an idiot, I had opened the code up in Processing and not Arduino. Can someone tell me how to add a Camera to take photos to this please?

      • John Edgar Park

        One straightforward way would be to use a camera that has an input for a remote switch, and have the Arduino close that for you with a transistor or relay. I made my own release switch: http://blog.makezine.com/2010/11/10/john-edgar-park-has-a-trigger-mouth/ using a 2.5mm phone earbud, you’d trigger it with the Arduino instead of the arcade button.

  • TommyTDesign

    Great project!

    I was wondering if anyone could help me tweak the arduino sketch?

    What code could I insert to make it so that there is a 3 second delay between the PIR receiving a signal and the outputs?

    • Brian

      There are other ways to make a delay, but the block of code using the Timer library allows for other actions to take place by using pulses to define your time-domain. If you use a Delay function that locks up the MCU until the delay is completed. You can find details at: http://playground.arduino.cc/Code/Timer

  • http://www.facebook.com/vidhiss Vidhi Shah

    Hey, is there a way to attach a servo instead? And what changes should be made to the code for that?

  • metamorp

    what code if i use pir and keypad,
    keypad for on/of that sensor

  • Ted

    My LED on pin 13 wont turn on, the PIR works fine, the only difference is I’m using a Duemilanove, any ideas.

  • http://www.tinkercamp.org Blake

    This was a fun build to learn a little about the motion sensor. However, it is just not loud enough to keep the dogs from getting up on the table. :-(

    What I need is a simple circuit to boost the output signal to drive one of the Radio Shack sirens (9-12 volt). Or a modification that turns the output into a simple switch to turn on a separately powered 9 volt circuit connected to the siren.

    I have tried a couple of ideas from Hack a Week and Instructables but no success yet. How about a little help?

  • Ronaldo Panaligan

    It may not Compile, the only error is you may have to retype the this character (“motion detected”) and (“motion ended”). It has to do with the way these character shows up on the internet. Thanks I’m going to try it now.

  • Alex

    Do you have the Electrical Schematic for this configuration?

  • https://www.facebook.com/misfits002 Julius Edward Asuncion

    GOOD DAY SIR i use RE200B pir sensor and the buzzer and the led is blingking and the buzzer is continuesly sounds up ….. is there a problem if i dont use the kind of pir that you use sir > ? help me please ..

    are RE200B AND YOUR PIR SENSOR THE SAME ?

  • Sandy

    Sir I want Source code in ASM Language will you please help me.

  • Zoey

    Hi..do you have block diagram for this project?

  • Ben

    Hi nice post!
    I’m looking for a PIR sensor that its delay time is less than 5 seconds.
    Do you know one that can answer my needs?
    Thanks
    Ben

  • priyanka saini

    hie nice project… i need arduino software… pls give me dat site

    • priyanka saini

      i juz got :)

  • Dombo

    Great tutoriol….!!!!!!
    I have search the web for asolution to switch with a pir my bedledlight on and off. And this great.
    Can you please tell me how i can change the buzzer code in to a ction to open the mosfet…
    Or a signal where the powerr on is for x seconds,please..

    Or can i set the mosfet to pin 13?

  • https://www.facebook.com/huda.daus HuDa Affandi

    sorry..how if i want when the PIR sensor detect the motion,then it will send the message via old cellphone to another cellphone to tell that the motion is detected..

  • alex

    How can you change the duration of the buzzer?

  • Pingback: Idéias para automação residencial com Arduino | Fazedores()

  • http://gravatar.com/manoellemos mlemos

    Hi, I’m writing a blog post about cool Arduino based projects and your’s is going to be there. Do you allow me to use one or two of your pictures in the post? I’m linking the references to the project back to this page.

    Thanks in advance!

    Manoel Lemos

    • Dombo

      Please some one make my day….
      My Pir A is working only Pir B not.
      Where do i go wrong…

      [code]// Twee pirren onder het bed…
      // Pir A en Pir B
      //Pir B werkt niet…

      int ledPin = 12; // choose the pin for the LED
      int inputPin = 2; // choose the input pin (for PIR sensor)
      int inputPinB = 3; // PIRB
      int pirState = LOW; // we start, assuming no motion detected
      int pirStateB = LOW; // PIR B
      int val = 1; // variable for reading the pin status
      int valB = 1; // PIR B
      void setup() {
      pinMode(ledPin, OUTPUT); // declare LED as output
      pinMode(inputPin, INPUT); // declare sensor as input
      pinMode(inputPinB, INPUT); // PIr B
      Serial.begin(9600);
      }
      //////////////// PIR A
      void loop(){
      val = digitalRead(inputPin); // read input value
      if (val == LOW) { // Zorgt dat hij de goed "richting"op gaat High is de pir anders om
      digitalWrite(ledPin, HIGH); // turn LED ON
      delay(100); // uitlezen van ?
      ////////////////
      if (pirState == HIGH) { //
      Serial.println("Motion detected!"); //
      pirState = LOW;
      delay(100); // weet niet maar zal iets met uitlezen zijn.
      ///////////////////////////
      }
      } else { digitalWrite(ledPin, LOW); //

      if (pirState == LOW){ // we have just turned of
      Serial.println("Motion ended!"); //
      pirState = HIGH;
      delay(10000); // Tijd lamp aan is
      }
      }
      }
      /////////////////////

      //}
      ////////////////PIR B
      void loopB(){
      valB = digitalRead(inputPinB); // read input value
      if (valB == LOW) { // Zorgt dat hij de goed "richting"op gaat High is de pir anders om
      digitalWrite(ledPin, HIGH); // turn LED ON
      delay(100); // uitlezen van ?
      ////////////////
      if (pirStateB == HIGH) { //
      Serial.println("Motion detected!"); //
      pirStateB = LOW;
      delay(100); // weet niet maar zal iets met uitlezen zijn.
      ///////////////////////////
      }
      } else { digitalWrite(ledPin, LOW); //

      if (pirStateB == LOW){ // we have just turned of
      Serial.println("Motion ended!"); //
      pirStateB = HIGH;
      delay(10000); // Tijd lamp aan is
      }
      }
      }
      [/code]

      So please…
      Whe you have time…

  • Pingback: Ideias para automação residencial com Arduino | iMasters()

  • Pingback: Arduino UNO - Stair Lighting Project - How To | Axelology()

  • Pingback: Note to Self: Hooking Up an Arduino Uno to WiFi So It Can Text Me | timshaya()

  • Ben

    How do I reduce the length of time it buzzes?

    • troll

      reduce time delay after it triggers the buzzer

  • Pingback: PIR Sensor | Interactive Costumes()

  • Mohammed

    Hi, I am using a different sensor, which has 3 wires (Red,white, Black). Red is Marked as +ve and and black is marked as “AL”. Kindly let me know which OUT wire and negetive wire..

    • fraggie

      Out is probably white , as red and black is mostly used for plus and minus voltage input..

  • MySensors

    You can also make the motion sensor wireless by following our guide here:

    http://www.mysensors.org/build/motion

    Cheers
    Henrik

  • ToxicT

    Cool! I used this code to make an alarm for my sneaky 3-yo who started getting up in the middle of the night (very stealthy). I tested it last night and sure thing it worked great! I’ll include some pics.

    • sri

      Hello,
      Is it possible to trigger the PIR only if a person is standing for more than 10 Secs, this is to avoid triggering for passersby. If so kindly share how this can be achieved.

      Thanks
      Sri

      • ToxicT

        I’m not an expert on this but you should look into “retriggering” and “non-retriggering” mode. I think using your PIR-sensor in “retriggering” mode (with the jumper on the back set to H) is the way to go. Then you can probably monitor the time the sensor is on an in software and set you threshold there. I.e when the sensor triggers, wait 10 seconds before doing anything.

        I found this instructable that explains this in more detail.
        http://www.instructables.com/id/PIR-Motion-Sensor-Tutorial/step5/Retriggering/

      • troll

        when it receives a signal, add a delay for 10 seconds then if there’s still a signal set off the alarm.

  • Guest

    Pic2

  • ToxicT

    Pic 2 (of insides)

    • Mohammad Asim Siddiqui

      Is it working 100%because I have the same PIR sensor and buzzer that u got , like in my PIR sensor there is not written which pin is of GND, VCC and Vout
      plz helpme bcoz my circuit is constantly ring alarm.

  • Ali Pour

    Hi, can I use one of the components like the one in this page:
    http://uk.rs-online.com/web/p/ir-receivers/0577897/

    to build this circuit. It is just that I dont know the difference between IR receivers and PIR.

    thanks
    ali.

    • Kerry Trantham

      The difference is PIR sensor Senses Infrared Light, IR reciever looks for Infrared pulsed signals, such as TV B GONE OR TV REMOTES I do believe

      • troll

        almost, pir senses body-emitted infra red or (rarely) heat but ir reciever takes any ambient light (not only signals)

  • Eduard

    Very nice project. In a few minutes I was able to test a PIR sensor I received today.

  • Stephen

    Hi guys, my alarm rings constantly as soon as I upload the sketch. Any thoughts on what the issue could be?

    • troll

      maybe a short circuit, or wrong polarity

    • Mohammad Asim Siddiqui

      Hey Stephen what happened , Is ur circuit working? now? I mean do you still have a problem of constantly buzzing of the buzzer??

  • Mohammad Asim Siddiqui

    where is PIR sensor in proteus?

  • Mohammad Asim Siddiqui

    what is the range of this PIR sensor?

    • Pradeep

      Up to 20 feet(6 meters)

      • Mohammad Asim Siddiqui

        thanx Pradeep

  • Nilesh

    Hi,
    Thanks for the wonderful tutorial on using PIR sensors. Works like charm! However, I ran into a minor issue with this. It works all perfect when I have my Arduino connected using the USB cable via my laptop. However, whenever I connect this using a 9v battery, the buzzer starts buzzing and doesn’t stop. I suspect I’m not using the right kind of battery. Using a USB cable, it works perfectly.

    Any idea on how this can be fixed?

  • Mohammad Asim Siddiqui

    Hey my circuit is constantly ringing alarm plz help me??

  • Mohammad Asim Siddiqui

    Now it’s working because I have tested it by varying two potentio meters in PIR sensor

  • Mohammad Asim Siddiqui

    How can we increase the volume of speaker?

  • Pingback: PIR Sensor Arduino Alarm | MAKE | olliesshowcase()

  • Pingback: And the Winner is… | Building & Hacking @ York College (CT 137)()

  • waheed

    how we can control the working temerature of pir sensors eg.my project is that detect the moving body whos temerature is above then 40 degree c. plz help me

  • Nidhish Lakhinana

    can you make the same project without using a pc or a laptop

  • Christian Duke Pasiona Tugay

    Sir I would like to say my gratitude to you and to your helpful work. I follow the instruction carefully and it was successful. But the difference is the sensor I used is a vibration sensor (SW-420). But it just work perfectly fine. anyway Sir, thank you for the reference you made. More Power. God Bless.

    • Kevin

      Can you tell me what you used for output voltage on the SW-420 to operate the buzzer?
      My understanding is the output is a digital signal.
      Thanks for any help you can offer.

  • Pingback: ARDUINO WIRELESS HOME SECURITY SYSTEM -Use Arduino for Projects()

  • Tom Garty

    made this but i have no idea of c sharp/arduino encoding and i am making it for my cat’s to trigger when they jump on the counter so i want it to triger once every time but the way it’s encoded it seems that it beeps like a car alarm and does not stop… what to do?
    and by the way: great work here and great thinking so thank you thery much!

  • bisry

    how to limit the range of pir sensor because i need to use the sensor in short range around 1 meter.

    • Altoidian

      You can install a small tube of paper around the sensor head, in effect, narrowing the field of view. Imagine looking at the world through a straw. I used a section of tubre cut from the inside tube from an old roll of waxed paper. A toilett paper roll tube would also do. Re-aim the senor to take in only a portion of the area in which you expect movement. You can also re-adjust the opening of the paper tube with masking take to reduce the area even more. You will have to experiment a bit to get jsut what you are needing for your project.

  • Pingback: Sensores – Sensor PIR | Guia Arduino()

  • Greasy Bus

    I am attempting to add a reed switch to this design. Where in the code would I add the coding for this in and what would it look like?

  • Pingback: Best of 2012: Weekend Projects - Make: | Make:()

  • Pingback: Tweaking4All.nl - Spelen met een bewegingssensor (PIR Sensor)()

  • qwerfghnmhgfdewedrfghjkhgfdsad

    hey guys

  • qwerfghnmhgfdewedrfghjkhgfdsad

    I like porn

  • qwerfghnmhgfdewedrfghjkhgfdsad

    smd

  • qwerfghnmhgfdewedrfghjkhgfdsad

    fdghjk

  • qwerfghnmhgfdewedrfghjkhgfdsad
  • aaron

    can someone tell me why the program is saying error compiling!!??

  • abhay

    hi my name is abhay

    i work with an intel galileo

    when i uploaded the code to it it is just glowing the led it is not responding at all

    please help

    the code is

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

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

    int pirState = LOW; // we start, assuming no motion detected

    int val = 0; // variable for reading the pin status

    int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)

    void setup() {

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

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

    pinMode(pinSpeaker, OUTPUT);

    Serial.begin(9600);

    }

    void loop(){

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

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

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

    playTone(300, 160);

    delay(150);

    if (pirState == LOW) {

    // we have just turned on

    Serial.println(“Motion detected!”);

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

    pirState = HIGH;

    }

    } else {

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

    playTone(0, 0);

    delay(300);

    if (pirState == HIGH){

    // we have just turned off

    Serial.println(“Motion ended!”);

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

    pirState = LOW;

    }

    }

    }

    // duration in mSecs, frequency in hertz

    void playTone(long duration, int freq) {

    duration *= 1000;

    int period = (1.0 / freq) * 1000000;

    long elapsed_time = 0;

    while (elapsed_time < duration) {

    digitalWrite(pinSpeaker,HIGH);

    delayMicroseconds(period / 2);

    digitalWrite(pinSpeaker, LOW);

    delayMicroseconds(period / 2);

    elapsed_time += (period);

    }

    }

  • Pingback: Week 1 – New semester & New topic | Creative Technology()

  • Pingback: Rough Basic Circuit | Creative Technology()