Introduction: Interface PIR Sensor With NodeMCU
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.
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
- Goto Tools
- Board > NodeMCU 1.0 (ESP - 12E Module)
- 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.

Participated in the
Makerspace Contest 2017
1 Person Made This Project!
- syeddaniyalali made it!
15 Comments
Question 3 months ago on Step 4
It is asking stay '#' while uploading the program
Question 2 years ago on Step 1
dont we need buzzer in this project ?
Question 2 years ago on Step 6
can you make a guide when using 2 PIR on ESP8266 bord
3 years 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
4 years ago
Can anyone tell me how far away distance wise I can put the PIR from the NodeMCU?
4 years ago
Thank you very much.
4 years 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
4 years 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.
Reply 4 years ago
hello can you share with me
Question 4 years 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....
Answer 4 years ago
The nodeMCU is programmed and powered via the microUSB.
Question 4 years ago on Step 6
Is the PIR Sensor giving the Correct signal, i tried the same, i am getting false signals from PIR
Answer 4 years ago
I got the same problem. It keeps saying "Motion absent!"
Reply 4 years ago
I have just created a comment about this issue. Check it out :)
4 years 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