What does this project do?
We build an alarm system that sounds a siren when someone walks into the room. The PIR sensor notices movement by detecting the heat given off by people and animals. You arm or disarm the alarm by pressing a button. If movement is detected while the alarm is armed, the red LED flashes and the buzzer plays a siren sound; the siren only stops when you press the button.
- Age group: 10 and up
- Level: Intermediate
- Time: About 45 minutes
- What you will learn: the PIR motion sensor, working with state variables, timing without
delay(), switching on and off with a button
Parts list
| Part | Qty |
|---|---|
| Arduino Uno (or compatible) | 1 |
| HC-SR501 PIR motion sensor | 1 |
| Passive buzzer | 1 |
| Red LED | 1 |
| 220 Ω resistor | 1 |
| Push button | 1 |
| Breadboard | 1 |
| Male-to-male and male-to-female jumper wires | ~10 |
| USB cable | 1 |
Wiring

| Component | Component pin | Arduino pin |
|---|---|---|
| PIR sensor | VCC | 5V |
| PIR sensor | OUT | D2 |
| PIR sensor | GND | GND |
| Button | One leg | D3 |
| Button | Diagonally opposite leg | GND |
| Red LED | Long leg (through 220 Ω) | D7 |
| Red LED | Short leg | GND |
| Buzzer | + (long leg) | D8 |
| Buzzer | – | GND |
Code
// Arduino Motion Sensor Alarm - ozandamci.com
const int pirPin = 2;
const int buttonPin = 3;
const int ledPin = 7;
const int buzzerPin = 8;
bool alarmArmed = false;
bool alarmRinging = false;
bool lastButton = HIGH;
void setup() {
pinMode(pirPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
Serial.println("PIR sensor warming up, wait 30 seconds...");
delay(30000);
Serial.println("Ready. Press the button to arm the alarm.");
}
bool buttonPressed() {
bool current = digitalRead(buttonPin);
bool pressed = (lastButton == HIGH && current == LOW);
lastButton = current;
if (pressed) delay(50); // ignore button bounce
return pressed;
}
void loop() {
// Button: arm / disarm the alarm
if (buttonPressed()) {
alarmArmed = !alarmArmed;
alarmRinging = false;
noTone(buzzerPin);
tone(buzzerPin, alarmArmed ? 2000 : 800, 150); // short confirmation beep
Serial.println(alarmArmed ? "Alarm ARMED" : "Alarm DISARMED");
}
// Was movement detected?
if (alarmArmed && !alarmRinging && digitalRead(pirPin) == HIGH) {
alarmRinging = true;
Serial.println("MOTION DETECTED!");
}
if (alarmRinging) {
// Siren: the tone and the LED change every 250 ms
bool phase = (millis() / 250) % 2;
tone(buzzerPin, phase ? 1000 : 1600);
digitalWrite(ledPin, phase);
} else {
digitalWrite(ledPin, alarmArmed); // LED stays on while armed
}
}
No Arduino? Try this circuit in your browser with the Wokwi simulation → (free, nothing to install). Click the PIR sensor to simulate motion; the code waits 30 seconds at start-up.
Step-by-step instructions
- Connect the PIR sensor. If you take off the sensor’s white dome, you can see the pin names. Connect VCC to 5V, OUT to D2 and GND to GND.
- Adjust the sensor. Of the two orange potentiometers on the HC-SR501, Sx sets the sensitivity (distance) and Tx sets how long the signal lasts. To start, turn both all the way to the left. If there is a jumper, put it in the H (repeat trigger) position.
- Connect the button, LED and buzzer. As shown in the table, connect the button to D3, the LED through a 220 Ω resistor to D7 and the buzzer to D8.
- Upload the code. Choose your board and port, then click Upload.
- Wait for it to warm up. After it is switched on, the PIR sensor calibrates itself for about 30 seconds; don’t move in front of it during this time.
- Arm the alarm and test it. Press the button, listen for a short high beep and check that the LED is on. Walk past the sensor: the siren should go off. Press the button again to silence it.
Troubleshooting
- The alarm goes off by itself: Keep the sensor away from sunlight, radiators and air conditioners; turn down the sensitivity with the Sx potentiometer.
- It never detects movement: Make sure you waited for the 30-second warm-up and that the OUT pin is connected to D2.
Try it yourself
- Make the alarm arm itself 10 seconds after you press the button instead of straight away, so you have time to leave the room.
- Use a keypad where you enter a 4-digit code instead of the button.
For teachers: a two-page activity sheet for using this project in class: lesson plan, discussion questions, answer key and a student worksheet.





“Arduino PIR Motion Sensor Alarm with Siren (Code + Wiring)” için 2 yorum