PIR Sensor Arduino Tutorial: Detect Motion and Build a Smart Light
A PIR sensor detects the infrared radiation that moving warm bodies give off and sets one output pin HIGH, so an Arduino can read it with a single digitalRead() call and no library. This tutorial explains how the HC-SR501 module works, how to wire it to an Arduino UNO, how the H and L trigger modes differ, and how to set the sensitivity and delay potentiometers. It then builds three projects: a smart light, an intruder alarm with a buzzer and a motion event counter.
The module is often combined with other detection sensors, and the IR sensor Arduino tutorial, the HC-SR04 ultrasonic sensor tutorial and the GP2Y0A41SK0F distance sensor tutorial cover sensors that measure presence or distance instead of motion. The digitalWrite() and digitalRead() tutorial explains the functions used in every sketch below.
Table of contents
- PIR Sensor Arduino Tutorial: Detect Motion and Build a Smart Light
- PIR Sensor Arduino Working Principle
- PIR Sensor Arduino Pinout and Specifications
- PIR Sensor Arduino Wiring
- PIR Sensor Arduino H and L Trigger Modes
- PIR Sensor Arduino Sensitivity and Delay Adjustment
- PIR Sensor Arduino Motion Detection Code
- PIR Sensor Arduino Smart Light
- PIR Sensor Arduino Intruder Alarm
- PIR Sensor Arduino Motion Counter
- PIR Sensor Arduino Troubleshooting
- Conclusion
PIR Sensor Arduino Working Principle
A PIR (passive infrared) sensor does not send out any signal of its own. It measures the infrared radiation in its field of view and reports a change in that radiation, which is what happens when a warm body moves across the area.
PIR Sensor Detection Principle
The sensing element contains two infrared-sensitive halves that are wired against each other. A person or animal that stands still gives both halves the same amount of radiation and the difference stays near zero. When the warm body moves from the field of one half to the field of the other, the difference changes, and the circuit on the module amplifies this change and sets the output pin HIGH.

PIR Sensor Fresnel Lens and Detection Zones
The white dome on the module is a Fresnel lens. It divides the field of view into several zones and focuses the infrared radiation from each zone onto the sensing element. A body that moves from one zone to the next causes the change described above, so motion across the field of view is detected more reliably than motion straight towards the sensor.
PIR Sensor Arduino Pinout and Specifications
The HC-SR501 is the PIR module used in this tutorial. It has three pins, two potentiometers and a jumper for the trigger mode. The pin order can differ between manufacturers, so the labels printed on the board should always be checked before wiring.
PIR Sensor Pinout Table
| Pin | Description |
|---|---|
| VCC | Power supply, 5 V from the Arduino |
| OUT | Digital output, HIGH when motion is detected |
| GND | Ground |
PIR Sensor Specifications
| Specification | Typical value |
|---|---|
| Supply voltage | About 5 V to 20 V; some listings give 4.5 V as the lower limit |
| Output level | About 3.3 V when HIGH and 0 V when LOW |
| Standby current | Roughly 50 µA to 65 µA, according to the sources checked |
| Sensing range | About 3 m to 7 m, set by the sensitivity potentiometer |
| Detection angle | Less than 120° |
| Delay time | About 5 s to 300 s, set by the delay potentiometer |
| Block time | Between about 0.2 s and 3 s after the delay, depending on the module version |
| Warm-up time | About 30 s to 60 s after power is applied |
| Operating temperature | −15 °C to +70 °C |
The values are typical for HC-SR501 modules, and clones can differ, so the documentation of the module in use should be checked when exact timing matters.
PIR Sensor Output Level on the Arduino
The output of the module is about 3.3 V when HIGH. The ATmega328P on the Arduino UNO reads any voltage above 0.6 × VCC as HIGH, which is 3.0 V at a 5 V supply, so a 3.3 V output is read correctly and the module can be connected to the UNO without a level shifter. The margin is small, so the module should share the 5 V supply and the ground of the Arduino. The pin numbers of each board are listed in the Arduino UNO pinout guide.
PIR Sensor Arduino Wiring
The module needs three wires to the Arduino UNO. The same wiring is used for every sketch in this tutorial, and the projects add one output device each.
PIR Sensor Wiring Diagram
| PIR sensor pin | Arduino pin |
|---|---|
| VCC | 5V |
| GND | GND |
| OUT | D2 |
PIR Sensor Power and Placement
- Power the module from a stable 5 V supply and keep the wires short.
- Leave the module powered for 30 s to 60 s before testing, because the output can switch HIGH several times while the sensor settles.
- Keep the sensor away from direct sunlight, heaters, lamps and the airflow of fans or air conditioners, since sudden changes in heat can trigger it.
- Mount the sensor so that people move across its field of view, not straight towards it.
- Keep the module powered continuously, because switching its power on and off interrupts the settling process.
PIR Sensor Arduino H and L Trigger Modes
The jumper on the module selects how the output behaves when motion continues. The Arduino sketches stay the same in both modes, and only the output timing on the OUT pin changes.
PIR Sensor H Mode (Repeatable Trigger)
With the jumper in the H position, every new motion that is detected while the output is HIGH restarts the delay timer. The output therefore stays HIGH as long as people keep moving and falls LOW only after the delay time has passed without further motion. This mode suits lights and other loads that should stay on while someone is present.
PIR Sensor L Mode (Single Trigger)
With the jumper in the L position, the output goes HIGH for the set delay time and then returns to LOW, and any motion during that time is ignored. A person who keeps moving therefore produces one HIGH period per delay time. This mode suits counters and alarms that should react once per event.
Some modules have no jumper pins and are fixed to repeatable mode. Changing them to single trigger requires cutting a trace and soldering a link, so the documentation of the module should be read first.
PIR Sensor H and L Mode Timing
The diagram shows how the output reacts to three motion events in each mode.
PIR Sensor Block Time
After the output returns to LOW, the module ignores motion for a short block time. Depending on the module version this lasts between about 0.2 s and 3 s, so a second event that follows immediately after the output falls can be missed. This is normal behavior and does not indicate a fault.
PIR Sensor Arduino Sensitivity and Delay Adjustment
Two potentiometers on the module set the detection range and the hold time. They are adjusted with a small screwdriver while the module is powered.
PIR Sensor Sensitivity Potentiometer
The sensitivity potentiometer sets how far the sensor reacts to motion. On the HC-SR501 a clockwise turn increases the range up to about 7 m, and an anticlockwise turn shortens it to roughly 3 m to 4 m. A lower setting also reduces false triggers in small rooms, so it should be tested at the real mounting position.
PIR Sensor Delay Potentiometer
The delay potentiometer sets how long the output stays HIGH after a detection. A clockwise turn lengthens the time, and the usual range is about 5 s to 300 s. When the Arduino controls the load, the delay is best set to its minimum so that the sketch decides how long a light or an alarm stays on.
The turning direction can differ on clone boards, so the effect of each potentiometer should be confirmed with the Serial Monitor sketch in the next section.
PIR Sensor Arduino Motion Detection Code
The sketches in this section use the wiring from the wiring section, with OUT connected to D2. Each sketch waits 60 seconds at startup, because the output of the module is not reliable while the sensor settles.
PIR Sensor Basic Motion Test Code
The first sketch prints the state of the OUT pin twice per second. A hand moved in front of the sensor should change the message.
// PIR basic test: prints the state of the OUT pin of the sensor
const uint8_t PIR_PIN = 2; // OUT pin of the PIR module
const unsigned long WARMUP_MS = 60000UL; // Settling time of the sensor
void setup() {
pinMode(PIR_PIN, INPUT);
Serial.begin(9600);
Serial.println("PIR warming up for 60 seconds...");
delay(WARMUP_MS); // Ignore the output while the sensor settles
Serial.println("PIR ready");
}
void loop() {
if (digitalRead(PIR_PIN) == HIGH) {
Serial.println("Motion Detected");
} else {
Serial.println("No Motion");
}
delay(500); // Keeps the serial output readable
}The original post used two nearly identical sketches for the two modes, and the only real difference between them is the position of the jumper. The sketch below replaces both. It reports when the output rises and how long it stayed HIGH, so the effect of the jumper and of the delay potentiometer can be measured directly.
// PIR mode test: reports when the output goes HIGH and how long it stays HIGH
const uint8_t PIR_PIN = 2; // OUT pin of the PIR module
const unsigned long WARMUP_MS = 60000UL; // Settling time of the sensor
bool lastState = false; // Previous state of the OUT pin
unsigned long startTime = 0; // Time when the output went HIGH
void setup() {
pinMode(PIR_PIN, INPUT);
Serial.begin(9600);
Serial.println("PIR warming up for 60 seconds...");
delay(WARMUP_MS);
Serial.println("PIR ready");
}
void loop() {
bool state = (digitalRead(PIR_PIN) == HIGH);
if (state && !lastState) { // Rising edge: motion detected
startTime = millis();
Serial.println("Motion started");
}
if (!state && lastState) { // Falling edge: output returned to LOW
Serial.print("Output was HIGH for ");
Serial.print((millis() - startTime) / 1000.0, 1);
Serial.println(" s");
}
lastState = state;
}The expected results are listed below. Both modes use the same sketch, and only the jumper changes.
| Jumper | Behavior while a hand keeps moving in front of the sensor |
|---|---|
| H (repeatable) | One “Motion started” line, and the HIGH time ends only after the movement stops and the delay has passed |
| L (single trigger) | One “Motion started” line, and the HIGH time ends when the delay has passed, even if the movement continues |
The GIFs in the original post were recorded with the earlier sketches, which printed different messages, so they should be re-recorded or the captions should describe the behavior only.
PIR Sensor Arduino Smart Light
This project switches a light on when the sensor detects motion and switches it off again a few seconds after the last motion. The sketch holds the light on for a time that is set in software, which is why the delay potentiometer on the module can stay at its minimum.
PIR Sensor Smart Light Wiring
| Connection | Arduino pin |
|---|---|
| PIR VCC | 5V |
| PIR GND | GND |
| PIR OUT | D2 |
| LED anode through a 220 Ω resistor | D8 |
| LED cathode | GND |
An LED with a 220 Ω resistor draws about 10 mA to 15 mA, which is within the 20 mA that is recommended for one pin of the ATmega328P. A mains lamp must never be connected to a pin of the Arduino. For a lamp or any other larger load, D8 should drive a relay module or a transistor stage that is rated for the load.
PIR Sensor Smart Light Code
// PIR smart light: switches an output on with motion and off after a hold time
const uint8_t PIR_PIN = 2; // OUT pin of the PIR module
const uint8_t LIGHT_PIN = 8; // LED or relay input
const unsigned long WARMUP_MS = 60000UL; // Settling time of the sensor
const unsigned long LIGHT_HOLD_MS = 5000UL; // Light stays on this long after the last motion
unsigned long lastMotion = 0; // Time of the last HIGH reading
bool lightOn = false;
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(LIGHT_PIN, OUTPUT);
digitalWrite(LIGHT_PIN, LOW);
Serial.begin(9600);
Serial.println("PIR warming up for 60 seconds...");
delay(WARMUP_MS);
Serial.println("PIR ready");
}
void loop() {
if (digitalRead(PIR_PIN) == HIGH) {
lastMotion = millis(); // Restart the hold time
if (!lightOn) {
digitalWrite(LIGHT_PIN, HIGH);
lightOn = true;
Serial.println("Light ON - motion detected");
}
}
// Switch off after the hold time without motion
if (lightOn && (millis() - lastMotion > LIGHT_HOLD_MS)) {
digitalWrite(LIGHT_PIN, LOW);
lightOn = false;
Serial.println("Light OFF - no motion");
}
}The messages print only when the state changes, so the Serial Monitor does not fill with repeated lines. The hold time can be changed with LIGHT_HOLD_MS.
PIR Sensor Arduino Intruder Alarm
The alarm sounds a buzzer while the sensor reports motion. It suits a room or a cabinet that should stay empty, and the same logic can drive a siren through a relay.
PIR Sensor Alarm Wiring
| Connection | Arduino pin |
|---|---|
| PIR VCC | 5V |
| PIR GND | GND |
| PIR OUT | D2 |
| Active buzzer positive | D9 |
| Active buzzer negative | GND |
The sketch needs an active buzzer, which makes a sound when it receives a HIGH level. A small buzzer that draws less than about 20 mA can be connected to the pin directly. A larger buzzer needs a transistor, and a passive buzzer needs the tone() function, as shown in the passive buzzer tutorial.
PIR Sensor Alarm Code
// PIR intruder alarm: sounds an active buzzer while motion is detected
const uint8_t PIR_PIN = 2; // OUT pin of the PIR module
const uint8_t BUZZER_PIN = 9; // Active buzzer
const unsigned long WARMUP_MS = 60000UL; // Settling time of the sensor
bool alarmOn = false;
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
Serial.begin(9600);
Serial.println("PIR warming up for 60 seconds...");
delay(WARMUP_MS);
Serial.println("Alarm armed");
}
void loop() {
bool motion = (digitalRead(PIR_PIN) == HIGH);
if (motion && !alarmOn) { // Motion has just started
digitalWrite(BUZZER_PIN, HIGH);
alarmOn = true;
Serial.println("Intruder detected!");
} else if (!motion && alarmOn) { // Output returned to LOW
digitalWrite(BUZZER_PIN, LOW);
alarmOn = false;
Serial.println("Area safe");
}
}In H mode the buzzer stays on while the intruder keeps moving and for the delay time afterwards. A short delay on the potentiometer makes the alarm stop sooner.
PIR Sensor Arduino Motion Counter
This project counts motion events. The original post called it a people counter, but a PIR sensor cannot tell how many people pass, because several people who move together produce one HIGH period. The count is therefore the number of detection events, which is useful for logging activity in a room or a corridor.
PIR Sensor Motion Counter Wiring
The wiring is the same as in the basic wiring section, and the results appear on the Serial Monitor. An I2C LCD1602 display can show the count when no computer is connected.
PIR Sensor Motion Counter Code
// PIR motion counter: counts each time the output goes from LOW to HIGH
const uint8_t PIR_PIN = 2; // OUT pin of the PIR module
const unsigned long WARMUP_MS = 60000UL; // Settling time of the sensor
unsigned long count = 0; // Number of detected motion events
bool lastState = false; // Previous state of the OUT pin
void setup() {
pinMode(PIR_PIN, INPUT);
Serial.begin(9600);
Serial.println("PIR warming up for 60 seconds...");
delay(WARMUP_MS);
Serial.println("Motion counter ready");
}
void loop() {
bool state = (digitalRead(PIR_PIN) == HIGH);
if (state && !lastState) { // Count only on the rising edge
count++;
Serial.print("Count: ");
Serial.println(count);
}
lastState = state;
}The sketch counts on the rising edge, so continuous movement in H mode adds one to the count until the output falls. In L mode the count can increase once per delay time while a person keeps moving. The delay potentiometer therefore sets how often one long movement can be counted, and the minimum setting gives the finest resolution.
PIR Sensor Arduino Troubleshooting
Most problems with a PIR module come from the settling time, the power supply or the surroundings of the sensor. The table lists the common symptoms with their usual causes.
| Problem | Likely cause | Fix |
|---|---|---|
| The output switches HIGH after power-up with nobody present | The sensor is still settling | Wait 30 s to 60 s, and ignore the output during that time, as the sketches in this tutorial do |
| No motion is detected | Wrong pin or wiring, low sensitivity or a missing power supply | Check VCC, GND and OUT against the board labels, then turn the sensitivity potentiometer clockwise |
| The output stays HIGH for a long time | The delay potentiometer is set high, or H mode keeps restarting the delay | Turn the delay potentiometer anticlockwise and test again, or use L mode |
| Random triggers with nobody present | Unstable power, electrical noise or a nearby heat source | Use a stable 5 V supply, keep the wires short, place a capacitor of 10 µF to 100 µF between VCC and GND and move the sensor away from heaters, fans and windows |
| A second movement is missed right after the output falls | The block time of the module | No fix is needed, because the module ignores motion for 0.2 s to 3 s after each HIGH period |
| The range is shorter than expected | Sensitivity set low, or motion straight towards the sensor | Turn the sensitivity potentiometer clockwise and mount the sensor so that people cross its field of view |
| Detection is unreliable in a bright room | Sunlight or reflective surfaces near the lens | Move the sensor away from windows and reflective surfaces, and keep the lens clean |
In the original post the troubleshooting section recommended a 100 µF capacitor and the FAQ recommended 10 µF. The table gives the range from 10 µF to 100 µF, because the module datasheets that were checked do not specify a value.
PIR Sensor Mounting Tips
- Mount the sensor at a height of about 1.5 m to 2 m for a room, and aim it across the area where people walk.
- Keep the lens clean, because dust reduces the radiation that reaches the sensing element.
- Avoid placing the sensor near motors and relays, since they can disturb the supply voltage.
Conclusion
The PIR sensor is a simple way to add motion detection to an Arduino project, because it needs only three wires and no library. This tutorial covered the working principle, the wiring, the H and L trigger modes, the sensitivity and delay adjustment, and three projects that use the output: a smart light, an intruder alarm and a motion counter.
The next steps can be a display for the motion count, a relay for a lamp, or a second sensor for direction detection. The SSD1306 OLED tutorial shows how to build a display, and the IR sensor tutorial shows how to detect entry and exit with two sensors.
Download Project Here
Download the CubeMX project files and HAL source code — all tested on real hardware.
If you find this useful, you can support us with a Donation as well.
Browse More Arduino Sensor Tutorials

Arun is an embedded systems engineer with 10+ years of experience in STM32, ESP32, and AVR microcontrollers. He created ControllersTech to share practical tutorials on embedded software, HAL drivers, RTOS, and hardware design — grounded in real industrial automation experience.
Recommended Tools
Essential dev tools
Categories
Browse by platform











