Arduino with PIR Motion Sensor

In this project you’re going to create a simple circuit with an Arduino and PIR motion sensor that can detect movement. An LED will light up when movement is detected.

Watch the video below to see how it works

Introducing the PIR Motion Sensor

The PIR motion sensor is ideal to detect movement. PIR stand for “Passive Infrared”. Basically, the PIR motion sensor measures infrared light from objects in its field of view.

So, it can detect motion based on changes in infrared light in the environment. It is ideal to detect if a human has moved in or out of the sensor range.

pir

The sensor in the figure above has two built-in potentiometers to adjust the delay time (the potentiometer at the left) and the sensitivity (the potentiometer at the right).

Pinout

Wiring the PIR motion sensor to an Arduino is pretty straightforward – the sensor has only 3 pins.

  • GND – connect to ground
  • OUT – connect to an Arduino digital pin
  • 5V – connect to 5V

Parts required

1parts

Here’s the required parts for this project

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics

Assemble all the parts by following the schematics below.

Arduino with PIR motion sensor schematics

 

Code

Upload the following code.

/*  
    Arduino with PIR motion sensor
    For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
    Modified by Rui Santos based on PIR sensor by Limor Fried
*/
 
int led = 13;                // the pin that the LED is atteched to
int sensor = 2;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)

void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  Serial.begin(9600);        // initialize serial
}

void loop(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds 
    
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds 
      
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
  }
}

View raw code

Wrapping Up

This post shows a simple example on how to use the PIR motion sensor with the Arduino. Now, you can use the PIR motion sensor in more advanced projects. For example, you can build a Night Security Light project.

If you’re an absolute beginner, and you’re just getting started, we recommend taking a look at our Free Arduino Mini Course.

Thanks for reading. If you like this post probably you might like our next ones, so please support us by subscribing our blog.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

60 thoughts on “Arduino with PIR Motion Sensor”

    • Hi!
      Not exactly, I do plan to make a more advanced tutorial in a few weeks.
      Integrate the PIR motion sensor with my website: http://homeautomationserver.com/
      So it notifies on your account and changes the state when motion is detected.
      I’m currently working on the homeautomationserver API, so we can send input data, such as temperature or sensor values.
      But it’s taking longer than I thought and I won’t have much time left in the upcoming weeks.

      Reply
  1. You should connect the data PIR connector to the GND with a resistor on your schematic to delete the no-wanted parasits signal

    Reply
  2. You need targeted visitors for your Arduino with PIR Motion Sensor | Random Nerd Tutorials website so why not try some for free? There is a VERY POWERFUL and POPULAR company out there who now lets you try their website traffic service for 7 days free of charge. I am so glad they opened their traffic system back up to the public! Check it out here: http://kururu.info/yourls/2u

    Reply
  3. Dear
    Sir I very like your project and learnd and can you tell about how to make the gsm security alarm with this project and code. Thank you very much

    Reply
  4. Sir,
    I like you PIR motion sensor with arduino project. Can you tell me how can I use this model in place of 230v switch to make it automated? Can you provide me with diagram of the circuit and the materials required in this project?
    Thank you

    Reply
  5. the PIR sensor is very sensitive. can you somehow tell me how to reduce the turning on and off of the sensor on its own…. please reply cuz i need to submit it quickly.

    Reply
  6. /*
    I NEED FURTHER EXPLANATION BECAUSE IM JUST A BEGINNER

    */

    int led = 13; // the pin that the LED is atteched to
    int sensor = 2; // the pin that the sensor is atteched to
    int state = LOW; // by default, no motion detected
    int val = 0; // variable to store the sensor status (value)

    void setup() {
    pinMode(led, OUTPUT); // initalize LED as an output
    pinMode(sensor, INPUT); // initialize sensor as an input
    Serial.begin(9600); // initialize serial
    }

    void loop(){
    val = digitalRead(sensor); // read sensor value
    if (val == HIGH) { // check if the sensor is HIGH
    digitalWrite(led, HIGH); // turn LED ON
    delay(100); // delay 100 milliseconds

    if (state == LOW) { //————————-IN THIS PART HOW TO MAKE IT STOP?
    Serial.println(“Motion detected!”);
    state = HIGH; // update variable state to HIGH //——- THERE IS NO TIME INTERVAL PUTTED HERE
    }
    }
    else {
    digitalWrite(led, LOW); // turn LED OFF
    delay(200); // delay 200 milliseconds

    if (state == HIGH){
    Serial.println(“Motion stopped!”);
    state = LOW; // update variable state to LOW
    }
    }
    }

    I dont really understand the part which motion is detected… how you make if stop if it detects a motion?

    can you please explain me further?
    thank you ! and godbless

    Reply
  7. Trying with an wemos hooked up the pir to pin 4 D2 and the led to pin 13 D7 the led blinking like crazy , tried in dark, tried the resistor between data pin of pir and GND , tried with a second brand new pir adjusted those both potentiometers on the pir all directions .. btw(why there 2 whats the role of the second? if one is sensitivity?) nothing still blinking like crazy any idea?
    thanks
    Teodor

    Reply
    • Hi Teodor.
      The best way to use the PIR with the wemos is setting it as an interrupt and set a callback function to light up the LED for a predetermined number of seconds whenever the interrupt is triggered.
      With the code in this example, you may get a lot of interference and get false positives, which results in the LED blinking.
      Regards,
      Sara 🙂

      Reply
  8. hi
    good project
    is someone able to help me on connecting an extra LDR (Light Detection) on the PIR Board ?
    I try to but it is not working
    thanks

    Reply
  9. Hello
    I am using the code provided and followed the schematic but the LED stays lit all the time, no matter the position of the potentiometers. What could the problem be. It is all new parts from a beginner kit as I am a beginner. Not sure how to trouble shoot this. Any advice?
    Thanks
    jack

    Reply
    • Hi.
      If you have a multimeter, check the output from the PIR motion sensor. Check if it changes to LOW and HIGH on the right times. Also, double-check if everything is connected properly.
      Regards,
      Sara

      Reply
  10. Hello, I need an application with a very low sensitivity. I need a sensor that does not detect movement at a distance larger than 10 centimetres. Is it possible to do that using potentiometer or should I look for different type of sensor?

    Thank you.

    Reply
  11. The PIR shown is used by many projects. I got a bunch of these to use for my ESPHome projects around the house, and all of them fail miserably, with false positives. Every few seconds with some of the sensors, to every few minutes for the better ones. If you search for this sensor, people complain about how useless they are, and the frustrations and waste of money. No amount of pull-up or pull-down resistors, added capacitors are going to fix it. I found a place they suggested to use the Panasonic EKMB or EKMC series PIR’s. You can buy these from DigiKey or Mouser. They are a little more. I got some of them, and they work perfectly. The one I got said the data pin would be VIN minus half a volt. So for my ESP32, I powered it with 3.3 volts so I wouldn’t overpower the data line to my ESP32.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.