Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Based Security System with PIR Sensor and Solenoid Lock Control

View and Edit Project In Cirkit DesignerImage of ESP32-Based Security System with PIR Sensor and Solenoid Lock Control

Circuit Documentation

Summary

The circuit is designed to create a security system that integrates a PIR sensor for motion detection, a solenoid lock for access control, a buzzer for audible alerts, and an ESP32-CAM module for capturing images upon motion detection. The system is controlled by an ESP32 microcontroller, which also handles communication with a cloud service for image storage. The circuit includes a relay to control the solenoid lock and a flush switch for manual override or additional control inputs.

Component List

ESP32-CAM

PIR Sensor

Door MC-38

12V Solenoid Lock

Buzzer

ESP32 38 PINS

5V Relay

Flush Switch

Wiring Details

ESP32-CAM

PIR Sensor

Door MC-38

12V Solenoid Lock

Buzzer

ESP32 38 PINS

5V Relay

Flush Switch

Documented Code

ESP32-CAM Code

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

ESP32 38 PINS Code

/*
 * ESP32-Based Security System with PIR Sensor and Solenoid Lock Control
 * This system detects motion using a PIR sensor, triggers a buzzer, and
 * activates a solenoid lock. The ESP32 microcontroller interfaces with
 * the PIR sensor and controls the solenoid lock via a relay. Additionally,
 * it captures an image using the ESP32-CAM module when motion is detected
 * and uploads it to the cloud. Remote access is provided to control the lock.
 */

#include "esp_camera.h"
#include "FS.h"
#include "SD_MMC.h"
#include <WiFi.h>
#include <FirebaseESP32.h>

// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// Firebase project credentials
#define FIREBASE_HOST "your_project_id.firebaseio.com"
#define FIREBASE_AUTH "your_database_secret"

// Pin definitions
const int pirPin = 32; // PIR sensor signal pin
const int relayPin = 19; // Relay control pin
const int doorSensorPin = 34; // Door sensor pin
const int buzzerPin = 14; // Buzzer pin

// Camera configuration
camera_config_t config;

void setup() {
  // Initialize serial communication
  Serial.begin(115200);

  // Initialize WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Initialize Firebase
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  Firebase.reconnectWiFi(true);

  // Initialize pins
  pinMode(pirPin, INPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(doorSensorPin, INPUT);
  pinMode(buzzerPin, OUTPUT);

  // Initial states
  digitalWrite(relayPin, LOW); // Solenoid lock off
  digitalWrite(buzzerPin, LOW); // Buzzer off

  // Initialize camera
  // ... (camera configuration code omitted for brevity)
  // Camera init
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }

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

void loop() {
  // Read PIR sensor
  int pirState = digitalRead(pirPin);

  // Read door sensor
  int doorState = digitalRead(doorSensorPin);

  // If motion is detected
  if (pirState == HIGH) {
    Serial.println("Motion detected!");
    digitalWrite(buzzerPin, HIGH); // Turn on buzzer
    digitalWrite(relayPin, HIGH); // Activate solenoid lock

    // Capture image
    // ... (image capture and storage code omitted for brevity)

    // Upload image to Firebase
    // ... (Firebase upload code omitted for brevity)
  } else {
    digitalWrite(buzzerPin, LOW); // Turn off buzzer
    digitalWrite(relayPin, LOW); // Deactivate solenoid lock
  }

  // If door is open
  if (doorState == HIGH) {
    Serial.println("Door is open!");
  } else {
    Serial.println("Door is closed.");
  }

  // Small delay to avoid bouncing
  delay(100);
}

This code snippet is responsible for the main operation of the security system. It initializes the system, connects to Wi-Fi, sets up Firebase for image upload, and configures the camera. The loop function checks the PIR sensor and door sensor states, activates the buzzer and solenoid lock when motion is detected, captures an image, and uploads it to Firebase. It also provides feedback on the door state and ensures the system is responsive to changes in sensor states.