Built-in project · Arduino Uno R3

Motion sensor on pin 12 lights the LED on 13, on an Arduino Uno

An HC-SR501 motion sensor on pin 12: when it sees someone move, its output goes HIGH and the LED on pin 13 lights for as long as it stays HIGH. 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 $29. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • How a PIR sensor detects movement
  • A sensor that holds its output for a while
  • Copying an input to an output
  • Printing only on a change of state
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

The HC-SR501 motion sensor sees the change in infrared light a moving body makes. Then its OUT pin goes HIGH and stays that way for a few seconds. OUT connects to pin 12.

The sketch copies the sensor straight onto pin 13, so the LED there follows it, and the board's own L lamp lights too. It prints motion or still when the state changes. Click the sensor's dome to walk past it.

Parts list

2 parts, plus the jumper wires. They retail for about $29; on Mokxi they're free.

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 pinConnects to
Motion sensor pin GNDArduino Uno R3 pin GND
Motion sensor pin OUTArduino Uno R3 pin 12
Motion sensor pin VCCArduino 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.

sketch.ino · Arduino Uno R3
// A motion sensor lights an LED: pin 12 in, pin 13 out.
//
//   HC-SR501 VCC -> 5V   OUT -> pin 12   GND -> GND
//
// The sensor watches for the change in infrared light a moving body makes,
// and holds its output HIGH for a few seconds after. The sketch copies that
// straight onto the LED on pin 13 (the board's own L lamp lights too).
//
// Click the sensor's dome to walk past it.

const int SENSOR = 12;
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 12");
}

void loop() {
  int now = digitalRead(SENSOR);
  digitalWrite(LED_PIN, now);
  if (now != last) {
    last = now;
    Serial.println(now ? "motion" : "still");
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Makes SENSOR (pin 12) 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 12");
}

loop sketch.ino

Reads the sensor into now and writes that value straight to the LED. If now differs from last, it saves it and prints motion or still. Nothing slows the loop down, so the LED follows the sensor at once.

void loop() {
  int now = digitalRead(SENSOR);
  digitalWrite(LED_PIN, now);
  if (now != last) {
    last = now;
    Serial.println(now ? "motion" : "still");
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED stays on after the motion stops. This project
    That is the sensor. It holds its output HIGH for a few seconds.
  • Nothing happens. This project
    Check OUT on pin 12, not 3, along with 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.