ElectroMartBDElectroMartBD

Build a Motion-Activated Security Alarm

A PIR sensor detects the body heat of people moving in front of it. Connect it to an Arduino and you have a simple alarm that turns on a light or buzzer when someone enters a room.

What You Need:

1. Wiring

  • PIR VCC → 5V, GND → GND, OUT → Pin 2
  • LED long leg → 220Ω resistor → Pin 13, short leg → GND
  • Relay IN → Pin 7 (optional)

2. The Code

const int pirPin = 2, ledPin = 13, relayPin = 7;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH); // relay off (active-low)
  Serial.begin(9600);
  delay(30000); // let the PIR warm up for 30 seconds
}

void loop() {
  if (digitalRead(pirPin) == HIGH) {
    Serial.println("Motion detected!");
    digitalWrite(ledPin, HIGH);
    digitalWrite(relayPin, LOW);
    delay(5000);
  } else {
    digitalWrite(ledPin, LOW);
    digitalWrite(relayPin, HIGH);
  }
}

3. Adjusting the Sensor

The HC-SR501 has two small knobs: one sets sensitivity (detection distance) and the other sets time delay (how long the output stays on). Turn them slowly with a small screwdriver while testing.

Safety: Only switch low-voltage DC loads with this project. Do not connect 220V mains unless you are trained to do so.

Need help? Stuck on wiring or code? Message us on WhatsApp at +880 1577-098376 and we will help you with your project.