Skip to Content

MOTION TRIGGERED-CAMERA

In today’s world, automation and smart security solutions are becoming increasingly essential for homes, offices, and industries. One effective way to enhance security is by using a motion-triggered camera system that automatically captures images or records videos whenever movement is detected. This project utilizes an ESP32-CAM module paired with a PIR (Passive Infrared) motion sensor to detect movement and trigger the camera, making it an ideal solution for surveillance applications. The ESP32-CAM is chosen over Arduino because of its built-in camera support, Wi-Fi connectivity, and ability to process images efficiently. By integrating the PIR sensor with the ESP32-CAM, the system can detect human presence and capture real-time footage, making it suitable for home security, wildlife monitoring, or even remote surveillance.


Components Required

  • ESP32-CAM (Microcontroller with camera module)
  • PIR Motion Sensor (Detects movement based on infrared radiation)
  • FTDI Programmer (For programming the ESP32-CAM)
  • MicroSD Card (Optional, for local image/video storage)
  • Jumper Wires
  • 5V Power Supply or Battery


Circuit Diagram


Component Connections

Connecting the PIR Sensor to ESP32-CAM

The PIR sensor detects motion by sensing changes in infrared radiation. When movement is detected, the sensor outputs a HIGH signal, which can be read by the ESP32-CAM.

Wiring the PIR Sensor to ESP32-CAM:

  • VCC (Power Input) → Connect to 5V on ESP32-CAM
  • GND (Ground) → Connect to GND on ESP32-CAM
  • OUT (Signal Output) → Connect to GPIO13 on ESP32-CAM

Why Use a PIR Sensor?

The PIR sensor efficiently detects motion with minimal power consumption, making it suitable for battery-powered security systems.

Connecting the ESP32-CAM

Unlike Arduino boards, the ESP32-CAM requires an FTDI programmer for flashing the code. The connection is as follows:

  • FTDI VCC → Connect to 5V on ESP32-CAM
  • FTDI GND → Connect to GND on ESP32-CAM
  • FTDI TX → Connect to U0R (RX) on ESP32-CAM
  • FTDI RX → Connect to U0T (TX) on ESP32-CAM
  • GPIO0 → Connect to GND (Only for programming mode)

Why Choose ESP32-CAM Over Arduino?

The ESP32-CAM is preferred because:

  • Built-in Camera Support: It has a dedicated OV2640 camera module, unlike Arduino, which would require an external camera shield.
  • Wi-Fi Connectivity: Allows remote access to captured images or videos, making it useful for real-time monitoring.
  • Higher Processing Power: Capable of handling image processing tasks efficiently.


Powering the System

The ESP32-CAM operates on 3.3V or 5V, but for stable performance, a 5V power supply is recommended. Avoid using the 3.3V output from an FTDI adapter, as it may not provide enough current.


Installing the Components

Mounting the PIR Sensor
  • Position the PIR sensor in a location with a clear field of view.
  • Secure it to the enclosure to avoid unwanted movements that may trigger false alerts.
Setting Up the Camera Module
  • Ensure the ESP32-CAM’s camera ribbon cable is properly inserted.
  • Position the camera to cover the desired surveillance area.


Uploading the Code to ESP32-CAM

  • Open Arduino IDE and install the ESP32 board package.
  • Select the ESP32 Wrover Module board.
  • Paste the provided Arduino code.
  • Connect the FTDI programmer to the ESP32-CAM.
  • Hold the GPIO0 pin LOW while uploading the code.
  • Once uploaded, restart the ESP32-CAM by disconnecting and reconnecting power.


Code

#include "esp_camera.h"

#include "Arduino.h"


#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


#define PIR_SENSOR_PIN  13 // PIR Sensor connected to GPIO13


void setup() {

  Serial.begin(115200);

  pinMode(PIR_SENSOR_PIN, INPUT);

  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;


  if (psramFound()) {

    config.frame_size = FRAMESIZE_UXGA;

    config.jpeg_quality = 10;

    config.fb_count = 2;

  } else {

    config.frame_size = FRAMESIZE_SVGA;

    config.jpeg_quality = 12;

    config.fb_count = 1;

  }


  esp_err_t err = esp_camera_init(&config);

  if (err != ESP_OK) {

    Serial.printf("Camera init failed with error 0x%x", err);

    return;

  }

}


void loop() {

  if (digitalRead(PIR_SENSOR_PIN) == HIGH) {

    Serial.println("Motion Detected! Capturing Image...");

    camera_fb_t *fb = esp_camera_fb_get();

    if (!fb) {

      Serial.println("Camera capture failed");

      return;

    }


    // Save or process image

    esp_camera_fb_return(fb);


    delay(5000); // Prevents continuous triggering

  }

}


Code Explanation

Including Libraries

#include "esp_camera.h"

#include "Arduino.h"

  • esp_camera.h: This library helps the ESP32-CAM to control the camera module.
  • Arduino.h: The core Arduino functions like pinMode() and digitalRead() are included here.
Defining Camera Pins

#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

These #define lines assign the GPIO pins of the ESP32-CAM board that are connected to the camera module.

What are GPIO Pins?

GPIO means General Purpose Input/Output pins. They are used to connect sensors, cameras, or LEDs to the board.

PIR Sensor Pin
#define PIR_SENSOR_PIN  13

The PIR sensor is connected to GPIO Pin 13.

Setup Function (Initial Setup)

void setup() {

  Serial.begin(115200);  // Start serial communication

  pinMode(PIR_SENSOR_PIN, INPUT); // PIR Sensor set as input

  • Serial.begin(115200): This allows the board to print messages to your computer to check what's happening.
  • pinMode(PIR_SENSOR_PIN, INPUT): The PIR sensor detects motion, so it is set as an input device.
Camera Configuration

camera_config_t config;

config.ledc_channel = LEDC_CHANNEL_0;

config.ledc_timer = LEDC_TIMER_0;

This part configures how the ESP32 communicates with the camera hardware.

Important Parameters

Parameter

Purpose

pixel_format

Defines the image format (JPEG)

frame_size

Image resolution (UXGA or SVGA)

jpeg_quality

Quality of the image (Lower number = Better quality)

fb_count

Number of frames to store

Checking External RAM (PSRAM)

if (psramFound()) {

  config.frame_size = FRAMESIZE_UXGA;  // High-quality image

  config.jpeg_quality = 10;           // Better quality

  config.fb_count = 2;               // Store 2 frames

} else {

  config.frame_size = FRAMESIZE_SVGA;  // Lower quality image

  config.jpeg_quality = 12;

  config.fb_count = 1;

}

ESP32 boards with PSRAM (Extra RAM) can capture higher quality images. Without PSRAM, the resolution is smaller.

Camera Initialization

esp_err_t err = esp_camera_init(&config);

if (err != ESP_OK) {

  Serial.printf("Camera init failed with error 0x%x", err);

  return;

}

This code starts the camera.

If the camera doesn't start, it will print an error message.

Loop Function (Main Program)

void loop() {

  if (digitalRead(PIR_SENSOR_PIN) == HIGH) {

    Serial.println("Motion Detected! Capturing Image...");

In this section:

  • The PIR sensor detects motion.
  • If motion is detected, the camera captures an image.
Capturing Image

camera_fb_t *fb = esp_camera_fb_get();

if (!fb) {

  Serial.println("Camera capture failed");

  return;

}

  • The camera takes the picture and saves it temporarily in memory (frame buffer).
  • If the camera fails, it will print "Camera capture failed".
Free the Memory
esp_camera_fb_return(fb);

This releases the camera memory to avoid crashes.

Delay to Prevent Continuous Triggering
delay(5000);

The system waits 5 seconds before detecting motion again.


Observing Functionality

  • Normal Mode: The ESP32-CAM remains idle.
  • Motion Detected: The camera captures an image.
  • Captured Image: Stored in RAM or an SD card, or sent via Wi-Fi.


Common Problems and Solutions

  • ESP32-CAM Not Responding?
    • Ensure GPIO0 is LOW while uploading code.
    • Use a stable 5V power source.
  • PIR Sensor False Triggers?
    • Reduce sensitivity using PIR adjustment knobs.


Conclusion

This motion-triggered camera system efficiently detects movement and captures images, making it an excellent security solution. The ESP32-CAM’s built-in camera, Wi-Fi capabilities, and ability to interface with sensors make it the ideal choice for real-time surveillance applications.

CONDUCTIVITY TESTER