HomeArduino TutorialsArduino SensorsArduino PIR Motion Sensor Tutorial: Wiring, Modes, Sensitivity & Full Codes

PIR Sensor with Arduino: Complete Interfacing Guide with All Functions and Codes

PIR sensors are one of the easiest and most useful sensors for Arduino projects. They help you detect motion in a simple and reliable way. These sensors are used in automatic lights, alarms, counters, and many smart home systems.

In this tutorial, we will learn how to interface a PIR sensor module with Arduino. We will explore all its functions, modes, and adjustments. You will get full Arduino codes for every function. We will display results on the Serial Monitor of the Arduino IDE.

PIR Sensor with Arduino: Complete Interfacing Guide with All Functions and Codes

What is a PIR Sensor and How It Works

A PIR sensor is a simple and low-cost device used to detect motion. It works by sensing infrared radiation coming from warm objects like humans and animals. Every living body emits IR energy. When this energy changes suddenly, the PIR sensor identifies it as movement. Because of its high sensitivity and low power usage, the PIR module is widely used in home security systems, automatic lights, and smart automation projects.

PIR sensor module

Understanding Passive Infrared Technology

The term “Passive” means the sensor does not send out any signals. It only receives infrared rays from the surroundings. Inside the PIR module, two IR-sensitive elements are placed side by side. When the environment is still, both elements receive the same amount of IR energy.

When a warm body moves across the sensor’s field, one element receives more IR than the other. This imbalance creates a small electrical signal. The internal circuitry then amplifies this signal and marks it as motion. This is the basic working principle of every PIR motion sensor.


PIR Sensor Pinout and Features

Most PIR sensor modules have three pins:

  • VCC – Connect to 5V
  • OUT – Digital output signal
  • GND – Ground
Image showing pinout of PIR sensor. It has 3 pins, VCC, GND and OUT.

You will also notice two adjustable knobs on the board:

  • Sensitivity Control – Sets how far the sensor can detect motion.
  • Delay Time Control – Sets how long the sensor stays HIGH after detecting motion.
Image showing the potentiometers on PIR sensor to control Hold Time and Sensitivity.

Some modules also include a small switch for two operating modes:

  • H Mode (Hold Mode) – Output stays HIGH while motion continues.
  • L Mode (Non-Hold Mode) – Output goes HIGH once even if movement continues.
Image showing the jumper on PIR sensor to control the Hold and No-Hold modes.

These features allow you to customize the sensor for different applications.


How Motion Detection Actually Happens

The Fresnel lens on top of the PIR splits the detection area into multiple zones. When a warm object moves from one zone to another, the amount of IR hitting the sensing elements changes quickly. This sudden change is detected by the PIR circuitry.

Once motion is detected:

  1. The sensor output goes HIGH.
  2. Arduino reads the HIGH signal.
  3. The system triggers your action, such as alarm, light, display message, etc.

Connecting PIR Sensor with Arduino

The Connection between a PIR motion sensor to an Arduino is very straightforward. The sensor outputs a digital HIGH whenever it detects motion, which the Arduino can easily read. In this section, we’ll walk through wiring the PIR sensor, adding the LCD1602 I2C display, and applying a few tips to ensure stable and accurate readings. The motion status will first appear on the Serial Monitor and then on the LCD screen.

1. Wiring the PIR Sensor to Arduino

Start by identifying the three pins on the PIR sensor: VCC, OUT, and GND.

  • Connect PIR VCC to Arduino 5V
  • Connect PIR GND to Arduino GND
  • Connect PIR OUT to Arduino Digital Pin 2
Image showing the wiring connection between PIR sensor and Arduino Uno.

Once connected, the sensor will output a HIGH signal on the OUT pin whenever it detects movement in front of it. The Arduino continuously monitors pin D2 and reacts whenever it sees this HIGH state.

Before testing, give the PIR sensor 30–60 seconds to warm up after powering. During this time, it may give random triggers. Also, make sure the sensor is pointed toward an open area. Avoid directing it at heat sources, sunlight, or airflow, as these can cause false triggers.


2. Power and Range Tips for Stable Readings

To get consistent results from the PIR sensor, keep the following points in mind:

Power Tips

  • Use a clean and stable 5V power supply.
  • Keep the wiring short to reduce electrical noise.
  • Always share the same ground between Arduino and the PIR sensor.

Range & Stability Tips

  • Adjust the Sensitivity knob on the PIR to control how far it detects (typically up to 6–7 meters).
  • Use the Delay knob to decide how long the output stays HIGH after detecting motion.
  • Avoid placing the sensor in direct sunlight or near heaters.
  • Keep it away from fans or strong airflow, as sudden temperature changes can cause false triggers.

PIR Sensor Functions and Complete Arduino Codes

The PIR sensor offers several useful functions. You can read basic motion, control how long the output stays HIGH, and adjust distance using the sensitivity knob. In this section, we will explore each feature with separate Arduino codes. All examples will show the result on the Serial Monitor first. Later, we will combine everything and display the output on the LCD1602 I2C screen as well.

Reading Basic Motion Detection (Digital Output)

This is the simplest way to read the PIR sensor. The OUT pin becomes HIGH when motion is detected.

Below is the code to read the signal from PIR sensor.

int pirPin = 2;

void setup() {
  pinMode(pirPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  int state = digitalRead(pirPin);

  if (state == HIGH) {
    Serial.println("Motion Detected");
  } else {
    Serial.println("No Motion");
  }

  delay(500);
}

Explanation:

The Arduino reads the PIR output from pin 2. If the value is HIGH, motion is detected. If LOW, the area is clear. We will print the result on the serial console.


Output

Below is the gif showing the output of the code. The PIR sensor detects motion, and the result is printed on the serial console of the Arduino IDE.

gif showing the PIR sensor is detecting the motion. The result for the same is printed on the serial console of Arduino IDE.

Using PIR Sensor in H-Mode (Hold Mode)

In H-Mode, the PIR sensor keeps the output HIGH as long as motion continues. If the person keeps moving, the signal stays HIGH.

Make sure to connect the jumper as shown in the image below.

Image shows how to connect the jumper on PIR sensor to put it in Hold Mode.

Below is the code to read the signal in H-Mode.

int pirPin = 2;

void setup() {
  pinMode(pirPin, INPUT);
  Serial.begin(9600);
  Serial.println("PIR Hold Mode Active");
}

void loop() {
  int state = digitalRead(pirPin);

  if (state == HIGH) {
    Serial.println("Motion Active - Holding");
  } else {
    Serial.println("No Motion");
  }

  delay(500);
}

Explanation:

In H-Mode the output remains HIGH as long as the sensor is detecting motion. Therefore the serial console will keep showing motion.

It is ideal for alarms and lights. Output stays HIGH until the movement stops.


Output

Below is the gif showing the output of the code. The PIR sensor detects motion in H-Mode, and as long as the motion continues, the Out pin remains HIGH. Hence the serial console shows the continuous motion.

gif showing the PIR sensor is detecting the motion in the H-Mode. The result for the same is printed on the serial console of Arduino IDE.

Using PIR Sensor in L-Mode (Non-Hold Mode)

In L-Mode, the PIR output goes HIGH once when motion is detected. Even if the motion continues, it sends only a single HIGH pulse.

Below is the code to read the signal in L-Mode.

int pirPin = 2;
bool triggered = false;

void setup() {
  pinMode(pirPin, INPUT);
  Serial.begin(9600);
  Serial.println("PIR Non-Hold Mode Active");
}

void loop() {
  int state = digitalRead(pirPin);

  if (state == HIGH && !triggered) {
    Serial.println("Motion Triggered Once");
    triggered = true;
  }

  if (state == LOW) {
    triggered = false;
  }

  delay(100);
}

Explanation:

In L-Mode the output goes HIGH just for a moment, even if the sensor is detecting motion. It is ideal for alarms and lights. Output stays HIGH until the movement stops.


Output

Below is the gif showing the output of the code. The PIR sensor detects motion in L-Mode. Even if the motion continues, the Out pin goes HIGH just once.

gif showing the PIR sensor is detecting the motion in the L-Mode. The result for the same is printed on the serial console of Arduino IDE.

Adjusting Sensitivity and Delay Time

PIR modules include two small onboard knobs:

1. Sensitivity Knob

  • Controls motion detection range.
  • Rotate clockwise for longer distance (up to 6–7 meters).
  • Rotate anticlockwise for shorter range.

2. Delay Time Knob

  • Controls how long the output stays HIGH.
  • Can be adjusted from a few seconds to a few minutes.
  • Turn clockwise for a longer delay.

Use these knobs to fine-tune detection for your environment and avoid false triggers.

Practical Applications Using PIR and Arduino

PIR sensors are widely used in real-world automation projects. With just a few lines of code, you can build motion-based lights, alarms, and even counting systems. In this section, we will explore three practical projects that use the PIR sensor with Arduino. Each idea is simple to build and perfect for beginners.

PIR Based Smart Light

A PIR smart light turns ON automatically when motion is detected and turns OFF when the area is clear. This setup is useful for rooms, corridors, staircases, and bathrooms.

Below is the code to implement PIR based smart light.

int pirPin = 2;
int lightPin = 8;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(lightPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int state = digitalRead(pirPin);

  if (state == HIGH) {
    digitalWrite(lightPin, HIGH);
    Serial.println("Light ON - Motion Detected");
  } else {
    digitalWrite(lightPin, LOW);
    Serial.println("Light OFF - No Motion");
  }

  delay(300);
}

Explanation

This program switches the light ON when motion is present and OFF when the PIR output becomes LOW.


Output

Below is the gif showing the output of the code. The PIR sensor detects motion, and the LED turns on. After some time, the LED turns OFF automatically.

gif showing the PIR sensor is detecting the motion and the LED turns ON.

PIR Intruder Alarm with Buzzer

This project works like a basic security alarm. When the PIR sensor detects movement, the buzzer sounds an alert.

Below is the code to implement PIR based security Alarm.

int pirPin = 2;
int buzzerPin = 9;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int state = digitalRead(pirPin);

  if (state == HIGH) {
    digitalWrite(buzzerPin, HIGH);
    Serial.println("Intruder Detected!");
  } else {
    digitalWrite(buzzerPin, LOW);
    Serial.println("Area Safe");
  }

  delay(300);
}

When the PIR detects motion, the buzzer turns ON instantly. This is perfect for doorways and restricted areas.


Motion-Based Counter System

A motion counter increments a number each time the PIR detects movement. It can be used to count people entering a room, visitors at a shop, or animals passing through a gate.

Below is the code to implement Motion-Based counter system.

int pirPin = 2;
int count = 0;
bool triggered = false;

void setup() {
  pinMode(pirPin, INPUT);
  Serial.begin(9600);
  Serial.println("Motion Counter Ready");
}

void loop() {
  int state = digitalRead(pirPin);

  if (state == HIGH && !triggered) {
    count++;
    Serial.print("Count: ");
    Serial.println(count);
    triggered = true;
  }

  if (state == LOW) {
    triggered = false;
  }

  delay(100);
}

The counter increases only once per motion trigger. It prevents multiple counts if the person stays in front of the sensor.


Output

The gif below shows the output. PIR sensor detects the motion and the counter increases. the result is printed on the serial console of the Arduino IDE.

gif showing the PIR sensor is detecting the motion and the counter is increasing. The result for the same is printed on the serial console of Arduino IDE.

Troubleshooting PIR Sensor Issues

When working with PIR sensors and Arduino, you may face issues like no detection, false triggers, or unstable readings. This section explains the common problems and how to solve them for reliable performance.

PIR Sensor Not Detecting Motion

  • Ensure the sensor is powered with a stable 5V.
  • Check the OUT pin wiring to the correct Arduino digital pin.
  • Increase the sensitivity knob if the area is large.
  • Make sure the lens is facing the motion without any obstruction.
  • Allow 10–30 seconds warm-up time after powering the sensor.

Random Triggering Fixes

  • Keep the PIR away from fans, direct sunlight, heaters, or windows.
  • Use shorter wires or shielded cables to reduce interference.
  • Add a 100 µF capacitor between VCC and GND for stable power.
  • Disable nearby sources of electrical noise (motors, relays).
  • Avoid placing PIR near reflective surfaces.

Improving Detection Accuracy

  • Point the PIR toward areas where motion naturally occurs.
  • Adjust the delay time and sensitivity knobs as per room size.
  • Use H-Mode for stable detection and L-Mode for fast response.
  • Install the sensor at a height of 5–7 feet for maximum coverage.
  • Use a fresher lens and clean the dome regularly for better detection.

Conclusion

Interfacing a PIR sensor with Arduino is one of the simplest ways to build motion-based projects. In this tutorial, you learned how the PIR sensor works, how to wire it correctly, and how to use all of its functions with complete Arduino codes. You also saw how to display the results on both the Serial Monitor and the LCD1602 I2C display.

With these basics, you can now create smart lights, alarms, counters, and many other DIY automation ideas. Keep experimenting with sensitivity, delay settings, and placement to get the best performance.
PIR sensors are affordable, reliable, and perfect for beginners as well as hobbyists looking to add motion detection to their Arduino projects.

Browse More Arduino Sensors Tutorials

1 2

Arduino IR Sensor Project Download

Info

You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.

Arduino PIR Sensor FAQs

Subscribe
Notify of

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments