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
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | PIR motion sensor | OUT goes HIGH for a few seconds when motion is seen. Press "wave" in the controls to trigger it. |
| 1 | Buzzer | Pin 2 (+) to an output, pin 1 (−) to GND. tone(pin, hz) plays that pitch through your speakers. |
| 1 | 220 Ω resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | red LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno 5V | PIR motion sensor VCC |
| 2 | Arduino Uno pin 2 | PIR motion sensor OUT |
| 3 | Arduino Uno GND | PIR motion sensor GND |
| 4 | Arduino Uno pin 8 | Buzzer 2 |
| 5 | Arduino Uno GND | Buzzer 1 |
| 6 | Arduino Uno pin 13 | 220 Ω resistor leg 1 |
| 7 | 220 Ω resistor leg 2 | red LED anode (A, long leg) |
| 8 | Arduino Uno GND | red LED cathode (C, short leg) |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add a PIR motion sensor, a buzzer, a 220 Ω resistor and a red LED from the parts palette.
- Wire 1. Connect Arduino Uno 5V to PIR motion sensor VCC.
- Wire 2. Connect Arduino Uno pin 2 to PIR motion sensor OUT.
- Wire 3. Connect Arduino Uno GND to PIR motion sensor GND.
- Wire 4. Connect Arduino Uno pin 8 to Buzzer 2.
- Wire 5. Connect Arduino Uno GND to Buzzer 1.
- Wire 6. Connect Arduino Uno pin 13 to 220 Ω resistor leg 1.
- Wire 7. Connect 220 Ω resistor leg 2 to red LED anode (A, long leg).
- Wire 8. Connect Arduino Uno GND to red LED cathode (C, short leg).
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- 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
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator