PIR motion light on an Arduino Uno
An HC-SR501 motion sensor on pin 7 lights the LED on pin 13 while something warm moves in front of it. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $32. On Mokxi they're free.
What you'll learn
- How a PIR motion sensor works
- Reading a digital sensor output
- Copying an input onto an output
- Hold time and settling time on a real module
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
The HC-SR501 PIR sensor sees warm things move. Its lens splits the room into zones, and when warmth crosses from one zone to the next, the module drives its OUT pin high. OUT goes to pin 7. It stays high for a while after the movement stops, so the sketch only has to copy it to the LED.
The LED is on pin 13 with a 220 ohm resistor to ground. The sketch reads pin 7 and writes the same value to pin 13. It prints "motion detected" or "all still" when the state changes. A real module needs up to a minute to settle after power-up.
Parts list
5 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Motion sensorIn Halloween Prop Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Kits that can build it
These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Motion sensor pin GND | Arduino Uno R3 pin GND |
| LED, red pin C | Arduino Uno R3 pin GND |
| Resistor, 220 Ω pin 1 | Arduino Uno R3 pin 13 |
| Motion sensor pin OUT | Arduino Uno R3 pin 7 |
| Motion sensor pin VCC | Arduino Uno R3 pin 5V |
| LED, red pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Motion sensor: an HC-SR501 PIR lights an LED while something moves.
//
// PIR VCC -> 5V PIR OUT -> pin 7 PIR GND -> GND
// pin 13 -> the LED, through 220 ohm to GND
//
// A PIR sensor sees warm things move. Its lens splits the room into zones,
// and when warmth crosses from one zone into the next, the module drives OUT
// high. It holds OUT high for a while after the movement stops (the hold
// time, set by one of the two trimmers on the real module), so the sketch only
// has to copy OUT onto the LED.
//
// A real module also needs up to a minute to settle after power-up before it
// can be trusted.
const int PIR = 7;
const int LED = 13;
int last = LOW;
void setup() {
pinMode(PIR, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: PIR motion sensor");
}
void loop() {
int now = digitalRead(PIR);
digitalWrite(LED, now);
if (now != last) {
last = now;
Serial.println(now == HIGH ? "motion detected" : "all still");
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets PIR (pin 7) as an input and LED (pin 13) as an output, then starts serial at 9600 and prints a title.
void setup() {
pinMode(PIR, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: PIR motion sensor");
}loop sketch.ino
Reads the sensor into now and writes it straight to the LED. If now differs from last, it saves it and prints "motion detected" for HIGH or "all still" for LOW. The sensor holds its own output, so no timer is needed.
void loop() {
int now = digitalRead(PIR);
digitalWrite(LED, now);
if (now != last) {
last = now;
Serial.println(now == HIGH ? "motion detected" : "all still");
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED stays on for a while after you stop moving This project
That is the module's hold time. The sensor keeps OUT high, and the sketch just copies it. - Nothing happens This project
Check that OUT goes to pin 7, VCC to 5V and GND to ground. - The sensor reads high for ages after power up. Motion sensor
A real PIR needs 30 to 60 seconds to settle. Ignore its output during that time, and keep still in front of it until the delay passes. - Wrong output or it never triggers. Motion sensor
Power it from 5 V, join GND, and read OUT as a digital input (3.3 V logic high). Check the trimmers for delay and sensitivity. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.