Roborear
← Back to all articles
Robotics

How Does a PIR Sensor Work? The HC-SR501 Explained, from Physics to Arduino Code

No camera, no echo, and it doesn’t even see movement. Here is how a PIR sensor really works: infrared, the Fresnel lens, two pyroelectric elements, the HC-SR501 knobs and jumper, and three Arduino sketches.

Shahrear•November 26, 2026•12 min read
How Does a PIR Sensor Work? The HC-SR501 Explained, from Physics to Arduino Code

This little white dome can tell when you walk into a room. It has no camera. It doesn’t send out sound and wait for an echo like an ultrasonic sensor. And strangely, it doesn’t really detect movement at all.

So how does it know you’re there?

The short answer: a PIR sensor watches the infrared heat around it. Your body is warm, so it glows in infrared. When that heat pattern changes, because you moved, the sensor sets its output pin HIGH. If nothing changes, it stays LOW, even if you’re standing right in front of it.

HC-SR501 PIR sensor module with its white Fresnel lens dome, held over a cutting mat
The HC-SR501, the PIR module most Arduino and ESP32 kits include.

In this guide we go from the physics to working code: what’s inside the dome, why there are two sensing elements, what the two orange knobs and the little jumper do, and three Arduino sketches that I tested in the video.

Loads from YouTube when you press play, and YouTube may then set cookies. Watch on YouTube

What “passive infrared” means

PIR stands for passive infrared. The important word is passive.

An ultrasonic sensor like the HC-SR04 is active: it sends out a burst of sound and measures how long the echo takes to come back. A PIR sensor sends out nothing. It only receives.

PIR sensor (HC-SR501)Ultrasonic sensor (HC-SR04)
Sends a signal?No, it only listensYes, ultrasonic pulses
DetectsA change in infrared heatAny object that reflects sound
Gives youHIGH or LOWA distance
Sees a person standing still?NoYes
Sees a cold box?NoYes
Typical useLights, alarms, presenceObstacle avoidance, distance

If you want a distance, use an ultrasonic sensor, as in my ESP32 distance alarm. If you want to know that a warm body moved, use a PIR.

You are glowing right now

Everything warmer than absolute zero gives off infrared radiation. Walls, the desk, your cat, you. The warmer it is, the more it radiates.

Your skin is around 33–34 °C, warmer than a typical room. At that temperature the radiation is strongest at a wavelength of about 9–10 micrometres, deep in the infrared. Our eyes can’t see it, but a pyroelectric sensor can feel it.

Diagram: moving person, Fresnel lens, two pyroelectric elements, BISS0001 chip and the OUT pin
The whole journey, from your warm hand to a HIGH on the OUT pin.

Step 1: the dome is a lens, not the sensor

The white dome isn’t the sensor. It’s a Fresnel lens, and it pulls straight off.

Hands pulling the white Fresnel lens off an HC-SR501 PIR sensor module
The dome comes off with a gentle pull.

Look closely and you’ll see that the dome is made of many small facets. Each facet is a tiny lens that looks at one zone of the room and focuses that zone’s infrared onto the sensor. Together they split the room into many invisible zones.

Why does that matter? When you walk, your warm body moves from one zone to the next. Each time, the heat on the sensor jumps. Many zones mean many jumps, and that makes the sensor much more sensitive.

The dome looks milky because it’s made of a plastic (usually polyethylene) that lets long-wave infrared through. Ordinary window glass does the opposite: it blocks most of it. That’s why a PIR sensor usually can’t see you through a closed window.

Step 2: a crystal that reacts to change

Under the lens is the real sensor: the small metal can with a window on top.

HC-SR501 module without its lens, showing the round metal pyroelectric sensor in the middle
The pyroelectric sensor. The window on top lets infrared in.

Inside it is a pyroelectric material. When its temperature changes, the electric charge on its surface changes, and that creates a tiny voltage. When the temperature stays the same, the charge leaks away and the signal returns to zero.

This is the key to the whole sensor: it only responds to change. A constant temperature, warm or cold, gives no signal at all.

Step 3: why there are two sensing elements

If you could look inside the can, you would find not one but two sensing elements, side by side, wired so that their signals subtract.

Diagram: a person walking across gives an up and down signal, a person standing still gives none
Two elements that subtract: movement gives a clear up and down, a steady scene gives nothing.

When nothing moves, both elements see the same heat and cancel each other. When you walk across, your heat reaches element A first and then element B. For a moment they disagree, and the difference becomes a clear up-and-down pulse.

This design is clever. A slow change that affects both elements equally, like a room warming up in the afternoon, cancels out. A warm body crossing the zones doesn’t.

Why it forgets you when you stand still

Put your hand in front of the sensor and keep it perfectly still. The sensor will stop seeing it. The heat is still there, but it isn’t changing, so both elements agree again.

That’s why the lights in some offices switch off when you sit very still for too long. The sensor isn’t broken. It’s working exactly as designed.

Inside the HC-SR501 module

The pyroelectric signal is tiny, so the module needs a chip to turn it into something useful. On most HC-SR501 boards this is a BISS0001. It amplifies the signal, filters out noise, compares it with a threshold, and runs the timer that keeps the output HIGH.

Back of the HC-SR501 board with the BISS0001 chip, the potentiometers and the trigger jumper
The back of the board: the BISS0001 chip, two knobs and the trigger jumper.
PinWhat it doesConnect to
VCCPower for the module5 V on an Arduino (modules are usually rated for about 4.5–20 V)
OUTDigital output: about 3.3 V when motion is detected, 0 V when notAny digital pin, D2 in this guide
GNDGroundGND

OUT is only an output. You never send anything to the sensor. You only read it.

Because OUT gives about 3.3 V, the module also works directly with an ESP32 or a Raspberry Pi Pico. Power it from 5 V and connect OUT to any input pin. The PIR sensor in my wireless LoRa motion alarm works exactly like this.

The two orange knobs

Side of the HC-SR501 module with its two orange potentiometers and the yellow jumper
Sensitivity and time delay. A small screwdriver helps.

With the knobs facing you and the dome on top, as in the photo, my module has the time delay knob on the left and the sensitivity knob on the right. Turning either one clockwise increases it.

KnobClockwiseAnticlockwise
Sensitivity (right)Detects smaller and farther movement, up to several metresShorter range, fewer false triggers
Time delay (left)OUT stays HIGH longer, up to a few minutesOUT stays HIGH only a few seconds

Tip for testing: turn the time delay all the way anticlockwise first. Otherwise you’ll wave your hand, wait, wave again, and wonder why nothing changes. With a long delay, the output simply hasn’t finished its first HIGH.

The L and H jumper: what really happens

Next to the knobs there are three pins and a small jumper. The jumper connects the middle pin to either L or H. This decides what happens when you keep moving while the output is already HIGH.

Timing diagram: in L mode the output has a fixed length, in H mode every movement restarts the timer
Four hand waves. L mode gives fixed blocks; H mode stays HIGH as long as you keep moving.
  • L (single trigger): the output goes HIGH for the delay time and then LOW, no matter how much you move in between. If you’re still moving after that, it triggers again.

  • H (repeat trigger): every new movement restarts the timer. The output stays HIGH for as long as you keep moving, plus the delay time.

For an automatic light, you almost always want H. Nobody wants the light to blink off while they’re walking around.

After the output goes LOW, the module also ignores motion for a short moment, typically a couple of seconds. This lock-out prevents the output from flickering.

Wiring it to an Arduino

I used an Arduino Nano on an expansion board, but an Uno, a Mega or an ESP32 work the same way. You need:

  • An HC-SR501 PIR sensor

  • An Arduino Nano or Uno

  • Three jumper wires

  • For the LED projects: an LED, a 220–330 Ω resistor and a small breadboard

Wiring: HC-SR501 VCC to 5V, OUT to D2, GND to GND, and an LED with a resistor on D3
The wiring for all three sketches. The LED is only needed for the second and third.

New to breadboards or resistor colours? My guides to breadboard connections and resistor colour codes cover both.

Setting up the project in PlatformIO

I write Arduino code in VS Code with PlatformIO. Install the PlatformIO IDE extension, open PIO Home, create a new project, choose your board (I picked Arduino Nano ATmega328) and the Arduino framework. Your code goes in src/main.cpp.

PlatformIO project wizard in VS Code with Arduino Nano ATmega328 being selected
Choosing the board in the PlatformIO project wizard.

PlatformIO writes the settings into platformio.ini. For my Nano it looked like this:

[env:nanoatmega328]
platform = atmelavr
board = nanoatmega328
framework = arduino

Prefer the Arduino IDE? All three sketches work there too. Just leave out the #include <Arduino.h> line if you like; the Arduino IDE adds it for you. If you’re curious why I switched, read why I moved from the Arduino IDE to PlatformIO.

Sketch 1: is anybody there?

The simplest possible test: read the pin once a second and print the result.

#include <Arduino.h>
const int pirPin = 2;

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

void loop() {
  int motion = digitalRead(pirPin);
  if (motion == HIGH) {
    Serial.println("Motion detected!");
  } else {
    Serial.println("No motion.");
  }
  delay(1000); // Wait for a second before checking again
}

Upload it, open the serial monitor at 9600 baud, and wave your hand.

Isn’t one reading per second too slow? Not here. The module itself keeps OUT HIGH for at least a few seconds, so a reading every second won’t miss it.

Don’t panic in the first minute. Right after power-up, the sensor needs time to settle, and many datasheets say around a minute. During that time it may report motion that isn’t there. In the video, my first readings also needed some knob adjustment before they made sense.

Calibrating with the serial monitor

This sketch is the best tool for setting the knobs. I started with sensitivity and delay both turned low, then turned the sensitivity up step by step while moving my hand farther away, until the range was what I wanted. In H mode, the monitor printed “Motion detected!” for as long as I kept moving.

Sketch 2: a motion-activated LED

Now let’s make something you can see. Put the LED and resistor on the breadboard and connect them to D3.

Small breadboard with a green LED and a resistor connected by jumper wires
The LED and its resistor. The long leg of the LED goes toward D3.
#include <Arduino.h>
const int pirPin = 2;
const int ledPin = 3;

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

void loop() {
  int motion = digitalRead(pirPin);
  if (motion == HIGH) {
    Serial.println("Motion detected!");
    digitalWrite(ledPin, HIGH); // Turn on the LED
  } else {
    Serial.println("No motion.");
    digitalWrite(ledPin, LOW); // Turn off the LED
  }
  delay(1000); // Wait for a second before checking again
}
PIR sensor wired to an Arduino with the green LED lit after motion
Motion detected: the LED is on.

With the jumper in H and the time delay up a little, the LED stayed on the whole time I kept moving my hand. Add a longer delay and that’s a hallway night light, already finished.

Sketch 3: a breathing LED

Switching an LED on is fine. Making it breathe is more fun. This version fades the LED up and back down every time it sees motion.

#include <Arduino.h>
const int pirPin = 2;
const int ledPin = 3;

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

void loop() {
  int motion = digitalRead(pirPin);
  if (motion == HIGH) {
    Serial.println("Motion detected!");
    int i = 0;
    while (i < 255) {        // fade up
      analogWrite(ledPin, i);
      i++;
      delay(2);
    }
    while (i > 0) {          // fade down
      analogWrite(ledPin, i);
      i--;
      delay(2);
    }
  } else {
    Serial.println("No motion.");
    digitalWrite(ledPin, LOW); // Turn off the LED
  }
  delay(1000); // Wait for a second before checking again
}
VS Code with the PIR LED fade sketch and the serial monitor printing Motion detected and No motion
The fade sketch in VS Code, with the serial monitor below.

analogWrite() sets the brightness from 0 (off) to 255 (full). Each step waits 2 ms, so one fade up takes about half a second and one fade down another half second. While motion continues, the LED keeps breathing about once every two seconds.

Why D3? analogWrite() only works on pins that support PWM. On a Nano or Uno those are D3, D5, D6, D9, D10 and D11. On D2 or D4 the LED would just be on or off.

Where PIR sensors go wrong

A PIR sensor reacts to changing heat, not to people. That explains almost every strange result:

ProblemLikely causeWhat to try
Random triggers right after power-upThe sensor is still settlingWait about a minute before trusting it
Random triggers laterA heater, sunlight, an air vent or a lamp in view; sensitivity too highPoint it away from heat sources and turn sensitivity down
Output stays HIGH for agesLong time delay, or H mode with continuous movementTurn the delay knob anticlockwise
Doesn’t notice a person sitting stillNo change in heatNormal for PIR; use a different sensor if you need presence
Detects almost nothingLens removed, sensitivity at minimum or a window in the wayRefit the dome, turn sensitivity up, remove the glass
Never goes HIGHWrong pin or too little supply voltageCheck VCC, OUT and GND, and power it from 5 V

What you can build with it

  • Automatic lights for stairs, cupboards and hallways

  • Motion alarms, like my LoRa motion alarm that sends a warning to another room

  • Energy savers that switch off a fan or screen when nobody’s around

  • Wake-up triggers for battery projects: the sensor watches while the microcontroller sleeps

  • Robots that react when someone walks up to them

If you’re just starting with Arduino, my Arduino Robotics for Absolute Beginners course starts from your very first program.

The whole idea in three lines

  1. Warm bodies glow in infrared, and the Fresnel lens divides the room into zones.

  2. Two pyroelectric elements compare those zones and react only when the heat changes.

  3. The BISS0001 chip turns that change into a clean HIGH, and your Arduino simply reads a pin.

So next time a light switches on as you walk in, you’ll know: it never saw you. It only felt your heat move. Which sensor should I take apart next? Tell me in the comments.

Frequently Asked Questions

How does a PIR sensor work?

It measures infrared heat with two pyroelectric elements behind a Fresnel lens. When a warm body moves between the lens zones, the two elements see different heat, and the module turns that difference into a HIGH output.

Can a PIR sensor detect a person who isn’t moving?

Usually not. It reacts to changes in infrared heat, so a person who stays very still soon stops producing a signal.

What do the two knobs on the HC-SR501 do?

One sets the sensitivity and range, the other sets how long the output stays HIGH after motion. On my module, with the knobs facing you and the dome on top, the time delay is on the left and the sensitivity on the right.

What is the difference between L and H mode?

In L mode the output stays HIGH for a fixed time and then goes LOW, even if you keep moving. In H mode every new movement restarts the timer, so it stays HIGH while movement continues.

Can I use the HC-SR501 with an ESP32?

Yes. Power the module from 5 V and connect OUT to an ESP32 input pin. The output is about 3.3 V, which is safe for the ESP32.

Does a PIR sensor work through glass?

Usually not. Ordinary glass blocks most of the long-wave infrared that the sensor needs.

Comments (0)

No comments yet. Start the discussion.

← More articlesThanks for reading Roborear.