Motion-Activated CCTV Camera using ESP32 with SD Card Storage
Monitoring areas in real-time while saving footage is a core concept of smart security systems.
This project uses ESP32-CAM to capture video, detect motion, and store images or short clips on an SD card for later review.
Hardware Requirements
- ESP32-CAM Module (with OV2640 camera)
- PIR Motion Sensor
- Micro SD Card (formatted FAT32)
- Jumper wires
- 5V power supply for ESP32-CAM
- Optional: LED for motion alert
Working Principle
Motion Detection:
- PIR sensor detects movement in the field of view.
- Sends HIGH signal to ESP32 when motion is detected.
Camera Activation:
- ESP32-CAM captures a photo (or video snippet) when motion is detected.
Data Storage:
- Captured files are saved on SD card with timestamped filenames.
Optional Streaming:
- ESP32 can also serve real-time video via Wi-Fi to a web browser.
Arduino Code Example
This example captures JPEG images on motion detection:
#include "esp_camera.h"
#include "FS.h"
#include "SD_MMC.h"
#define PIR_PIN 13
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
// Camera config
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
config.frame_size = FRAMESIZE_VGA;
config.jpeg_quality = 10;
config.fb_count = 1;
// Initialize camera
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed: %d\n", err);
return;
}
// Initialize SD card
if(!SD_MMC.begin()){
Serial.println("SD Card Mount Failed");
return;
}
Serial.println("Setup complete");
}
void loop() {
if(digitalRead(PIR_PIN) == HIGH){
Serial.println("Motion Detected!");
camera_fb_t * fb = esp_camera_fb_get();
if(!fb){
Serial.println("Camera capture failed");
return;
}
String path = "/motion_" + String(millis()) + ".jpg";
File file = SD_MMC.open(path.c_str(), FILE_WRITE);
if(!file){
Serial.println("Failed to open file for writing");
} else {
file.write(fb->buf, fb->len);
Serial.println("Saved image: " + path);
file.close();
}
esp_camera_fb_return(fb);
delay(2000); // avoid multiple triggers
}
}How to Use
Connect PIR sensor output to GPIO13 on ESP32-CAM.
Insert SD card into ESP32-CAM slot.
Power the ESP32-CAM with 5V.
Upload the code using ESP32-CAM has no onboard USB or CH340 (since FTDI programmer ).
Place in location where motion detection is required.
When motion occurs, images are stored on the SD card with unique timestamp names.