Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 18 of 47 · Home automation · Advanced

Home alarm system

Arm the system with a button. If the door opens or a PIR sees movement while armed, the siren sounds until you disarm it.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Home alarm system: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
Hall PIRPIR sensor
Door contactPush button
Arm / disarmPush button
Armed LEDLED
SirenBuzzer

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1RedHall PIR - VCCPin 2 (5V)
2ColouredHall PIR - OUTPin 7 (GPIO4)
3BlackHall PIR - GNDPin 6 (GND)
4ColouredDoor contact - SignalPin 15 (GPIO22)
5BlackDoor contact - GNDPin 9 (GND)
6ColouredArm / disarm - SignalPin 13 (GPIO27)
7BlackArm / disarm - GNDPin 14 (GND)
8ColouredArmed LED - anode +Pin 11 (GPIO17)
9BlackArmed LED - cathode -Pin 20 (GND)
10ColouredSiren - +Pin 12 (GPIO18)
11BlackSiren - -Pin 25 (GND)

Python code (gpiozero)

from gpiozero import MotionSensor, Button, LED, Buzzer
from time import sleep

pir = MotionSensor(4)
door = Button(22)          # pressed = door open (a reed switch on a real door)
arm_button = Button(27)
armed_led = LED(17)
siren = Buzzer(18)

armed = False
alarm = False

def toggle_arm():
    global armed, alarm
    armed = not armed
    alarm = False
    siren.off()
    armed_led.value = armed
    print("System ARMED" if armed else "System disarmed")

arm_button.when_pressed = toggle_arm

print("Press Arm / disarm. Then click the PIR or hold the door contact.")
while True:
    if armed and not alarm and (pir.motion_detected or door.is_pressed):
        alarm = True
        print("ALARM! Press Arm / disarm to stop")
        siren.beep(0.2, 0.2)
    sleep(0.1)

How the code works

You will learn

  • A state machine with armed and alarm flags
  • Global variables used by event functions
  • Combining sensors with or

Try this

  • Arm the system, hold the door contact, then disarm.
  • Add a 10 second exit delay so you can leave after arming.
  • Print the time of the alarm with time.strftime.

On a real Pi

  • Use normally-closed reed switches on doors: a cut wire then also raises the alarm.
  • A real alarm needs a battery backup and an outdoor siren with its own power. A Pi alone is a learning project, not a certified alarm.
  • PIR sensors need about a minute to settle after power-up.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
45 mm LED, red1Robu.in Amazon.in ThinkRobotics
5330 ohm resistor, 1/4 W
One per LED
1Robu.in Amazon.in ThinkRobotics
6Tactile push button (12 mm, with cap)2Robu.in Amazon.in ThinkRobotics
7Active buzzer, 5 V1Robu.in Amazon.in ThinkRobotics
8HC-SR501 PIR motion sensor1Robu.in Amazon.in ThinkRobotics
9Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
10Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 17. Smart door lockAll projects19. Production counter with batch alarm →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.