Built-in project · Arduino Uno R3

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 Runs in your browser. Free, and no account needed.

PIR motion light · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// 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");
  }
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; Motion sensor pin GND; LED, red pin C
  • Arduino Uno R3 pin 13; Resistor, 220 Ω pin 1
  • Arduino Uno R3 pin 7; Motion sensor pin OUT
  • Arduino Uno R3 pin 5V; Motion sensor pin VCC
  • Resistor, 220 Ω pin 2; LED, red pin A

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.