Arduino PIR Motion Sensor Alarm: Wiring, Code and Simulator

To build this Arduino PIR Motion Sensor Alarm project you need an Arduino Uno and a PIR motion sensor, a buzzer, a 220 Ω resistor and a red LED. A PIR sensor on pin 2 lights an LED and sounds a buzzer on pin 8 while it sees motion. Make the 8 connections in the wiring table, upload the Arduino C++ code, and press "wave a hand" in the controls: the alarm sounds for five seconds, then goes quiet.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1PIR motion sensorOUT goes HIGH for a few seconds when motion is seen. Press "wave" in the controls to trigger it.
1BuzzerPin 2 (+) to an output, pin 1 (−) to GND. tone(pin, hz) plays that pitch through your speakers.
1220 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1red LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.

Wiring

#FromTo
1Arduino Uno 5VPIR motion sensor VCC
2Arduino Uno pin 2PIR motion sensor OUT
3Arduino Uno GNDPIR motion sensor GND
4Arduino Uno pin 8Buzzer 2
5Arduino Uno GNDBuzzer 1
6Arduino Uno pin 13220 Ω resistor leg 1
7220 Ω resistor leg 2red LED anode (A, long leg)
8Arduino Uno GNDred LED cathode (C, short leg)

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a PIR motion sensor, a buzzer, a 220 Ω resistor and a red LED from the parts palette.
  3. Wire 1. Connect Arduino Uno 5V to PIR motion sensor VCC.
  4. Wire 2. Connect Arduino Uno pin 2 to PIR motion sensor OUT.
  5. Wire 3. Connect Arduino Uno GND to PIR motion sensor GND.
  6. Wire 4. Connect Arduino Uno pin 8 to Buzzer 2.
  7. Wire 5. Connect Arduino Uno GND to Buzzer 1.
  8. Wire 6. Connect Arduino Uno pin 13 to 220 Ω resistor leg 1.
  9. Wire 7. Connect 220 Ω resistor leg 2 to red LED anode (A, long leg).
  10. Wire 8. Connect Arduino Uno GND to red LED cathode (C, short leg).
  11. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  12. Run it. Press Run. Press "wave a hand" in the controls: the alarm sounds for five seconds, then goes quiet.

Arduino C++ code

// Motion alarm: a PIR sensor on pin 2 lights the LED and sounds the buzzer.
const int PIR = 2;
const int LED = 13;
const int BUZZER = 8;

void setup() {
  pinMode(PIR, INPUT);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Watching for motion...");
}

void loop() {
  static bool last = false;
  bool motion = digitalRead(PIR) == HIGH;
  digitalWrite(LED, motion);
  if (motion) tone(BUZZER, 880); else noTone(BUZZER);
  if (motion != last) {
    Serial.println(motion ? "Motion detected!" : "All quiet.");
    last = motion;
  }
  delay(20);
}

What to try

Press "wave a hand" in the controls: the alarm sounds for five seconds, then goes quiet.

Frequently asked questions

How do I wire the PIR Motion Sensor Alarm project on an Arduino Uno?

Make 8 connections: Arduino Uno 5V to PIR motion sensor VCC; Arduino Uno pin 2 to PIR motion sensor OUT; Arduino Uno GND to PIR motion sensor GND; Arduino Uno pin 8 to Buzzer 2; Arduino Uno GND to Buzzer 1; Arduino Uno pin 13 to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to red LED anode (A, long leg); Arduino Uno GND to red LED cathode (C, short leg).

What parts do I need for this Arduino project?

An Arduino Uno and a PIR motion sensor, a buzzer, a 220 Ω resistor and a red LED.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

A PIR sensor on pin 2 lights an LED and sounds a buzzer on pin 8 while it sees motion.

More Arduino Uno projects