A comprehensive motion detection system for Raspberry Pi that captures images and sends email alerts when motion is detected.
- Motion Detection: Uses PIR sensor to detect movement
- Image Capture: Takes high-quality photos with Raspberry Pi camera
- LED Alert: Visual indicator when motion is detected
- Email Notifications: Sends captured images via email
- Error Handling: Robust error recovery and GPIO management
- Auto-Recovery: Handles GPIO busy errors automatically
smart_alert_system/
βββ smart_alert.py # Main application
βββ start.sh # Quick start script
βββ install_startup.sh # Install auto-start service
βββ uninstall_startup.sh # Remove auto-start service
βββ smart-alert.service # Systemd service file
βββ config/
β βββ .env # Email configuration (private)
βββ captured_images/ # Stored motion detection images
βββ tests/
β βββ test_pir_sensor.py # PIR sensor testing
β βββ test_led.py # LED testing
β βββ test_email.py # Email configuration testing
β βββ release_gpio.py # GPIO cleanup utility
βββ docs/
β βββ README.md # Main documentation
β βββ AUTOSTART.md # Auto-start setup guide
β βββ EMAIL_SETUP.md # Email configuration guide
βββ README.md # Quick reference
- Raspberry Pi (any model with GPIO)
- PIR Motion Sensor (HC-SR501 or similar)
- LED with 220Ξ© resistor
- Raspberry Pi Camera Module (v1, v2, or HQ)
- Jumper wires
- Breadboard (optional)
- VCC β Pin 2 or 4 (5V)
- OUT β Pin 11 (GPIO 17)
- GND β Pin 6 (GND)
- Long leg (+) β 220Ξ© Resistor β Pin 12 (GPIO 18)
- Short leg (-) β Pin 14 (GND)
- Connect to Camera Serial Interface (CSI) port
# System packages
sudo apt-get update
sudo apt-get install python3-pip python3-rpi.gpio
# Python packages
pip3 install picamera2 python-dotenv
# Enable camera
sudo raspi-config
# Navigate to: Interface Options β Camera β Enablecd ~/smart_alert_systemEdit the configuration file:
nano config/.envAdd your email credentials:
SENDER_EMAIL=youremail@gmail.com
EMAIL_PASSWORD=your_16_char_app_password
RECIPIENT_EMAIL=recipient@gmail.com
EMAIL_SUBJECT=π¨ Motion Detected AlertImportant: Use Google App Password, not your regular Gmail password!
- Generate at: https://myaccount.google.com/apppasswords
- Enable 2-Step Verification first
Test PIR Sensor:
python3 tests/test_pir_sensor.pyTest LED:
python3 tests/test_led.pyTest Email:
python3 tests/test_email.pyInstall as a system service to run automatically at startup:
cd ~/smart_alert_system
./install_startup.shBenefits:
- β Starts automatically when Raspberry Pi boots
- β Auto-restarts if it crashes
- β Runs in background as a service
- β Easy log management with systemd
Manage the service:
sudo systemctl status smart-alert # Check status
sudo systemctl stop smart-alert # Stop service
sudo systemctl start smart-alert # Start service
sudo systemctl restart smart-alert # Restart service
sudo journalctl -u smart-alert -f # View live logsRemove from auto-start:
./uninstall_startup.shSee AUTOSTART.md for detailed instructions.
Start the system manually:
cd ~/smart_alert_system
python3 smart_alert.pyOr use the helper script:
./start.shnohup python3 smart_alert.py > smart_alert.log 2>&1 &pkill -f smart_alert.pytail -f smart_alert.log- Go to Google Account: https://myaccount.google.com/
- Navigate to Security β 2-Step Verification (enable if needed)
- Scroll to App passwords: https://myaccount.google.com/apppasswords
- Select: Mail and Other (Custom name)
- Enter: "Smart Alert System"
- Click Generate
- Copy the 16-character password
- Update
config/.envfile
- Subject line with motion alert
- Timestamp of detection
- Attached JPEG image
- System location identifier
The system automatically handles GPIO busy errors. If issues persist:
# Kill any existing instances
pkill -f smart_alert.py
# Check GPIO status
gpioinfo gpiochip0 | grep -E "line (17|18):"# Enable camera
sudo raspi-config
# Interface Options β Camera β Enable β Reboot
# Test camera
libcamera-still -o test.jpg- Verify app password (not regular password)
- Check 2-Step Verification is enabled
- Ensure correct email addresses
- Check spam folder
- Test with:
python3 tests/test_email.py
- Check PIR sensor connections
- Verify GPIO 17 is used
- Test sensor with:
python3 tests/test_pir_sensor.py - Adjust PIR sensitivity (potentiometer on sensor)
- Verify LED polarity (long leg = positive)
- Check resistor is connected
- Test with:
python3 tests/test_led.py
Edit smart_alert.py to customize:
PIR_PIN = 17 # PIR sensor GPIO pin
LED_PIN = 18 # LED GPIO pin
LED_ON_TIME = 10 # LED duration (seconds)
COOLDOWN_TIME = 2 # Cooldown between detectionsImage settings (in setup_camera()):
main={"size": (1920, 1080)} # Image resolution- Keep
config/.envfile private (contains passwords) - Never commit
.envto version control - Use dedicated Gmail account for IoT projects
- Revoke app passwords when no longer needed
- Regularly review sent emails
- PIR sensor triggers
- LED turns ON
- Camera captures image
- Image saved to
captured_images/ - Email sent with attachment
- LED stays on for 10 seconds
- LED turns OFF
- System ready for next detection
Images are saved as: motion_YYYYMMDD_HHMMSS.jpg
Example: motion_20251102_143022.jpg
Create service file:
sudo nano /etc/systemd/system/smart-alert.serviceAdd:
[Unit]
Description=Smart Alert Motion Detection System
After=network.target
[Service]
Type=simple
User=pi
WorkingDirectory=/home/jkarm/smart_alert_system
ExecStart=/usr/bin/python3 /home/jkarm/smart_alert_system/smart_alert.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.targetEnable and start:
sudo systemctl daemon-reload
sudo systemctl enable smart-alert.service
sudo systemctl start smart-alert.service
sudo systemctl status smart-alert.servicesudo journalctl -u smart-alert.service -fPotential improvements:
- Cloud storage integration (Google Drive, Dropbox)
- Web interface for viewing images
- Mobile app notifications (Pushbullet, Telegram)
- Multiple PIR sensor support
- Video recording capability
- Motion analytics and statistics
- Integration with home automation systems
Feel free to submit issues, fork the repository, and create pull requests for any improvements.
This project is open source and available for personal and educational use.
Smart Alert System Version 1.0 Created: November 2025
For issues or questions:
- Check troubleshooting section
- Test individual components using test scripts
- Review system logs
- Check GPIO and camera status
Happy Monitoring! π₯ποΈ