Source: https://mokxi.com/learn/arduino-pir-motion-sensor
Updated: 2026-09-27

Learn

# A PIR motion sensor, on its own and then on an Arduino

Written by the Mokxi team, updated September 27, 2026

Try it now, no account Sign up free or see every lesson

- 192 parts on the bench
- 25 boards running now
- 1.00x real time, on every board

Click to open it in the editor

The Motion light example, with no board. Click the dome to start movement; the lamp holds for two seconds after it stops.

The PIR motion sensor is the white dome on porch lights and hallway switches, and the HC-SR501 module is the version in nearly every Arduino kit. It watches for something warm moving in front of its lens and answers with a plain digital signal: high while there is movement, low when there is not. That makes it one of the easiest sensors to use, and one of the most misunderstood, because of what it does after the movement stops.

The circuit above is a motion light with no board in it at all: the PIR module, one transistor and an LED. The module does the sensing and the timing itself, so the easiest way to learn it is to watch it alone first. Further down is the Arduino version, with the code to paste into the editor.

## What you need

- An HC-SR501 PIR motion sensor module
- A 2N2222 NPN transistor
- An LED and a 220 ohm resistor
- A 1 k resistor for the transistor’s base
- A 5 V supply, a half-size breadboard and jumper wires
- For the Arduino version: an Arduino Uno instead of the transistor

## Wiring

Part and pin

Goes to

Note

PIR VCC

5 V

The module has its own regulator, so 5 V in is fine

PIR GND

Ground

Shared ground

PIR OUT

1 k resistor to the 2N2222 base

OUT is 3.3 V high, whatever the supply

2N2222 emitter

Ground

2N2222 collector

LED cathode

The LED anode goes through 220 ohm to 5 V

Arduino version: PIR OUT

Uno pin 2

Read with digitalRead; no pull-up needed

## What a PIR actually senses

PIR stands for passive infrared. Everything warm gives off infrared light, and a person gives off more than a wall. The sensor behind the dome has two halves, and the dome is a lens that splits the room into zones. When something warm moves from one zone to the next, one half sees more than the other for a moment, and the module’s chip turns that difference into a pulse on OUT.

The important word is moves. A PIR is not a presence detector. Someone walking across the room triggers it; someone sitting perfectly still in front of it does not. So the control on the canvas is whether something is moving in front of it: click the dome to start movement and click again to stop.

## Hold time, block time and the jumper

Click the dome and the lamp comes on at once. Click again and count: it stays on for two more seconds. That is the hold time, set by the left trimmer on a real board, from about 3 seconds up to about 5 minutes. In Mokxi it is the hold property, 2 seconds by default.

Now click again right after the lamp goes out. Nothing happens for two and a half seconds. That is the block time: after each pulse the module ignores everything for a moment, and it is not adjustable on the real board. It is why a PIR that has just timed out looks broken, and why a sketch that reads that low as “they left” is often wrong.

The jumper beside the pins sets retrigger. In repeat, which is how most boards ship, continued movement keeps OUT high for as long as it lasts. In single, OUT goes low one hold time after the first movement no matter what. Set retrigger to single in the part’s properties to see the difference.

## Why there is a transistor

OUT is 3.3 volts and can supply only a milliamp or two. That is enough for a transistor’s base or an Arduino input and not enough for a lamp. Through 1 k, the base gets roughly two and a half milliamps, and the 2N2222 switches the LED’s much larger current from the 5 V rail. The same idea, a small signal switching a bigger load, is how you would drive a relay or a strip of lights.

## Reading it with an Arduino

On an Arduino, OUT goes straight to a digital pin and the sketch reads it with digitalRead. No pull-up is needed, because the module drives the line both high and low. Its 3.3 volt high is above the Uno’s input threshold at 5 volts, so it reads HIGH reliably.

This code is not part of the built-in project. To try it in Mokxi, open the motion light in the editor, add an Arduino Uno, wire the PIR’s OUT to pin 2 and share its ground, and paste the sketch into the Uno. The on-board LED on pin 13 follows the sensor, and the serial monitor says when the state changes.

Printing only on a change is the important habit. The loop runs thousands of times a second, and printing on every pass would bury the two lines that matter.

A plain Arduino sketch for a PIR on pin 2

## Where to buy the parts in the US

Adafruit sells a PIR motion sensor of the same kind, with the two trimmers and a jumper, that runs on 5 to 12 volts with a 3.3 volt output, and a smaller breadboard-friendly mini PIR. SparkFun sells a PIR sensor on a JST cable whose output is open collector, which means it needs a pull-up resistor (INPUT_PULLUP works) and reads LOW on motion, the opposite of the HC-SR501. DigiKey carries PIR sensors from several makers. Read the listing for the output type before you wire it.

## Common mistakes

Testing it the moment it powers on. A real HC-SR501 needs up to a minute to settle and may trigger on its own while it does. Mokxi does not model that warm-up, so the simulated one is ready at once.

Expecting it to see someone sitting still. It only reports movement.

Reading the block time as “nobody there”. Plan for a couple of seconds of deafness after each pulse.

Driving a lamp or a relay straight from OUT. Use a transistor or a relay module.

False triggers on the real thing from a heater, sunlight moving across the lens or a draft. The simulated sensor only fires when you click it, so it is quieter than any real one.

## Questions

How do I connect a PIR sensor to an Arduino?

VCC to 5 V, GND to GND and OUT to any digital pin, such as pin 2. Read it with digitalRead: HIGH means movement. No pull-up is needed for an HC-SR501.

Why does my PIR sensor stay on after I stop moving?

That is the hold time, set by a trimmer on the module, from a few seconds to about five minutes. With the jumper in the repeat position, continued movement keeps extending it.

Is the Mokxi motion light an Arduino project?

No. The built-in project has no board, to show that the module does the sensing and timing on its own. The Arduino sketch on this page is plain code you add yourself.

Related

## Keep going

An NPN Transistor as a Switch, Driving a Motor Pull-Up Resistors and the Floating Input They Fix Arduino Ultrasonic Sensor (HC-SR04) Tutorial Interrupts vs Polling on Arduino, Side by Side How the PIR is modeled A door alarm on an Uno, for the latching idea The project page, with the full sketch

Sources

## Where the facts on this page come from

- Adafruit: PIR motion sensor, 5 to 12 V supply, 3.3 V output (checked 2026-09-27)
- Adafruit: breadboard-friendly mini PIR motion sensor (checked 2026-09-27)
- SparkFun: PIR motion sensor (JST), open collector output (checked 2026-09-27)
- Adafruit Learning System: PIR motion sensor guide

## Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.

Start building Open the editor
