Interface PIR Sensor With NodeMCU

by CodeChamp in Circuits > Sensors

111487 Views, 84 Favorites, 0 Comments

Interface PIR Sensor With NodeMCU

Coer.jpg

Hello Makers,

This is a simple Instructable.

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

Things Needed

9.jpeg

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

Description

8.jpg
02.jpeg
5.jpeg
7.jpeg
03.jpeg
04.jpeg

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.

Circuit Connections

MCU.png
1.jpeg
14.jpg
15.jpg
17.jpg
16.jpg
13.jpg

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

Let's Start Coding

Code.PNG

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.

Downloads

Upload Code

Upload.PNG
Up2.PNG
Up3.PNG
  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**

Output

00.jpg
000.jpg

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.