Thanks to visit codestin.com
Credit goes to lastminuteengineers.com

How HC-SR501 PIR Sensor Works & Interface It With Arduino

Have you ever wondered how automatic doors know when you’re approaching? Or how your hallway light turns on by itself when you walk by at night? These aren’t magic tricks—they’re using a clever device called a PIR sensor! A PIR (Passive Infrared) sensor lets you detect when a person or animal moves into or out of the sensor’s range.

One of the most popular PIR sensors among hobbyists is the HC-SR501, and for several good reasons:

  • It uses very little power (less than 2mA)
  • It’s affordable (usually costs less than $5)
  • It can detect motion up to 21 feet away
  • It’s really easy to connect to your projects

Before we dive into all the technical details, let’s understand the basics of how a PIR sensor actually works. This will help you understand why they’re so useful and why they behave the way they do in your projects.

How does a PIR sensor work?

Everything around us—including our bodies—gives off heat energy as infrared radiation. This happens as long as objects are warmer than absolute zero (0 Kelvin or -273.15°C). The warmer an object is, the more infrared radiation it releases. We can’t see this radiation with our eyes because it’s in the infrared part of the light spectrum, beyond what humans can detect visually.

A basic infrared receiver can detect the presence of infrared radiation, but to detect movement, we need something that can notice changes in infrared levels. This is exactly what PIR (Passive Infrared) sensors do! They’re specially designed to detect changes in infrared radiation caused when warm objects, like people, move within their detection range.

A PIR sensor has two main parts:

  1. Pyroelectric sensor – This is the heart of the device, appearing as a round metal piece with a rectangular crystal in the center.
  2. Fresnel lens – A special lens that collects and focuses infrared signals onto the pyroelectric sensor.
HC SR501 PIR Sensor with Lens

The Pyroelectric Sensor

The pyroelectric sensor has a window with two rectangular slots made from a material (usually silicon with a special coating) that allows infrared radiation to pass through. Behind this window are two separate infrared-detecting electrodes – one produces a positive output, while the other produces a negative output.

PIR Sensor Working Pyroelectric Sensor Two Detection Slots

When nothing is moving near the sensor, both parts detect the same amount of infrared radiation, so they cancel each other out and no signal is produced.

However, when a warm object like a person moves within the sensor’s detection area, one part detects a change in infrared radiation before the other. This creates a difference in signals between the two parts, which the sensor recognizes as movement.

PIR Sensor Working Animation Differential Output

The Fresnel Lens

The Fresnel lens might not look like it’s doing much, but it actually greatly increases the range and field of view of the PIR sensor.

A Fresnel lens is made of plastic with a series of concentric circular grooves carved into it. Each groove acts as a refracting surface that focuses parallel light rays onto a focal point, similar to how a regular optical lens works, but in a much flatter design.

Fresnel Lens Working

To increase the detection range and field of view, the lens is divided into multiple sections, with each section acting as its own individual Fresnel lens.

Fresnel Lens Actual Macro Photo

These different sections create various detection zones that overlap with one another. This is why the centers of the lenses appear to be pointing in different directions—each one directs infrared radiation from a different area to the PIR sensing element, allowing the sensor to cover a much wider area.

PIR Sensor Detection Zone Areas Pattern

This clever design is why PIR sensors can detect movement across a room even though the actual sensing element inside is quite small.

HC-SR501 PIR Sensor Hardware Overview

One of the best things about the HC-SR501 PIR sensor is that it’s ready to use right out of the box—no complicated setup required. All you need to do is connect it to power (anywhere between 5V and 12V) and ground. When the sensor detects movement, its output signal goes HIGH. When there’s no movement, the output stays LOW.

Because of this simple functionality, you can use the HC-SR501 sensor to trigger all kinds of cool actions in your projects! For example, you could turn lights on or off, start a fan, activate a spooky Halloween decoration, or even capture a picture of an unexpected visitor.

What makes this sensor even better is that it uses very little electricity (less than 2mA) and can detect motion up to 7 meters (about 21 feet) away.

BISS0001 PIR Controller

Inside the sensor is a special chip called the BISS0001 PIR Controller. It takes signals from the pyroelectric sensor (the part that actually detects infrared radiation from warm bodies) and processes them to create a digital output.

HCSR501 PIR Sensor BISS0001 IC

This chip is known for its excellent noise immunity, which makes it one of the most reliable PIR controllers you can get. If you want to learn more about it, you can look up the BISS0001 datasheet.

Power

The module has a built-in 3.3V precision voltage regulator, so you can power it with any DC voltage between 4.5 and 12 volts. Most people use 5V since that’s common in Arduino projects.

There’s also a protection diode (sometimes called a safety diode) that keeps your sensor safe if you accidentally connect the power backwards. This means that even if you make a mistake with the wiring, your sensor won’t get damaged.

HCSR501 PIR Sensor Diode and Regulator

Sensitivity Adjustment

On the back of the PIR sensor, you’ll find a small potentiometer that lets you adjust how sensitive the sensor is to movement.

HCSR501 PIR Sensor Sensitivity Adjustment

By turning this dial, you can control how far away the sensor can detect motion. If you turn the potentiometer clockwise, the sensor becomes more sensitive and can detect motion from farther away, up to 21 feet. If you turn it counterclockwise, it becomes less sensitive and will only detect motion that’s closer, down to about 9 feet.

This adjustment is super helpful when you want to fine-tune your project. For example, if you’re making a motion-activated light for a small hallway, you might want to decrease the sensitivity so it doesn’t trigger when someone walks by in a different room.

Time-Delay Adjustment

On the back of the PIR sensor, you’ll find another potentiometer that lets you adjust the time delay.

HCSR501 PIR Sensor Time Delay Adjustment

This time delay controls how long the sensor’s output stays HIGH (or “ON”) after it detects motion. You can set this delay to be as short as 1 second or as long as 3 minutes. If you turn the potentiometer clockwise, the delay gets longer. Turn it counterclockwise, and the delay gets shorter.

Trigger Selection Jumper

The PIR sensor has two different ways of responding when it detects movement:

Single Trigger Mode: In this mode, even if someone keeps moving around, the sensor will only trigger once.

Multiple Trigger Mode: In this mode, continuous movement will cause the sensor to trigger multiple times.

The sensor has a jumper (or sometimes a solder bridge) that lets you choose between these two modes:

HCSR501 PIR Sensor Trigger Select Jumper

L (Single Trigger Mode): When you select this mode, the output goes HIGH as soon as motion is detected and stays HIGH for the amount of time you set with the Time-Delay potentiometer. During this time, the sensor ignores any new motion. Only after the end of the delay (the output goes back to LOW) will the sensor start detecting motion again. If someone is still moving around after the delay ends, the sensor will trigger again. This means some motion might be completely ignored if it happens during the delay period. As you can see in the image below, Motion #3 is completely ignored in this mode.

HCSR501 PIR Sensor Single Trigger Mode

H (Multiple Trigger Mode): In this mode, the output also goes HIGH when motion is detected and stays HIGH for the time set by the Time-Delay potentiometer. But unlike single trigger mode, the sensor doesn’t ignore new motion. Instead, each time it detects new movement, it resets the timer. This means the output stays HIGH as long as there’s continued motion. Once all movement stops, the output will go back to LOW after the delay period has passed. This mode is great for keeping lights on in a room as long as someone is moving around.

HCSR501 PIR Sensor Multiple Trigger Mode

Optional Components – Thermistor and LDR

The HC-SR501 module has solder pads for two optional components, usually labeled as ‘RT’ and ‘RL’. On some boards, these labels might be hidden under the Fresnel lens on the other side.

HCSR501 PIR Sensor Place for Thermistor and LDR

RT: This pad is for connecting a thermistor, which is a special resistor that changes its resistance based on temperature. Adding a thermistor helps the HC-SR501 improve accuracy in extreme temperatures.

RL: This pad is for connecting a Light Dependent Resistor (LDR). An LDR changes its resistance based on how much light is hitting it. If you add an LDR, you can make the module work only when it’s dark, which is perfect for projects like motion-activated night lights or security systems that only run after sunset.

Technical Specifications

Here are the specifications:

Operating Voltage4.5 – 20V (typically 5V)
Maximum Current Draw< 2mA
Time Delay~ 1 sec to 3 min
Detection Distance3 – 7 meters (9 – 21 feet)
Detection Angle120 degrees (typically)

HC-SR501 PIR Sensor Pinout

The HC-SR501 PIR sensor has a simple 3-pin connector that you’ll need to hook up to your projects. Since the labels for these pins are often hidden underneath the Fresnel lens, it can be a bit tricky to know which pin is which without a diagram.

Passive Infrared PIR Sensor Pinout Diagram

VCC is the power supply pin for the sensor. You can connect this pin to a power source that provides anywhere from 5 volts to 12 volts. Most people use 5 volts because that’s what’s commonly available from an Arduino or similar microcontroller.

Output is the 3.3V TTL logic output pin. When the sensor detects movement, this pin goes HIGH (3.3 volts). When there’s no movement detected, the pin stays LOW (0 volts).

GND is the ground pin.

Using PIR Sensor as a standalone unit

One of the best things about the HC-SR501 PIR sensor is that it works really well on its own without needing a microcontroller. But you can make it do even more cool stuff by connecting it to an Arduino or similar device.

Let’s start with a simple experiment to show how useful this sensor can be all by itself.

The wiring is super easy! Simply Connect batteries to the sensor’s VCC and GND pins. Then, connect a small red LED to the output pin. Finally, add a 220Ω resistor between the LED and output pin to limit the current (this protects the LED from burning out)

That’s all there is to it! Now, when the PIR sensor detects someone moving, the output pin will go “HIGH” (or turn ON), and the LED will light up.

Testing PIR Sensor Wiring Fritzing Connections without Arduino

You could even connect the PIR sensor’s output directly to a relay module if you want to control bigger devices, like turning a lamp ON or OFF whenever someone walks by.

Important tip

After you turn on the circuit, you’ll need to wait about 30-60 seconds for the PIR sensor to get used to the normal infrared energy in the room. During this warm-up time, you might see the LED blink a few times. Once the LED stays off, try walking in front of the sensor or waving your hand to trigger it. You should see the LED light up when it detects your movement!

Wiring a PIR Sensor to an Arduino

Now that we understand how the PIR sensor works by itself, let’s hook it up to an Arduino to do even more cool stuff!

Connecting the PIR sensor to an Arduino is pretty straightforward. Simply connect the PIR sensor’s VCC pin to the Arduino’s 5V pin, the GND pin to the Arduino’s ground (GND), and the output pin to digital pin 8 on the Arduino.

For the HC-SR501 to work best with your Arduino, you should:

  • Set the jumper to the H position (Multiple Trigger Mode)
  • Adjust the Time-Delay to at least 3 seconds by turning the Time-Delay potentiometer all the way counterclockwise
  • Set the sensitivity potentiometer to whatever level you want (if you’re not sure, just put it in the middle)

Here’s a quick overview of the pin connections:

HC-SR501 PIR SensorArduino
VCC5V
GNDGND
OUT8

Refer to the image below for how to wire the HC-SR501 PIR sensor to your Arduino.

Arduino Wiring Fritzing Connections with PIR Sensor

Once everything is connected properly and you’ve adjusted the settings, you’re ready to upload your code and start using your PIR sensor with your Arduino!

Arduino Example Code

The code is very straightforward. It simply monitors whether the input on pin #8 is HIGH or LOW.

int ledPin = 13;                // choose the pin for the LED
int inputPin = 8;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
 
void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
}
 
void loop(){
  val = digitalRead(inputPin);  // read input value
  
  if (val == HIGH)        // check if the input is HIGH
  {            
    digitalWrite(ledPin, HIGH);  // turn LED ON
        
    if (pirState == LOW) 
        {
      Serial.println("Motion detected!");        // print on output change
      pirState = HIGH;
    }
  } 
  else 
  {
    digitalWrite(ledPin, LOW); // turn LED OFF
        
    if (pirState == HIGH)
        {
      Serial.println("Motion ended!");        // print on output change
      pirState = LOW;
    }
  }
}

With the sensor facing up, try swiping your hand over it. When motion is detected, you should see a “Motion Detected” message appear on the serial terminal.

passive infrared pir sensor arduino sketch output serial monitor

Things to consider before designing PIR based applications

When you’re designing projects with the HC-SR501 PIR sensor, there are two special delay periods you need to understand. These delays affect how the sensor works and might surprise you if you don’t know about them!

Lockout Time

After the sensor’s output goes from HIGH back to LOW (meaning it stops detecting motion), the sensor takes a short break for about 2 seconds during which it completely ignores any new movement. This is called the “lockout time.”

HCSR501 PIR Sensor Blocked Lockout Time

For example, let’s say you’ve set your sensor’s time-delay to 3 seconds, and the jumper is in the ‘L’ position (Single Trigger Mode). When you wave your hand in front of the sensor, the output goes HIGH for 3 seconds. Once those 3 seconds pass, the output returns to LOW. Then there’s a 2-second “lockout period”. If you move during this 2-second break, the sensor won’t see it at all (like with Motion #2 in the example)!

Power On Delay

When you first connect power to the HC-SR501 sensor, it needs about 30 to 60 seconds to get ready. During this time, it’s learning what the normal infrared energy in the room is.

The sensor might trigger a few false alarms during this time (the LED might blink). So, you should ignore any signals from the sensor during the first minute after powering it up. Also, try not to move around too much in front of the sensor. If there’s too much movement during this period, the sensor won’t learn the room properly and might not work correctly later.

Understanding these two special delay periods will help you design better projects with your PIR sensor and avoid confusion when it seems like it’s not detecting motion when it should be!