Arduino PIR Motion Sensor Alarm with Siren (Code + Wiring)

Arduino ile Hareket Sensörlü Alarm Sistemi – PIR Sensör projesi kapak görseli

🇹🇷 Bu yazıyı Türkçe okuyun

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

PartQty
Arduino Uno (or compatible)1
HC-SR501 PIR motion sensor1
Passive buzzer1
Red LED1
220 Ω resistor1
Push button1
Breadboard1
Male-to-male and male-to-female jumper wires~10
USB cable1

Wiring

Arduino motion sensor alarm with PIR sensor wiring diagram
ComponentComponent pinArduino pin
PIR sensorVCC5V
PIR sensorOUTD2
PIR sensorGNDGND
ButtonOne legD3
ButtonDiagonally opposite legGND
Red LEDLong leg (through 220 Ω)D7
Red LEDShort legGND
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

  1. 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.
  2. 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.
  3. 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.
  4. Upload the code. Choose your board and port, then click Upload.
  5. 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.
  6. 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.

Video

The narration is in Turkish. English subtitles are available: click CC on the video.

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

Bir Cevap Yazın