PIR Sensor for Shed Security with Email Alerts

Project Overview

This project creates a simple but effective security system for your shed, garage, or workshop. When motion is detected, the system sends an email alert to your phone or computer. It can also trigger a siren or flash lights as a deterrent.

Difficulty: Intermediate
Estimated time: 2 hours
Estimated cost: $25-35

How It Works

A PIR sensor monitors the shed interior. When motion is detected, the ESP32 sends an email via SMTP to your email address. An optional siren and strobe light can also be activated. The system includes a cooldown timer to prevent multiple emails for the same event.

Materials Needed

  • ESP32 (1)
  • HC-SR501 PIR sensor (1)
  • Buzzer or siren (optional)
  • LED or strobe light (optional)
  • Relay module (for siren/light)
  • Power supply (5V 2A) or battery with solar charger
  • Weatherproof enclosure
  • Jumper wires

Email Setup (Gmail)

For Gmail, you need an App Password (regular password won’t work):

  1. Enable 2-factor authentication on your Google account.
  2. Go to Security → App Passwords.
  3. Select “Mail” and “Other” (name it “ESP32 Security”).
  4. Copy the 16-character password.

Circuit Diagram

Connection Table

Arduino Code

// Shed Security with Email Alerts
#include <WiFi.h>
#include <ESP_Mail_Client.h>

// Wi-Fi credentials
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";

// Email credentials
const char* smtp_server = "smtp.gmail.com";
const int smtp_port = 587;
const char* sender_email = "your_email@gmail.com";
const char* sender_password = "your_app_password";
const char* recipient_email = "recipient@example.com";

const int pirPin = 4;
const int sirenPin = 5;
const int ledPin = 2;

unsigned long lastAlertTime = 0;
const unsigned long alertCooldown = 600000; // 10 minutes between emails
bool alertActive = false;

void setup() {
  Serial.begin(115200);
  
  pinMode(pirPin, INPUT);
  pinMode(sirenPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
  digitalWrite(sirenPin, LOW);
  digitalWrite(ledPin, LOW);
  
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  
  Serial.println("Shed Security Ready");
  delay(60000); // PIR warm-up
}

void sendEmailAlert() {
  if (WiFi.status() != WL_CONNECTED) return;
  
  ESP_Mail_Session session;
  session.server.host_name = smtp_server;
  session.server.port = smtp_port;
  session.login.email = sender_email;
  session.login.password = sender_password;
  session.login.user_domain = "";
  
  SMTP_Message message;
  message.sender.name = "Shed Security";
  message.sender.email = sender_email;
  message.subject = "SECURITY ALERT: Motion Detected in Shed";
  message.addRecipient("Recipient", recipient_email);
  
  String textMsg = "Motion was detected in your shed at " + String(millis()) + "ms.\n\n";
  textMsg += "Please check the property immediately.\n";
  textMsg += "This alert was generated by your ESP32 security system.";
  message.text.content = textMsg.c_str();
  message.text.charSet = "us-ascii";
  message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
  
  if (!MailClient.sendMail(&session, &message)) {
    Serial.println("Email send failed");
  } else {
    Serial.println("Email alert sent");
  }
}

void activateAlarm() {
  alertActive = true;
  digitalWrite(ledPin, HIGH);
  
  // Sound siren for 5 seconds
  digitalWrite(sirenPin, HIGH);
  delay(5000);
  digitalWrite(sirenPin, LOW);
  
  digitalWrite(ledPin, LOW);
  alertActive = false;
}

void loop() {
  bool motion = digitalRead(pirPin) == HIGH;
  
  if (motion && (millis() - lastAlertTime > alertCooldown)) {
    lastAlertTime = millis();
    Serial.println("Motion detected! Sending alert...");
    
    // Send email
    sendEmailAlert();
    
    // Activate local alarm
    activateAlarm();
    
    delay(5000);
  }
  
  delay(100);
}

Battery-Powered Version with Deep Sleep

#include <esp_sleep.h>

RTC_DATA_ATTR unsigned long lastAlertTime = 0;

void setup() {
  Serial.begin(115200);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  
  esp_sleep_enable_ext0_wakeup((gpio_num_t)pirPin, 1);
  
  if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT0) {
    if (millis() - lastAlertTime > alertCooldown) {
      lastAlertTime = millis();
      
      // Connect to WiFi and send email
      WiFi.begin(ssid, password);
      int attempts = 0;
      while (WiFi.status() != WL_CONNECTED && attempts < 20) {
        delay(500);
        attempts++;
      }
      if (WiFi.status() == WL_CONNECTED) {
        sendEmailAlert();
        WiFi.disconnect();
      }
      
      // Flash LED
      for (int i = 0; i < 10; i++) {
        digitalWrite(ledPin, HIGH);
        delay(200);
        digitalWrite(ledPin, LOW);
        delay(200);
      }
    }
  }
  
  esp_deep_sleep_start();
}

Solar Power for Remote Shed

For sheds without power, add solar charging:

  • 5V solar panel (5-10W)
  • TP4056 charging module
  • 18650 lithium battery (3000-5000mAh)
  • Low-dropout regulator (MCP1700)

Installation Steps

  1. Assemble circuit: Test with USB power first.
  2. Configure email: Set up Gmail App Password.
  3. Update code: Enter Wi-Fi credentials and email info.
  4. Upload to ESP32: Test with hand motion, verify email received.
  5. Mount PIR sensor: Place in shed corner at 2m height, covering entrance.
  6. Enclose electronics: Place ESP32 and relay in weatherproof box.
  7. Power up: Connect to shed power or solar/battery.
  8. Final test: Enter shed and check email alert.

Project Extensions

  • Camera capture: Add ESP32-CAM to capture and email images.
  • SMS alerts: Add GSM module for cellular alerts.
  • Multiple sensors: Add door/window contact sensors.
  • Dashboard: Create simple web page to view alert history.
  • Home Assistant: Integrate via MQTT for central monitoring.

Troubleshooting

  • No email received: Check Gmail App Password. Ensure SMTP settings correct.
  • Wi-Fi not connecting: Check credentials. ESP32 may need external antenna for shed location.
  • False triggers: Adjust PIR sensitivity. Ensure sensor not facing window.
  • Battery drains quickly: Use deep sleep mode. Reduce Wi-Fi connection time.

Conclusion

This shed security system provides peace of mind by alerting you immediately when someone enters. With email alerts, you can respond quickly even when away from home.

Leave a Reply

Your email address will not be published. Required fields are marked *

Component Pin ESP32 Pin
PIR Sensor VCC 3.3V PIR Sensor GND GND PIR Sensor OUT GPIO 4 Relay (Siren) IN GPIO 5 Status LED Anode GPIO 2