PIR Interfacing with NodeMCU

Overview of PIR Motion Sensor

PIR Motion Sensor
PIR Sensor


PIR sensor is used for detecting infrared heat radiations. This makes them useful in the detection of moving living objects that emit infrared heat radiations.

The output (in terms of voltage) of the PIR sensor is high when it senses motion; whereas it is low when there is no motion (stationary object or no object).

PIR sensors are used in many applications like for room light control using human detection, human motion detection for security purposes at home, etc.

For more information on the PIR sensor and how to use it, refer to the topic PIR Sensor in the sensors and modules section.


Connection Diagram of PIR Sensor with NodeMCU

PIR Interface with NodeMCU
PIR Interface with NodeMCU




  • PIR sensor: Never keep PIR Sensor close to the Wi-Fi antenna, ESP32, or NodeMCU.
  • PIR (Passive Infrared) sensor close to a WiFi antenna impacts the sensor's performance.
  • PIR sensors detect changes in infrared radiation for motion detection.
  • WiFi signals emit electromagnetic radiation that can interfere with the PIR sensor. Which causes false detection.
  • So always keep the PIR sensor and WiFi antenna as far apart as possible.
  • Also, you can try to shield the PIR sensor from the WiFi signal. This can be done by using metal shields or Faraday cages around the PIR sensor.


PIR Sensor NodeMCU Example

Let’s interface the PIR sensor with NodeMCU. When motion is detected, PIR output goes HIGH which will be read by NodeMCU. So, we will turn on LED when motion is detected by the PIR sensor. LED is connected to the D4 pin.

Here we are writing Arduino Sketch as well as Lua script for NodeMCU. To know more about NodeMCU development using Arduino sketch or Lua script refer to Getting started with NodeMCU using Arduino IDE and Getting started with NodeMCU using ESPlorer IDE


Lua Script for PIR

PIRpin = 1
LEDpin = 4

gpio.mode(PIRpin, gpio.INPUT)
gpio.mode(LEDpin, gpio.OUTPUT)

while true do
   gpio.write(LEDpin, gpio.read(PIRpin))


Arduino Sketch for PIR

int LED = D4;
intPIR_Input = D1;

void setup(){

void loop() {
 digitalWrite(LED, digitalRead(PIR_Input));

Components Used

PIR Sensor
PIR motion sensors sense the Infrared signal radiated from moving objects including human or animal body. It is generally used to detect the presence of human or animal motions.
LED 5mm
LED 5mm


NodeMCU PIR source codes Download