Connecting a PIR Motion Sensor to an Arduino Is Simpler Than You Think, But It Will Make You Pull Your Hair Out If You Skip the Details

A PIR sensor is a passive infrared motion detector. It senses changes in infrared radiation, which happens when a warm body moves across its field of view. The sensor outputs a digital high signal when motion is detected and a low signal otherwise. That is the entire principle. Nothing more complicated than that. The sensor I use most often is the HC-SR501. It costs about two dollars, it is available everywhere, and it works well enough for hobby projects. It runs on 5 volts and draws around 65 milliamps during operation. The output is a clean 3.3-volt logic high, which means you can wire it directly to an Arduino pin without level shifting or resistors. Do not skip the datasheet reading. Even a quick glance tells you which pin is which, and guessing based on color-coded wires will waste your time.

Ultimate Pir Sensor With Arduino A Beginners Guide From Setup To Code

Before writing any code, connect the hardware. I am going to assume you have an Arduino Uno or a compatible board, a breadboard, jumper wires, and the HC-SR501 module. Pin out is straightforward: VCC to the 5-volt rail, GND to ground, and OUT to any digital pin. I used pin 7 in my builds because it keeps the serial monitor free for debugging. That is a personal preference, not a requirement. Here is the wiring layout: VCC on the sensor goes to the 5V pin on the Arduino.
GND on the sensor goes to a GND pin on the Arduino.
OUT on the sensor goes to digital pin 7.

That is it. Three wires. You do not need a current-limiting resistor on the signal line. You do not need pull-up or pull-down resistors either, because the HC-SR501 has the pull-up built into its output stage. This surprises a lot of beginners who expect to wire resistors like they would with a pushbutton. Do not add them. The sensor handles its own biasing. Now for the code. I keep it minimal so you can actually read what is happening. This is the kind of sketch I start every project with:

Get the Full Details

How To Use A Pir Sensor With An Arduino at Tracie Greathouse blog
How To Use A Pir Sensor With An Arduino at Tracie Greathouse blog
const int pirPin = 7;
bool lastState = false;

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

void loop() {
  bool currentState = digitalRead(pirPin);
  if (currentState != lastState) {
    if (currentState == true) {
      Serial.println("Motion detected");
    } else {
      Serial.println("No motion");
    }
    lastState = currentState;
  }
}

The first thing you will notice is the 60-second delay in setup(). That is not optional for most HC-SR501 modules. They need a one-minute stabilization period after power-up to calibrate their baseline infrared readings. If you skip this, the sensor will return false positives for the entire first minute, sometimes longer. I have seen it happen repeatedly. One project I worked on had a customer complain that the alarm triggered every time someone walked into the room within the first three minutes of installation. We replaced the sensor thinking it was faulty. It was not. The installation guide that came with it did not mention the warmup period at all. We added the delay and the problem went away. The logic in the loop checks for state changes rather than simply printing whenever the sensor fires. This prevents the serial monitor from filling up with repeated "Motion detected" lines while someone is standing still in the detection zone. The sensor stays high for a few seconds after motion stops, determined by the time-delay potentiometer on the module. Adjusting that screw clockwise increases the delay, counterclockwise decreases it. The range is roughly 3 seconds to 5 minutes depending on the model. There is another detail people miss. The sensitivity potentiometer controls detection range. Turning it clockwise increases the range up to about 7 meters under ideal conditions. Realistically, indoors with average ceiling height, you are looking at 4 to 5 meters. Outdoors with no shielding, wind, or heat sources nearby, you might squeeze out 6 or 7. Beyond that, you are chasing false triggers from distant heat sources like car engines or sunlight reflecting off metal.

I once deployed a sensor in a hallway where an HVAC vent blew warm air directly across the detection zone. The system triggered once every four minutes like clockwork, coinciding with the heater cycling on. The fix was not a software solution. I repositioned the sensor so the vent was outside its field of view and added a small baffle made from cardboard to block the direct airflow. Software filtering would have worked too, but it would have also made the sensor slower to respond to real motion. Sometimes the hardware fix is the right fix. If you want to add LED indicators or trigger a relay, connect them to a different pin and control them from code. The PIR output pin should stay dedicated to reading the sensor. Mixing actuator control and sensor reading on the same pin creates race conditions and unpredictable behavior. One more thing about power. The HC-SR501 can run from a USB port or a 9-volt battery with a regulator. But if you are powering it from a battery and the Arduino from USB, make sure both grounds are connected. Floating grounds cause intermittent sensor readings that make no sense until you trace the ground loop. I spent an afternoon diagnosing what I thought was a bad sensor. The module was fine. The ground wire was loose on the breadboard.

There are limitations you should be aware of before building anything permanent with this sensor. PIR sensors detect movement, not presence. If someone sits still in the detection zone for longer than the time-delay setting, the sensor goes back to idle. It will not tell you they are still there. For occupancy counting or security systems where you need to know if a room is still occupied, you need additional logic, like resetting a timer on each motion event, or you need a different sensor type altogether, such as an ultrasonic distance sensor or a mmWave radar module. PIR sensors also struggle in extreme temperatures. They are designed to detect the difference between a human body and the background. If the ambient temperature approaches body temperature, which can happen in hot climates or unventilated spaces, the contrast drops and the sensor becomes unreliable. I tested one in a garage in Phoenix during summer where the temperature hit 48 degrees Celsius. The sensor stopped triggering altogether until evening when the ambient temperature dropped below 35 degrees. That is a hard environmental limit of the technology. Another issue is weather exposure. The HC-SR501 is not sealed. Moisture, dust, and insects inside the housing will degrade performance over time. If you need an outdoor deployment, enclose the sensor in a weatherproof box with a breathable membrane or desiccant packet. I used silica gel packets inside a project box and mounted the sensor facing through a small gap cut into the side. It survived two monsoon seasons without issues.

How to Interface PIR Sensor with Arduino UNO – Pantech.AI
How to Interface PIR Sensor with Arduino UNO – Pantech.AI

For power efficiency, the HC-SR501 is reasonable but not optimal. It draws current continuously, not just on detection. If you are running on batteries and need weeks of operation, consider a module with a built-in low-power mode or switch to an ESP32 with deep sleep and external wake-on-interrupt. The current savings from putting the Arduino to sleep between detections can be significant, especially if the sensor triggers multiple times per hour. Uploading the sketch is standard. Open the Arduino IDE, paste the code, select your board and port, and click upload. The serial monitor will show motion events as they happen. If nothing appears, verify the wiring and check that the stabilization delay has completed. After one minute, move your hand in front of the sensor. You should see "Motion detected" followed by "No motion" a few seconds later. If the sensor triggers randomly with no movement nearby, check for heat sources, direct sunlight, or air currents in the detection path. Try covering the sensor with a small piece of heat-shrink tubing or a opaque cap to reduce light interference. The HC-SR501 has a Fresnel lens on top, and bright infrared light from sunlight can overload it.

The scope of what you can do with this sensor expands quickly from here. Add a buzzer, log events to an SD card, send notifications over Wi-Fi with an ESP8266, integrate it into a home automation system. The sensor itself is the easy part. The challenges come from understanding its behavior in real environments, not from the wiring or the code. Factor that in before you assume it will work the same way in your project as it does in a controlled bench test.