ESP32-CAM Security Camera with Motion Detection

Build an affordable IP camera with motion detection, image capture, and remote monitoring capabilities.

Key Features

  • Live video streaming
  • Motion detection alerts
  • Image capture & storage
  • Night vision (with IR LEDs)
  • Face recognition (optional)

Components Required



  • ESP32-CAM Module
  • FTDI Programmer
  • MicroSD Card
  • Power supply (5V)
  • PIR sensor (optional)

Applications

  • Home security
  • Baby monitoring
  • Pet surveillance
  • Warehouse monitoring

Difficulty Level

Intermediate to Advanced

Quick Setup (2 Steps):

Step 1: Import to Wokwi

Diagram.json:
{
  "version": 1,
  "author": "Arduino Security Project",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-esp32-devkit-v1",
      "id": "esp",
      "top": 0,
      "left": 0,
      "attrs": {}
    },
    {
      "type": "wokwi-pir-motion-sensor",
      "id": "pir1",
      "top": -67.2,
      "left": 124.8,
      "attrs": {}
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -34.8,
      "left": 278.2,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": 14.4,
      "left": 278.2,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -25.2,
      "left": 230.4,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": 24,
      "left": 230.4,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-buzzer",
      "id": "bz1",
      "top": 96,
      "left": 240,
      "attrs": {}
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": 163.2,
      "left": 124.8,
      "attrs": { "color": "blue", "label": "RESET" }
    }
  ],
  "connections": [
    [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    [ "pir1:VCC", "esp:3V3", "red", [ "h0" ] ],
    [ "pir1:GND", "esp:GND.1", "black", [ "h0" ] ],
    [ "pir1:OUT", "esp:D13", "green", [ "h0" ] ],
    [ "led1:A", "r1:2", "red", [ "v0" ] ],
    [ "r1:1", "esp:D12", "red", [ "h0" ] ],
    [ "led1:C", "esp:GND.1", "black", [ "v0" ] ],
    [ "led2:A", "r2:2", "green", [ "v0" ] ],
    [ "r2:1", "esp:D14", "green", [ "h0" ] ],
    [ "led2:C", "esp:GND.1", "black", [ "v0" ] ],
    [ "bz1:1", "esp:D27", "orange", [ "h0" ] ],
    [ "bz1:2", "esp:GND.1", "black", [ "h0" ] ],
    [ "btn1:1.l", "esp:D15", "blue", [ "h0" ] ],
    [ "btn1:2.l", "esp:GND.1", "black", [ "h0" ] ]
  ],
  "dependencies": {}
}


  1. Go to https://wokwi.com
  2. Create new ESP32 project
  3. Replace diagram.json content with provided file
  4. Replace sketch.ino content with provided file
CODE:
/*
 * ESP32-CAM Security Camera with Motion Detection
 *
 * Features:
 * - PIR Motion Sensor Detection
 * - LED Status Indicators (Red=Motion Detected, Green=Standby)
 * - Buzzer Alert System
 * - Serial Monitoring
 * - Reset Button
 *
 * Note: This is a simplified version for Wokwi simulation
 * For real ESP32-CAM, you'll need to add camera initialization
 */

// Pin Definitions
#define PIR_PIN 13          // PIR Motion Sensor
#define RED_LED 12          // Motion Detected Indicator
#define GREEN_LED 14        // Standby/Ready Indicator
#define BUZZER_PIN 27       // Alert Buzzer
#define RESET_BTN 15        // Manual Reset Button

// System Variables
bool motionDetected = false;
bool systemArmed = true;
unsigned long lastMotionTime = 0;
unsigned long alertDuration = 5000;  // Alert duration in milliseconds
int motionCount = 0;

// Timing variables
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  Serial.println("\n\n=================================");
  Serial.println("ESP32-CAM Security System");
  Serial.println("Motion Detection Active");
  Serial.println("=================================\n");
 
  // Configure pins
  pinMode(PIR_PIN, INPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(RESET_BTN, INPUT_PULLUP);
 
  // Initial state - System Ready
  digitalWrite(GREEN_LED, HIGH);
  digitalWrite(RED_LED, LOW);
  digitalWrite(BUZZER_PIN, LOW);
 
  Serial.println("✓ System Initialized");
  Serial.println("✓ Sensors Ready");
  Serial.println("✓ Monitoring Started\n");
 
  // Warm-up delay for PIR sensor
  Serial.println("Warming up PIR sensor...");
  for(int i = 5; i > 0; i--) {
    Serial.print(i);
    Serial.print("... ");
    delay(1000);
  }
  Serial.println("\n✓ System Armed!\n");
}

void loop() {
  // Read PIR sensor
  int pirState = digitalRead(PIR_PIN);
 
  // Check for motion
  if (pirState == HIGH && systemArmed) {
    if (!motionDetected) {
      // Motion just detected
      motionDetected = true;
      lastMotionTime = millis();
      motionCount++;
     
      // Trigger alarm
      activateAlert();
     
      // Log event
      logMotionEvent();
    }
  }
 
  // Check if alert duration has passed
  if (motionDetected && (millis() - lastMotionTime > alertDuration)) {
    deactivateAlert();
    motionDetected = false;
  }
 
  // Continuous alert while motion detected
  if (motionDetected && pirState == HIGH) {
    lastMotionTime = millis(); // Reset timer
  }
 
  // Check reset button
  if (digitalRead(RESET_BTN) == LOW) {
    delay(50); // Debounce
    if (digitalRead(RESET_BTN) == LOW) {
      resetSystem();
      while(digitalRead(RESET_BTN) == LOW); // Wait for release
    }
  }
 
  delay(100); // Small delay for stability
}

void activateAlert() {
  // Turn on red LED
  digitalWrite(RED_LED, HIGH);
  digitalWrite(GREEN_LED, LOW);
 
  // Activate buzzer with pattern
  for(int i = 0; i < 3; i++) {
    digitalWrite(BUZZER_PIN, HIGH);
    delay(200);
    digitalWrite(BUZZER_PIN, LOW);
    delay(100);
  }
 
  Serial.println("\n╔════════════════════════════════╗");
  Serial.println("║    ⚠️  MOTION DETECTED! ⚠️     ║");
  Serial.println("╚════════════════════════════════╝");
}

void deactivateAlert() {
  // Turn off red LED, turn on green LED
  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, HIGH);
  digitalWrite(BUZZER_PIN, LOW);
 
  Serial.println("\n✓ Alert cleared - System back to standby\n");
}

void logMotionEvent() {
  Serial.println("\n--- Motion Event Log ---");
  Serial.print("Event #: ");
  Serial.println(motionCount);
  Serial.print("Timestamp: ");
  Serial.print(millis() / 1000);
  Serial.println(" seconds");
  Serial.print("Status: ");
  Serial.println("MOTION ACTIVE");
  Serial.println("Action: Alert triggered");
  Serial.println("------------------------\n");
}

void resetSystem() {
  Serial.println("\n>>> SYSTEM RESET <<<");
 
  // Reset all outputs
  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, LOW);
  digitalWrite(BUZZER_PIN, LOW);
 
  // Blink all LEDs
  for(int i = 0; i < 3; i++) {
    digitalWrite(RED_LED, HIGH);
    digitalWrite(GREEN_LED, HIGH);
    delay(200);
    digitalWrite(RED_LED, LOW);
    digitalWrite(GREEN_LED, LOW);
    delay(200);
  }
 
  // Reset variables
  motionDetected = false;
  motionCount = 0;
 
  // Return to ready state
  digitalWrite(GREEN_LED, HIGH);
 
  Serial.println("✓ System reset complete");
  Serial.println("✓ Motion counter cleared");
  Serial.println("✓ System re-armed\n");
}

// Additional helper function for status display
void printSystemStatus() {
  Serial.println("\n=== SYSTEM STATUS ===");
  Serial.print("Armed: ");
  Serial.println(systemArmed ? "YES" : "NO");
  Serial.print("Motion Detected: ");
  Serial.println(motionDetected ? "YES" : "NO");
  Serial.print("Total Detections: ");
  Serial.println(motionCount);
  Serial.print("Uptime: ");
  Serial.print(millis() / 1000);
  Serial.println(" seconds");
  Serial.println("====================\n");
}

Step 2: Run Simulation

  1. Click "Start Simulation" ▶️
  2. Click the PIR sensor to trigger motion
  3. Watch LEDs, buzzer, and Serial Monitor!

🔌 Circuit Features:

  • PIR Motion Sensor on GPIO 13
  • Red LED (motion alert) on GPIO 12
  • Green LED (standby) on GPIO 14
  • Buzzer on GPIO 27
  • Reset Button on GPIO 15

💡 What Happens:

  1. Green LED = System armed and monitoring
  2. Click PIR sensor = Triggers motion detection
  3. Red LED turns ON + Buzzer beeps 3x
  4. Serial Monitor logs the event
  5. After 5 seconds, returns to standby

Comments