Interface PIR Sensor With NodeMCU

20,067

53

9

About: Work until you no longer have to introduce yourself.

Hello Makers,

This is a simple Instructable.

In this Instructable we will learn how to interface PIR /HC-SR501 sensor with NodeMCU.

Step 1: Things Needed

We need few components to get started with the Instructable,

Hardware Components

  • NodeMCU
  • PIR Sensor / HC-SR501
  • Breadboard
  • Jumper Wires
  • Micro USB Cable
  • LED
  • 100 ohm

Software Components

  • Arduino IDE

Step 2: Description

What is a PIR sensor?

Pyroelectric / Passive InfraRed sensor : PIR sensors allow you to sense motion, generally used to detect whether a human has moved in or out of the sensors range. They are small, inexpensive, low-power, easy to use and don't wear out.

For that reason they are commonly found in appliances and gadgets used in homes or businesses. They are often referred to as PIR, "Passive Infrared", "Pyroelectric", or "IR motion" sensors.

Output : Digital pulse high (3V) when triggered (motion detected) digital low when idle (no motion detected). Pulse lengths are determined by resistors and capacitors on the PCB and differ from sensor to sensor. Sensitivity range: up to 20 feet (6 meters) 110 degrees x 70 degrees detection range.

Step 3: Circuit Connections

Connecting PIR sensors to a NodeMCU is really simple. The PIR acts as a digital output so all you need to do is operate the pin to flip high (detected) or low (not detected). Check out the images for more details.

Most PIR modules have a 3-pin connection at the side or bottom. The pinout may vary between modules so check the pinout carefully! Power is usually 3-5v DC input.

The circuit connections are made as follows :

Vcc pin of the HC-SR501 is connected to +3v of the NodeMCU.

Output pin of the HC-SR501 is connected to Digital pin D7 of the NodeMCU.

GND pin of the HC-SR501 is connected to Ground pin (GND) of the NodeMCU.

In this example we'll connect Anode pin of the LED to Digital pin D6 and Cathode pin to GND pin of NodeMCU.

Before you get started with coding you need Arduino IDE.To download Arduino IDE and for NodeMCU setup, you can check my previous instructacle.

Interfacing Servo Motor With NodeMCU

Step 4: Let's Start Coding

The code is very simple, and is basically just keeps track of whether the input to Digital pin D7 is HIGH or LOW. It also tracks the state of the pin, so that it prints out a message when motion has detected or not.

int Status = 12;  // Digital pin D6

int sensor = 13;  // Digital pin D7
void setup() {

  pinMode(sensor, INPUT);   // declare sensor as input
  pinMode(Status, OUTPUT);  // declare LED as output
}
void loop() {

  long state = digitalRead(sensor);
    if(state == HIGH) {
      digitalWrite (Status, HIGH);
      Serial.println("Motion detected!");
      delay(1000);
    }
    else {
      digitalWrite (Status, LOW);
      Serial.println("Motion absent!");
      delay(1000);
      }
}

Download the "PIR.ino" file and open it up in the Arduino IDE.
Then Create a new sketch and paste the code below in the Arduino IDE and hit Upload

You can tinker with the code to make it more useful or just use it as it is.

Step 5: Upload Code

  1. Goto Tools
  2. Board > NodeMCU 1.0 (ESP - 12E Module)
  3. Port ( Choose the right Port )

**Make sure you have your NodeMCU model selected and the correct serial port ticked (see pics). Then just click the Upload button**

Step 6: Output

Now, you can develop this instructable to a home security system and implement in many other applications.

That's it makers!

I hope you found this instructable most useful. You can contact me by leaving a comment. If you like this instructable probably you might like my next ones.

Share

    Recommendations

    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • Plastics Contest

      Plastics Contest
    • Optics Contest

      Optics Contest

    9 Discussions

    0
    None
    PetarS10

    8 weeks ago on Step 6

    Hey,

    The Vcc for the PIR sensor has to be 5-20 v. Giving it 3.3v from the nodemcu is going to make it behave unreliably I think.

    Petar

    0
    None
    tothg92g

    4 months ago

    Hello Guys!

    I have made the same project a couple of weeks ago. One hint for you: do NOT put the antenna of the nodeMCU too near to the Sensor because that way it will provide false alarms. Try to put it further from each other or you can choose low bandwith protocol (I use 802.11b) or less gain on the output (I use 0 dB). In my case the sensor and the WiFi module is next to each other, but only works if there is no wall between the router and the nodeMCU.
    You can try multiple settings to find the best solution. Maybe you can try to use some shielding between the sensor and the nodeMCU.

    1 reply
    0
    None
    NORS10tothg92g

    Reply 2 months ago

    hello can you share with me

    0
    None
    w0nders.

    Question 4 months ago on Introduction

    Do you have to have the LED? If I'm leaving this outside a room, is it even needed? Also in what part of the guide is the mico USB cable used, I must be blind....

    1 more answer
    0
    None
    tothg92gw0nders.

    Answer 4 months ago

    The nodeMCU is programmed and powered via the microUSB.

    3
    None
    Ram84

    Question 7 months ago on Step 6

    Is the PIR Sensor giving the Correct signal, i tried the same, i am getting false signals from PIR

    2 more answers
    0
    None
    SaghyBRam84

    Answer 7 months ago

    I got the same problem. It keeps saying "Motion absent!"

    0
    None
    tothg92gSaghyB

    Reply 4 months ago

    I have just created a comment about this issue. Check it out :)

    0
    None
    Gopika97

    4 months ago

    This project would work right? It can be used to test the functionality of PIR sensor, right?I am doing a project based on this for my internship,that is why