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.

Parts in this circuit
| Part | Type |
|---|---|
| Hall PIR | PIR sensor |
| Door contact | Push button |
| Arm / disarm | Push button |
| Armed LED | LED |
| Siren | Buzzer |
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.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Red | Hall PIR - VCC | Pin 2 (5V) |
| 2 | Coloured | Hall PIR - OUT | Pin 7 (GPIO4) |
| 3 | Black | Hall PIR - GND | Pin 6 (GND) |
| 4 | Coloured | Door contact - Signal | Pin 15 (GPIO22) |
| 5 | Black | Door contact - GND | Pin 9 (GND) |
| 6 | Coloured | Arm / disarm - Signal | Pin 13 (GPIO27) |
| 7 | Black | Arm / disarm - GND | Pin 14 (GND) |
| 8 | Coloured | Armed LED - anode + | Pin 11 (GPIO17) |
| 9 | Black | Armed LED - cathode - | Pin 20 (GND) |
| 10 | Coloured | Siren - + | Pin 12 (GPIO18) |
| 11 | Black | Siren - - | 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
- The program has two states: armed and alarm. The button flips armed. Only an armed system can raise the alarm.
- The "global" line lets toggle_arm change the variables of the main program.
- "not alarm" makes sure the siren is started once, not ten times a second.
- siren.beep(0.2, 0.2) makes the buzzer pulse in the background while the loop keeps checking the button.
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.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 5 mm LED, red | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 330 ohm resistor, 1/4 W One per LED | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | Tactile push button (12 mm, with cap) | 2 | Robu.in Amazon.in ThinkRobotics |
| 7 | Active buzzer, 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | HC-SR501 PIR motion sensor | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
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.