Motion sensor on pin 3 lights the LED on 13, on an Arduino Uno
An HC-SR501 PIR sensor on pin 3: a moving body makes its output HIGH, and the LED on pin 13 lights. The whole build, an Arduino Uno and 1 more part, 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.
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 sensor detects movement
- A sensor that holds its output for a while
- Reading a digital input
- Printing only on a change of state
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
The motion sensor sees the change in infrared light that a moving body makes. When it senses movement, its OUT pin goes HIGH and stays that way for a few seconds. OUT connects to pin 3.
The sketch reads pin 3 every 10 ms and copies the result to the LED on pin 13. It prints motion or still only when the state changes. Click the sensor's dome in the simulator to walk past it.
Parts list
3 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.
Kits that can build it
These 4 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 |
| Motion sensor pin OUT | Arduino Uno R3 pin 3 |
| Motion sensor pin VCC | Arduino Uno R3 pin 5V |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Motion sensor on pin 3 lights the LED on 13.
//
// sensor OUT -> pin 3 VCC -> 5V GND -> GND
//
// The sensor sees the change in infrared that a moving body makes, and holds its output HIGH for a few seconds.
//
// Click the sensor's dome to walk past it.
const int SENSOR = 3;
const int LED_PIN = 13;
int last = -1;
void setup() {
pinMode(SENSOR, INPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: motion sensor on pin 3, the LED on pin 13");
}
void loop() {
int now = digitalRead(SENSOR) == HIGH ? 1 : 0;
digitalWrite(LED_PIN, now ? HIGH : LOW);
if (now != last) {
last = now;
Serial.println(now ? "motion" : "still");
}
delay(10);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Makes SENSOR (pin 3) an input and LED_PIN (pin 13) an output. It starts the serial port at 9600 baud and prints a hello message.
void setup() {
pinMode(SENSOR, INPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: motion sensor on pin 3, the LED on pin 13");
}loop sketch.ino
Sets now to 1 when the sensor reads HIGH. It writes that to the LED. If now differs from last, it prints motion or still. A 10 ms delay ends each pass.
void loop() {
int now = digitalRead(SENSOR) == HIGH ? 1 : 0;
digitalWrite(LED_PIN, now ? HIGH : LOW);
if (now != last) {
last = now;
Serial.println(now ? "motion" : "still");
}
delay(10);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED stays on for a few seconds after the motion stops. This project
That is the sensor. It holds its output HIGH for a few seconds after it sees movement. - Nothing happens. This project
Check OUT on pin 3, VCC on 5V and GND on GND. - 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. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.