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)

  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

VCC

3.3V

GND

GND

OUT

GPIO 4

IN

GPIO 5

Anode

GPIO 2

Arduino Code

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

const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";

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;
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);
  
  delay(60000);
}

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;
  
  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.\nPlease check the property immediately.";
  message.text.content = textMsg.c_str();
  
  MailClient.sendMail(&session, &message);
}

void activateAlarm() {
  alertActive = true;
  digitalWrite(ledPin, HIGH);
  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();
    sendEmailAlert();
    activateAlarm();
    delay(5000);
  }
  
  delay(100);
}

Battery-Powered Version with p

#include <esp_sleep.h>

RTC_DATA_ATTR unsigned long lastAlertTime = 0;

void setup() {
  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();
      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();
      }
      for (int i = 0; i < 10; i++) {
        digitalWrite(ledPin, HIGH);
        delay(200);
        digitalWrite(ledPin, LOW);
        delay(200);
      }
    }
  }
  
  esp_deep_sleep_start();
}

Installation Steps

  1. Assemble circuit and test with USB power
  2. Configure Gmail App Password
  3. Update code with Wi-Fi credentials and email info
  4. Upload to ESP32 and test with hand motion
  5. Mount PIR sensor in shed corner at 2m height
  6. Enclose electronics in weatherproof box
  7. Enter shed to test email alert

Project Extensions

  • Add ESP32-CAM to capture and email images
  • Add GSM module for cellular alerts
  • Add door/window contact sensors
  • Create web dashboard to view alert history
  • Integrate with Home Assistant via MQTT

Conclusion

This shed security system provides peace of mind by alerting you immediately when someone enters, allowing you to 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 PIR Sensor PIR Sensor Relay (Siren) Status LED