Interface PIR Sensor With NodeMCU

49,434

60

12

Introduction: Interface PIR Sensor With NodeMCU

About: " Work until you no longer have to introduce yourself " Show some love on Instagram @it.is.prem & Motivate me on YouTube @TheCircuit

Hello Makers,

This is a simple Instructable.

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

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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.

Attachments

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.

Makerspace Contest 2017

Participated in the
Makerspace Contest 2017

Be the First to Share

    Recommendations

    • Trash to Treasure Contest

      Trash to Treasure Contest
    • Raspberry Pi Contest 2020

      Raspberry Pi Contest 2020
    • Wearables Contest

      Wearables Contest

    12 Discussions

    0
    AmbroR
    AmbroR

    7 months ago

    The minimum voltage for the HC-SR501 operation is about 4.5 Vdc.
    How can it work at 3.3Vdc ? My one does not work properly at this low voltage.
    Could you please commennt it ?
    Thanks for all
    My direct mail is iw2fvo@yahoo.com

    0
    shepler007
    shepler007

    1 year ago

    Can anyone tell me how far away distance wise I can put the PIR from the NodeMCU?

    0
    Jay KishanS
    Jay KishanS

    1 year ago

    Thank you very much.

    0
    PetarS10
    PetarS10

    1 year 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
    tothg92g
    tothg92g

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

    0
    NORS10
    NORS10

    Reply 1 year ago

    hello can you share with me

    0
    w0nders.
    w0nders.

    Question 1 year 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....

    0
    tothg92g
    tothg92g

    Answer 1 year ago

    The nodeMCU is programmed and powered via the microUSB.

    3
    Ram84
    Ram84

    Question 1 year ago on Step 6

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

    0
    SaghyB
    SaghyB

    Answer 1 year ago

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

    0
    tothg92g
    tothg92g

    Reply 1 year ago

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

    0
    Gopika97
    Gopika97

    1 year 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