ESP32-CAM Motion Detection Security System

Sample Diagram 

1. Introduction

In security systems, capturing images is useful—but storing them is even more important. In this project, we enhance the ESP32-CAM motion detection system by saving captured images directly to a microSD card.

Whenever motion is detected, the ESP32-CAM captures an image and stores it permanently in the SD card with a unique filename.

This project is ideal for:

  • Home security systems
  • Surveillance applications
  • IoT-based monitoring 

2. Components

  • ESP32-CAM Module
  • PIR Motion Sensor (HC-SR501)
  • MicroSD Card (4GB–32GB, FAT32)
  • FTDI Programmer
  • Jumper Wires
  • Breadboard
  • 5V Power Supply (2A recommended)

3. Circuit and Connections

ESP32 CAM Pinout Diagram
ESP32 CAM Pinout Diagram

Pinout Diagram FTDI Programmer (USB to Serial)
Pinout Diagram FTDI Programmer (USB to Serial)
Pinout Diagram of PIR Motion Sensor
Pinout Diagram of PIR Motion Sensor


🔌 Connections:

💾 SD Card

  • Insert into ESP32-CAM slot (no wiring needed)

PIR Sensor → ESP32-CAM

  • VCC → 5V
  • GND → GND
  • OUT → GPIO 13

FTDI Programmer → ESP32-CAM (Uploading)

  • VCC → 5V
  • GND → GND
  • TX → U0R
  • RX → U0T
  • GPIO0 → GND (for programming mode)

4. Detailed Step-by-Step Circuit Working

  1. PIR sensor detects motion.
  2. Output goes HIGH when motion is present.
  3. ESP32-CAM reads this signal.
  4. Camera captures image using internal camera module.
  5. Image is stored in temporary memory (frame buffer).
  6. Image is then saved to SD card as a .jpg file.
  7. Each image is saved with a unique filename.

5. Code

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

#define PIR_PIN 13

void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);

if (!SD_MMC.begin()) {
Serial.println("SD Card Mount Failed");
return;
}

Serial.println("SD Card Initialized");

camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = 5;
config.pin_d1 = 18;
config.pin_d2 = 19;
config.pin_d3 = 21;
config.pin_d4 = 36;
config.pin_d5 = 39;
config.pin_d6 = 34;
config.pin_d7 = 35;
config.pin_xclk = 0;
config.pin_pclk = 22;
config.pin_vsync = 25;
config.pin_href = 23;
config.pin_sscb_sda = 26;
config.pin_sscb_scl = 27;
config.pin_pwdn = 32;
config.pin_reset = -1;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;

if (esp_camera_init(&config) != ESP_OK) {
Serial.println("Camera init failed");
return;
}
}

void loop() {
int motion = digitalRead(PIR_PIN);

if (motion == HIGH) {
Serial.println("Motion Detected!");

camera_fb_t * fb = esp_camera_fb_get();

if (!fb) {
Serial.println("Capture failed");
return;
}

String path = "/image_" + String(millis()) + ".jpg";

File file = SD_MMC.open(path.c_str(), FILE_WRITE);

if (!file) {
Serial.println("File open failed");
} else {
file.write(fb->buf, fb->len);
Serial.println("Image saved: " + path);
}

file.close();
esp_camera_fb_return(fb);

delay(5000); // Avoid multiple captures
}
}

6. Detailed Step-by-Step Code Working

  • SD_MMC.begin() initializes SD card
  • PIR sensor detects motion
  • esp_camera_fb_get() captures image
  • millis() generates unique filename
  • file.write() saves image to SD card
  • esp_camera_fb_return() clears memory

  • 7. Tips

  • Use Class 10 SD card for better speed
  • Always format SD card to FAT32
  • Use 5V 2A power supply (very important ⚠️)
  • Adjust PIR sensitivity using onboard potentiometer
  • Add delay to avoid multiple image captures

  • 8. Uses

    • Home surveillance system
    • Office security monitoring
    • Motion-triggered camera
    • Evidence recording system

    9. Conclusion

    This ESP32-CAM SD card-based motion detection system is a powerful and reliable solution for offline security monitoring. It allows continuous image logging without requiring internet connectivity, making it ideal for real-world applications.



    Note: Similar Projects

    https://simplebasicelectronics.blogspot.com/2026/03/arduino-pir-motion-sensor-alarm.html
    https://simplebasicelectronics.blogspot.com/2026/04/esp32-wi-fi-weather-station.html
    https://simplebasicelectronics.blogspot.com/2026/03/esp32-cam-face-recognition-system-arduino.html
    https://simplebasicelectronics.blogspot.com/2026/03/arduino-keypad-password-door-lock-system.html

    à´¤ുà´Ÿà´•്à´•à´•്à´•ാർക്à´•ാà´¯ി ഇലക്à´Ÿ്à´°ോà´£ിà´•്à´¸് ലളിതമാà´¯ി പഠിà´•്à´•ാം.

    Empowering students in Kerala with hands-on technical skills.