ESP32-Based Security System with PIR Sensor and Solenoid Lock Control
View and Edit Project In Cirkit Designer
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
- Description: A small-sized ESP32-based module with Wi-Fi and camera capabilities.
- Purpose: Captures images for security purposes and provides Wi-Fi connectivity.
PIR Sensor
- Description: A passive infrared sensor that detects motion by measuring changes in the infrared levels emitted by surrounding objects.
- Purpose: Triggers the security system upon detecting motion.
Door MC-38
- Description: A magnetic contact switch that detects the opening and closing of doors.
- Purpose: Monitors the state of a door to detect unauthorized access.
12V Solenoid Lock
- Description: An electromechanical lock that can be controlled via electrical signals.
- Purpose: Secures the door and is controlled by the relay.
Buzzer
- Description: An electronic buzzer that emits an audible alert.
- Purpose: Provides an audible alert when motion is detected or the door is opened.
ESP32 38 PINS
- Description: A microcontroller with a wide range of GPIO pins and Wi-Fi capabilities.
- Purpose: Acts as the central controller for the security system, interfacing with sensors and actuators.
5V Relay
- Description: An electromechanical switch that can be controlled with a low-power signal.
- Purpose: Controls the power to the solenoid lock.
Flush Switch
- Description: A manual switch that can be used to trigger events or override automatic controls.
- Purpose: Provides manual control over the system for maintenance or emergency purposes.
Wiring Details
ESP32-CAM
- 5V: Connected to the 5V power net.
- GND: Connected to the ground net.
- IO4: Connected to the PIR sensor signal output.
PIR Sensor
- VDD: Connected to the 3.3V power net.
- SIG: Connected to ESP32-CAM IO4 and ESP32 38 PINS G32.
- GND: Connected to the ground net.
Door MC-38
- S: Connected to ESP32 38 PINS G34.
- GND: Connected to the ground net.
12V Solenoid Lock
- +: Connected to the common terminal of the 5V relay.
- -: Connected to the ground net.
Buzzer
- PIN: Connected to ESP32 38 PINS G14.
- GND: Connected to the ground net.
ESP32 38 PINS
- GND: Connected to the ground net.
- 5V: Connected to the 5V power net.
- G32: Connected to the PIR sensor signal output.
- G34: Connected to Door MC-38 sensor output.
- G14: Connected to the buzzer.
- G19: Connected to the input of the 5V relay.
5V Relay
- Normally Open: Not connected in this circuit.
- Common terminal: Connected to the positive terminal of the 12V Solenoid Lock.
- Normally Closed: Not connected in this circuit.
- In: Connected to ESP32 38 PINS G19.
- GND: Connected to the ground net.
- VCC: Connected to the 5V power net.
Flush Switch
- GND: Connected to the ground net.
- VCC: Connected to the 5V power net.
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.