This tutorial combines digital input and two kinds of output into a real mini project: a motion-triggered alarm that lights an LED and sounds a buzzer when the PIR sensor detects movement — genuinely useful as-is, and a solid template for combining components in your own builds.
Intermediate
⌛ 40 min
🔧 What You'll Need
- Arduino Uno
- 1x PIR motion sensor (HC-SR501)
- 1x passive buzzer
- 1x LED
- 1x 220Ω resistor
- Breadboard
- 6x jumper wires
Wiring the Circuit
Connect the PIR sensor's VCC to 5V, GND to GND, and OUT to digital pin 2. Wire the buzzer's positive leg to pin 8 and negative to GND. Wire the LED through a 220Ω resistor from pin 9 to GND. Most PIR modules have two onboard potentiometers for sensitivity and delay time — leave them at their default (usually centered) position to start.
The Code
const int pirPin = 2;
const int buzzerPin = 8;
const int ledPin = 9;
void setup() {
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int motionDetected = digitalRead(pirPin);
if (motionDetected == HIGH) {
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000);
Serial.println("Motion detected!");
} else {
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
}
}
This sketch is really just the button-reading and buzzer tutorials combined: digitalRead(pirPin) checks the sensor exactly like reading a button, and when it returns HIGH (motion detected), the sketch turns on the LED and starts the buzzer tone, mirroring the earlier tutorials' patterns rather than introducing new concepts.
💡 Tip: PIR sensors need 30-60 seconds after power-up to calibrate to the room's background infrared level — if it seems to trigger randomly right after powering on, that's normal warm-up behavior, not a fault.
Troubleshooting
- Triggers constantly, even with no movement: The sensitivity potentiometer is likely turned too high — try turning it down, or allow the full warm-up period first.
- Never triggers at all: Confirm OUT is on pin 2 and check the sensor's jumper setting — most HC-SR501 modules have a jumper for "repeatable trigger" mode that should typically be set to H.
- Works but LED/buzzer lag behind actual motion: The onboard delay-time potentiometer controls how long the output stays HIGH after triggering — turn it down for a snappier response.
This project ties together every concept from the series — digital input, digital output, and combining components into something genuinely useful. From here, try swapping the buzzer for an SMS/notification module, or add the LCD from the previous tutorial to log how many times it's triggered.
Menu