DIY Power-Saving Surveillance Camera with ESP32-CAM and PIR Sensor

 I am going outside the town for a week or so and during that period I wanted to install a surveillance system build with ES32-CAM, PIR sensor and maybe SD-Card  to save the picture or video. I build simple video streaming system with ESP32-CAM which steams video on url in real time. But this system is continuously on and power is wasted. I think this is possible and a great way to create a low-power surveillance system! 

esp32-cam with PIR sensor

Here's how I plan implement it:

Concept Overview

  • The PIR sensor detects motion

  • When motion is detected, it wakes the ESP32-CAM from deep sleep

  • The camera captures images/video and saves to SD card

  • After recording, the system returns to deep sleep

Required Components

  • ESP32-CAM module

  • PIR motion sensor (HC-SR501)

  • MicroSD card (for storage)

  • Power source (battery or USB)

Basic Implementation Steps

  1. Hardware Connections:

    • Connect PIR output to a GPIO pin on ESP32-CAM (e.g., GPIO 13)

    • Ensure ESP32-CAM has SD card inserted

  2. Key Code Logic:


#include "esp_camera.h"
#include "FS.h"
#include "SD_MMC.h"

#define PIR_PIN 13

void setup() {
  pinMode(PIR_PIN, INPUT);
  
  // Initialize camera
  camera_config_t config;
  // ... camera configuration code ...
  esp_err_t err = esp_camera_init(&config);
  
  // Initialize SD card
  if(!SD_MMC.begin()){
    Serial.println("SD Card Mount Failed");
    return;
  }
}

void loop() {
  if(digitalRead(PIR_PIN) == HIGH) {
    // Motion detected - capture and save image
    camera_fb_t *fb = esp_camera_fb_get();
    if(fb) {
      // Save to SD card with timestamp
      String path = "/image" + String(millis()) + ".jpg";
      fs::FS &fs = SD_MMC;
      File file = fs.open(path.c_str(), FILE_WRITE);
      file.write(fb->buf, fb->len);
      file.close();
      esp_camera_fb_return(fb);
    }
    
    // Add delay to prevent multiple triggers
    delay(10000); // 10 seconds between detections
  }
  
  // Optional: Enter deep sleep between checks to save power
  // esp_deep_sleep(1000000); // 1 second sleep
}

Power Saving Enhancements

  1. Deep Sleep Mode:

    • Use the PIR sensor to wake the ESP32 from deep sleep

    • Configure the PIR output to connect to a wake-up capable GPIO

  2. Optimized Capture:

    • Only capture when motion is detected

    • Limit capture duration/frequency

    • Reduce image resolution/quality when possible

  3. Additional Improvements:

    • Add timestamp to filenames

    • Implement motion-triggered video recording

    • Add WiFi connectivity only when needed to upload files

Challenges to Consider

  • PIR sensors may have warm-up time (30-60 seconds after power on)

  • False triggers from pets or environmental changes

  • SD card writing can be power intensive

  • Battery life will still be limited for continuous operation

This approach will significantly reduce power consumption compared to continuous recording while still capturing all important events.


adfsasdfasdf

Post a Comment

Previous Post Next Post