0
0
Iot-protocolsHow-ToBeginner · 4 min read

Raspberry Pi Motion Detector Alarm Project Guide

Use a Raspberry Pi with a PIR motion sensor connected to its GPIO pins and write a Python script to detect motion and trigger an alarm sound or notification. The script reads sensor input, and when motion is detected, it activates a buzzer or plays a sound file as an alarm.
📐

Syntax

This project uses Python to interact with the Raspberry Pi's GPIO pins. The main parts are:

  • GPIO setup: Configure the pin connected to the PIR sensor as input.
  • Loop: Continuously check the sensor state.
  • Action: When motion is detected (sensor output is HIGH), trigger an alarm (e.g., buzzer or sound).
python
import RPi.GPIO as GPIO
import time

PIR_PIN = 7  # GPIO pin connected to PIR sensor output

GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIR_PIN, GPIO.IN)

try:
    while True:
        if GPIO.input(PIR_PIN):
            print("Motion Detected!")
        else:
            print("No Motion")
        time.sleep(1)
except KeyboardInterrupt:
    GPIO.cleanup()
Output
No Motion No Motion Motion Detected! Motion Detected! No Motion ...
💻

Example

This example shows a complete Python script that detects motion using a PIR sensor and sounds a buzzer connected to another GPIO pin when motion is detected.

python
import RPi.GPIO as GPIO
import time

PIR_PIN = 7      # PIR sensor output pin
BUZZER_PIN = 11  # Buzzer pin

GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIR_PIN, GPIO.IN)
GPIO.setup(BUZZER_PIN, GPIO.OUT)

try:
    print("Motion detector alarm started")
    while True:
        if GPIO.input(PIR_PIN):
            print("Motion Detected! Alarm ON")
            GPIO.output(BUZZER_PIN, GPIO.HIGH)  # Turn buzzer on
            time.sleep(2)  # Alarm duration
            GPIO.output(BUZZER_PIN, GPIO.LOW)   # Turn buzzer off
        else:
            print("No Motion")
        time.sleep(1)
except KeyboardInterrupt:
    print("Exiting program")
    GPIO.cleanup()
Output
Motion detector alarm started No Motion No Motion Motion Detected! Alarm ON No Motion ...
⚠️

Common Pitfalls

Common mistakes when building a motion detector alarm with Raspberry Pi include:

  • Not setting the GPIO mode correctly (use GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM) consistently).
  • Forgetting to clean up GPIO pins with GPIO.cleanup() on exit, which can cause warnings on next run.
  • Incorrect wiring of the PIR sensor or buzzer pins.
  • Not adding delays, causing the script to flood output or buzzer to stay on continuously.

Example of a wrong approach:

python
# Wrong: Missing GPIO.cleanup and no delay
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN)
while True:
    if GPIO.input(7):
        print("Motion!")
# Right way includes cleanup and delay
import time
try:
    while True:
        if GPIO.input(7):
            print("Motion!")
        time.sleep(1)
except KeyboardInterrupt:
    GPIO.cleanup()
📊

Quick Reference

Tips for your Raspberry Pi motion detector alarm:

  • Use GPIO.BOARD or GPIO.BCM mode consistently.
  • Connect PIR sensor output to a GPIO input pin.
  • Connect buzzer to a GPIO output pin with a suitable resistor.
  • Use try-except to handle program exit and clean GPIO.
  • Add time.sleep() to avoid busy loops.

Key Takeaways

Connect the PIR sensor to a GPIO input pin and read its state in Python.
Use a buzzer or sound output on a GPIO output pin to signal detected motion.
Always clean up GPIO pins with GPIO.cleanup() when the program ends.
Add delays in your loop to prevent excessive CPU use and repeated alarms.
Test wiring carefully to avoid hardware damage or incorrect readings.