Pantech.AI

How to Interface PIR Sensor with Arduino UNO

Overview of Motion Sensor

A PIR sensor detects infrared heat radiation, making it ideal for applications that involve detecting moving living objects that emit infrared radiation.

The PIR sensor output is high (voltage) when motion is detected, and low when there is no motion (either a stationary object or no object present).

For more information on the PIR sensor and its usage, see the PIR Sensor topic in the sensors and modules section.

Connection Diagram of PIR Sensor with Arduino

Interfacing PIR Sensor with Arduino UNO

Note:

Keep the PIR sensor away from Wi-Fi antennas, ESP32, or NodeMCU boards, as proximity to these can impact the sensor’s performance.

PIR (Passive Infrared) sensors detect motion by sensing changes in infrared radiation. However, Wi-Fi signals emit electromagnetic radiation that can interfere with the PIR sensor, potentially causing false detections.

To minimize interference, always position the PIR sensor and Wi-Fi antenna as far apart as possible. Additionally, you can shield the PIR sensor from Wi-Fi signals using metal shields or a Faraday cage around the sensor.

Detect Motion using PIR Sensor and Arduino Uno

Motion Detection of Living Objects Using a PIR Sensor with Arduino

When the PIR sensor detects motion, “Object detected” is displayed on the Arduino serial monitor. If no motion is detected, the message “No object in sight” is shown on the serial monitor.

PIR Sensor code for Arduino Uno

const int PIR_SENSOR_OUTPUT_PIN = 4;	/* PIR sensor O/P pin */
int warm_up;

void setup() {
  pinMode(PIR_SENSOR_OUTPUT_PIN, INPUT);
  Serial.begin(9600);	/* Define baud rate for serial communication */
  delay(20000);	/* Power On Warm Up Delay */
}

void loop() {
  int sensor_output;
  sensor_output = digitalRead(PIR_SENSOR_OUTPUT_PIN);
  if( sensor_output == LOW )
  {
    if( warm_up == 1 )
     {
      Serial.print("Warming Up\n\n");
      warm_up = 0;
      delay(2000);
    }
    Serial.print("No object in sight\n\n");
    delay(1000);
  }
  else
  {
    Serial.print("Object detected\n\n");    
    warm_up = 1;
    delay(1000);
  }  
}

Connection Diagram of PIR Sensor with Arduino Uno Interrupt

PIR Sensor Interfacing with Arduino using interrupt

PIR Sensor code for Arduino Uno using Interrupt

const int PIR_SENSOR_OUTPUT_PIN = 2;  /* PIR sensor O/P pin */

void setup() {
  pinMode(PIR_SENSOR_OUTPUT_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), pir, FALLING);  /* Interrupt on rising edge on pin 2 */
  Serial.begin(9600); /* Define baud rate for serial communication */
  delay(20000); /* Power On Warm Up Delay */
}

void loop() {
}

void pir(){
  Serial.println("Object Detected");
}

Leave a Comment

Your email address will not be published. Required fields are marked *