Uno: PIR Motion Detector is a runnable microcontroller simulation example on SensBench — open the wired circuit, inspect the code, and compile real firmware in your browser, no install needed. Last updated Aug 14, 2026.

FREE SIMULATOR EXAMPLE · BEGINNER

Uno: PIR Motion Detector

This example watches an HC-SR501 PIR motion sensor on digital pin 4 of an Arduino Uno and lights the built-in LED on pin 13 the moment something moves. The Serial Monitor prints “>>> MOTION DETECTED! <<<" on the rising edge and "No motion." when things settle — a three-wire motion alarm in about twenty lines of code.

Board Arduino Uno (ATmega328P, 16 MHz, 5 V logic)
Difficulty beginner
Category sensors
Libraries None — just the Arduino core
Runs in Sensbench browser simulator — free, no account
Arduino Uno with an HC-SR501 PIR motion sensor wired to pin 4 and the onboard LED on pin 13 in the Sensbench simulator

The exact circuit you get when you open this example — already wired, ready to run.

The sketch

// Arduino Uno — PIR Passive Infrared Motion Sensor
// Wiring: OUT → pin 4  |  VCC → 5V  |  GND → GND

#define PIR_PIN 4
#define LED_PIN 13  // built-in LED

bool prevMotion = false;

void setup() {
  Serial.begin(9600);
  pinMode(PIR_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.println("PIR Motion Sensor ready — waiting for motion...");
  delay(2000); // sensor warm-up
}

void loop() {
  bool motion = (digitalRead(PIR_PIN) == HIGH);
  if (motion && !prevMotion) {
    Serial.println(">>> MOTION DETECTED! <<<");
    digitalWrite(LED_PIN, HIGH);
  } else if (!motion && prevMotion) {
    Serial.println("No motion.");
    digitalWrite(LED_PIN, LOW);
  }
  prevMotion = motion;
  delay(200);
}

How it works

The loop polls digitalRead(PIR_PIN) every 200 ms and compares it against prevMotion from the previous pass. The serial prints and the LED only fire on edges — when the reading actually changes — which is what keeps the Monitor readable. Without that state variable you would get five identical lines per second for as long as anyone keeps moving.

Pin 13 is the Uno’s onboard LED, so the alarm output needs zero external parts: pinMode(LED_PIN, OUTPUT) in setup(), then one digitalWrite() per edge. Notice the sensor pin uses plain INPUT, not INPUT_PULLUP — a PIR module drives its OUT line actively both high and low, unlike a pushbutton that floats open and needs the pull-up.

The delay(2000) in setup() is commented as sensor warm-up. The simulator’s PIR is ready instantly, so two seconds is pure courtesy here — on the real module, as the pitfall below explains, that number is off by an order of magnitude.

Before you build it for real

A genuine HC-SR501 forgives nothing. After power-on it needs a 30–60 second warm-up while its pyroelectric element settles — expect a few random triggers during that minute no matter what your sketch does, so the 2-second delay in this code is optimistic by about 30x. The module also carries two trim pots: one sets detection range (roughly 3–7 m), the other sets the re-trigger hold time (2 s minimum at the factory setting), so OUT stays HIGH well after motion stops and your “No motion” line arrives late. Electrically, OUT idles LOW and swings to about 3.3 V on trigger, which just clears the ATmega328P’s ~3.0 V HIGH threshold at 5 V logic — but power the module from 5 V on VCC, not 3V3, because its onboard regulator needs the headroom.

Get the parts

Related examples

FAQ

Why does the LED stay on after I stop moving?

That is the module’s re-trigger hold, not your code. A real HC-SR501 keeps OUT HIGH for a minimum time set by its delay potentiometer — a couple of seconds at the factory setting, up to minutes at the far end. Turn the delay pot fully counter-clockwise for the shortest response.

Can I use an interrupt instead of polling?

Yes: move OUT to pin 2 or 3 — the Uno’s two external interrupt pins — and call attachInterrupt(digitalPinToInterrupt(2), onMotion, RISING). For human-speed events a 200 ms poll is honestly fine; the interrupt earns its keep when you want the CPU sleeping between detections to save battery.