Detecting Motion Using the HC-SR501 Passive Infra-Red (PIR) Sensor and NodeMcu

The PIR sensor is used to detect movement in front of the sensor. The sensor, which is coded as “HC-SR501” on the market, is made of pyroelectric material that can detect infrared radiation. The PIR sensor works by capturing infrared rays, then it will enter through the Fresnel lens and hit the pyroelectric sensor, infrared rays containing heat energy make the pyroelectric sensor can generate an electric current. This electric current will cause a voltage and can be read by the microcontroller. The PIR sensor (HC-SR501) is available in the form of a module as shown in the picture below.

This sensor can detect the movement of objects as far as 5-7 m with a beamwidth of 110º. The PIR sensor has 3 pins, namely Vcc, Vout, and GND. In this tutorial, the PIR sensor is used to detect motion, if motion is detected, the LED indicator will be active, otherwise, if the PIR sensor does not detect motion, the LED will not be active. To better understand how to use a PIR sensor, let’s do a simple experiment below

Tools and Materials for HC-SR501-NodeMCU Experiment

  1. NodeMCU
  2. PIR sensor (HC-SR501)
  3. Resistor 220Ω
  4. LED
  5. Jumper cables (female-male)

Then make a circuit like in the picture below

Meanwhile, the pin configuration is as follows:
• The VCC pin is connected to the 5V pin of Arduino Uno
• The Out pin is connected to the A0 pin of Arduino Uno
• The GND pin is connected to the GND pin of Arduino Uno
• The LED anode is connected to the A1 pin of Arduino Uno

PIR HCSR501-UNO Source Code

Open the Arduino IDE software on your laptop. If you don’t have it, you can download it at Arduino.cc. Then from the Arduino IDE Code Editor, write the following code:

int led = D5; // LED anode declaration on A1 pin NodeMCU
int PIR = D4; // PIR Sensor Out pin Declaration on A0 pin
int data = 0; // variable to save PIR data output

void setup() {
pinMode(led, OUTPUT); //set pin A1 as output
pinMode(PIR, INPUT); // set pin A0 as input
Serial.begin(115200); // serial monitor setup
}

void loop(){
  data = digitalRead(PIR); // read sensor digital value
  Serial.print("Logic PIR: "); // display digital sensor value on serial monitor
  Serial.print(data);
  Serial.print(".\n");
  delay(1000);
  
  if (data == HIGH){ // Instructions to activate the LED if motion is detected
  digitalWrite(led, HIGH); // Activate the LED indicator
  Serial.println("Motion Detected");
}

  else if (data == LOW){ // Instructions to turn off the LED if no motion is detected
  digitalWrite(led, LOW); // Turns off the LED if motion is not detected
  Serial.println("No motion");
}
}

Upload the above sketch to your Arduino IDE by pressing the upload button or via hotkey CTRL+U, and confirming the NodeMCU (ESP12-E Module) on Tools-Board menu settings. Then activate the ‘Serial Monitor’ tool in the Arduino IDE via the Tools – Serial Monitor menu. Make sure your USB cable is connected between the laptop and Arduino. After the program is finished uploading, please watch the digital value on your Serial Monitor window. And look at your experiment circuit, it should look like the following pictures (LED will be ON when motion is detected)

The code above controls the LED to ON / OFF based on the digital value that the Arduino reads from the PIR sensor. When the PIR sensor detects movement, the digital value read by Arduino is HIGH or logic “1”, while when the PIR sensor does not detect movement, the digital value read by Arduino is LOW or logic “0”. This digital value is used as a parameter to control the LED. Congrats, now you can detect object movement using The PIR HC-SR501 sensor and NodeMCU. If you want to connect to an IoT server (or broker) you can easily send data using a WIFI connection. Stay tuned for the next tutorial 🙂

Leave a Reply