ESP32 Security Camera
Materials Needed
To build a DIY ESP32 security camera, you’ll need:
1. ESP32 Dev Board (e.g., ESP32-WROOM-32, ESP32 DevKitC). -not needed if using breakout board.
2. Camera Module (e.g., OV2640, OV3660, or ESP32-Cam breakout board).
3. MicroSD Card (for video storage).
4. Power Supply: USB cable or battery (3.7V LiPo for portability).
5. Breadboard/Protoboard (for wiring).
6. Wires (Jumper cables).
7. Enclosure (Can be purchased or made) 8. Optional: Motion sensor (PIR), external antenna, buzzer, or cloud service (e.g., AWS, Firebase).


I used this ESP32CAM and breakout board.
1. Install Arduino IDE:
- Download from [arduino.cc](https://www.arduino.cc).


2. Add ESP32 Board Support:
- Go to Tools > Board > Boards Manager.
- Search for ESP32 and install the ESP32 by Espressif package.


3. Install Required Libraries:
- ESP32 Camera Library:
- Go to Sketch > Include Library > Manage Libraries.
- Search for ESP32 Camera and install.
- WiFi & SD Libraries: Included by default in Arduino.




### Step 2: Connect the Components
1. Camera Module to ESP32 (These connections are already made if using the ESP32CAM with breakout board shown above):
- OV2640/OV3660 (common for DIY):
- SCL → GPIO 23 (I2C SDA).
- SDA → GPIO 18 (I2C SCL).
- VCC → 3.3V.
- GND → GND.
- XCLK → GPIO 5 (or 26 for OV2640).
- HREF → GPIO 27 (or 25 for OV2640).
- PCLK → GPIO 25 (or 26 for OV2640).
- Data Pins (D0-D7) → GPIO 19-22 (or 16-19 for OV2640).
- ESP32-Cam Breakout Board: Plug directly into the ESP32 pins (no soldering needed).


ESP32CAM connected to breakout board.
```cpp
#include <WiFi.h>
#include <esp_camera.h>
#include <SD.h>
#include <FS.h>
// WiFi credentials
const char* ssid = "YOUR_WIFI_SSID"; // Replace with your network name
const char* password = "YOUR_WIFI_PASSWORD"; // Replace with your network password
// Camera pins (uncomment and adjust gpio numbers according to your module if using separate camera and ESP32 board)
#define PWDN_GPIO -1
#define RESET_GPIO -1
#define XCLK_GPIO 5
#define SIOD_GPIO 23
#define SIOC_GPIO 18
#define VSYNC_GPIO 25
#define HREF_GPIO 27
#define PCLK_GPIO 26
#define D0_GPIO 19
#define D1_GPIO 21
#define D2_GPIO 22
#define D3_GPIO 20
#define D4_GPIO 17
#define D5_GPIO 16
#define D6_GPIO 15
#define D7_GPIO 14
// SD card settings
#define SD_CS_PIN 5
#define SD_MMC_PIN 5
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
// Initialize camera
camera_config_t config;
config.ledc_nr = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_used = {XCLK_GPIO, SIOD_GPIO, SIOC_GPIO, VSYNC_GPIO, HREF_GPIO, PCLK_GPIO};
config.pin_d0 = D0_GPIO;
config.pin_d1 = D1_GPIO;
config.pin_d2 = D2_GPIO;
config.pin_d3 = D3_GPIO;
config.pin_d4 = D4_GPIO;
config.pin_d5 = D5_GPIO;
config.pin_d6 = D6_GPIO;
config.pin_d7 = D7_GPIO;
config.pin_pwdn = PWDN_GPIO;
config.pin_reset = RESET_GPIO;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
config.frame_size = FRAMESIZE_UXGA; // Adjust for resolution
// Initialize camera
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
SPIFFS.begin();
if (!SD.begin(SD_CS_PIN)) {
Serial.println("SD card failed to initialize.");
return;
}
}
void loop() {
// Capture video frames
camera_fb_t* fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return;
}
// Save frame to SD card (example: save as JPEG)
String filename = "/video_";
filename += String(millis() / 1000); // Save as timestamp
filename += ".jpg";
File file = SD.open(filename, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
esp_camera_fb_release(fb);
return;
}
file.write(fb->buf, fb->len);
file.close();
esp_camera_fb_release(fb);
// Optional: Stream video over Wi-Fi (use WiFiClient or WebServer)
// Add code for live streaming here (e.g., using ESP32-Cam's built-in HTTP server)
delay(1000); // Adjust frame rate
}
```
---
### Step 4: Upload and Test
1. Upload the Code:
- Connect your ESP32 to your computer via USB.
- Select the correct board and port in Arduino IDE.
- Click Upload.
2. Verify Functionality:
- Live Streaming: Use a web browser to access the ESP32’s IP address (e.g., `http://192.168.x.x`).
- SD Card Storage: Check the SD card for saved video clips (e.g., `video_12345.jpg`).
- Power: Use a USB power bank for portable use.
3. Enclosure: - Hidden Camera: Any item with space for the camera and board can be modified by making holes for camera and power connections. - Project Box: Project boxes can be purchased from some hobby stores or shopping sites. - 3D Printed: 3D print files for enclosures can be found at https://www.printables.com/search/models?q=esp32cam
### Optional Enhancements
1. Motion Detection:
- Add a PIR motion sensor (e.g., HC-SR501) to trigger video recording.
- Use a buzzer or LED to alert when motion is detected.
2. Cloud Integration:
- Stream video to AWS IoT, Firebase, or Home Assistant for remote access.
3. Night Vision:
- Add an IR LED array for low-light visibility.
4. Voice Alerts:
- Connect a microphone and use the ESP32’s audio libraries to capture and transmit sound.
---
### Troubleshooting Tips
- Camera Not Working: Double-check I2C/SPI connections and pin definitions.
- SD Card Errors: Format the card as FAT32 using tools like SDFormatter.
- WiFi Issues: Ensure your ESP32 is within range of your Wi-Fi router.
- Power Supply: Use a stable 3.3V power source to avoid resets.
---
**Conclusion**: With this ESP32 security camera setup, you’ve created a low-cost, customizable surveillance system perfect for home or small business use.
Expand it with motion detection, cloud storage, or night vision for even more functionality!
Write your text hOptional Enhancements for Your ESP32 Security Camera
### 1. Motion Detection with PIR Sensor
**Materials Needed**:
- PIR Motion Sensor (e.g., HC-SR501).
- Resistor (10kΩ for pull-down).
- Wires.
**Wiring**:
- Connect the VCC of the PIR to 3.3V on the ESP32.
- Connect the GND of the PIR to GND.
- Connect the OUT (signal pin) to a GPIO pin (e.g., GPIO 13).
- Add a 10kΩ resistor between the PIR’s OUT and GND for pull-down.
**Code Modifications**:
```cpp
// Add these variables
#define PIR_PIN 13
bool motionDetected = false;
void setup() {
// Initialize PIR pin
pinMode(PIR_PIN, INPUT);
}
void loop() {
if (digitalRead(PIR_PIN) == HIGH) {
motionDetected = true;
Serial.println("Motion detected!");
// Trigger video recording
// Add code to save frames to SD card or stream
} else {
motionDetected = false;
}
}
```
**Optional Add-Ons**:
- Add a buzzer (e.g., 5V piezo) to alert when motion is detected.
- Use an LED (e.g., 3mm) to flash when motion is triggered.
---
### 2. Cloud Integration (AWS IoT, Firebase, or Home Assistant)
**A. AWS IoT**
**Steps**:
1. Create an AWS IoT Core thing and get the endpoint, certificate, and private key.
2. Install the AWS IoT SDK for ESP32:
```bash
Arduino IDE > Sketch > Include Library > Add .ZIP Library > aws-iot-device-sdk-arduino-1.0.3.zip
```
3. Modify the code to connect to AWS and stream video:
```cpp
#include <AWSIoT.h>
AWSIoT awsIoT;
void setup() {
awsIoT.begin("your-thing-name", "your-thing-cert", "your-private-key", "your-aws-endpoint");
}
void loop() {
// Stream video frames to AWS IoT
awsIoT.publish("video/stream", frameData, frameSize);
}
```
**B. Firebase**
**Steps**:
1. Create a Firebase project and enable Cloud Messaging.
2. Install the Firebase Arduino Library:
```bash
Arduino IDE > Sketch > Include Library > Add .ZIP Library > FirebaseArduino-1.0.10.zip
```
3. Modify the code to upload frames to Firebase:
```cpp
FirebaseData data;
if (Firebase.upload("/video", fb->buf, fb->len)) {
Serial.println("Frame uploaded to Firebase!");
}
```
**C. Home Assistant (MQTT)**
**Steps**:
1. Set up MQTT broker (e.g., Mosquitto).
2. Install the ESP32 MQTT library in Arduino IDE.
3. Modify the code to publish frames via MQTT:
```cpp
WiFiClient client;
MQTTClient mqttClient(client);
void setup() {
mqttClient.setServer("mqtt.broker.ip", 1883);
mqttClient.connect("ESP32");
}
void loop() {
mqttClient.publish("video/stream", frameData, frameSize);
}
```
---
### 3. Night Vision with IR LEDs
**Materials Needed**:
- IR LED array (e.g., 10x IR LEDs).
- Resistor (220Ω for each LED).
- Power source (5V for IR LEDs, 3.3V for ESP32).
**Wiring**:
- Connect the anode of each IR LED to 5V.
- Connect the cathode to a GPIO pin (e.g., GPIO 12) via a 220Ω resistor.
- Use a transistor (e.g., 2N2222) to control the LEDs with 3.3V.
**Code Modifications**:
```cpp
#define IR_PIN 12
void setup() {
pinMode(IR_PIN, OUTPUT);
digitalWrite(IR_PIN, HIGH); // Turn on IR LEDs
}
void loop() {
// Adjust IR intensity based on ambient light
if (lightLevel < 100) {
digitalWrite(IR_PIN, HIGH);
} else {
digitalWrite(IR_PIN, LOW);
}
}
```
---
### 4. Voice Alerts with Microphone
**Materials Needed**:
- Microphone module (e.g., VS1053).
- Wires.
**Wiring**:
- Connect the VCC of the microphone to 3.3V.
- Connect the GND to GND.
- Connect the DATA pin to a GPIO pin (e.g., GPIO 34).
**Code Modifications**:
```cpp
#include <ESP32Audio.h>
ESP32Audio audio;
void setup() {
audio.begin();
}
void loop() {
// Capture and transmit audio
audio.record();
audio.play();
}
```
---
### 5. Power Management with LiPo Battery
**Materials Needed**:
- LiPo battery (3.7V).
- Voltage regulator (e.g., AMS1117 3.3V).
- USB cable for charging.
**Wiring**:
- Connect the LiPo battery to the regulator’s input.
- Connect the regulator’s output to 3.3V on the ESP32.
- Use a USB power bank for extended use.
**Tips**:
- Use a battery charger module (e.g., TP4056) for safe charging.
- Add a power switch to turn off the ESP32 when not in use.
---
### Troubleshooting Tips for Enhancements
- PIR Sensor: Ensure the resistor is properly connected and the GPIO pin is set to INPUT.
- Cloud Integration: Verify credentials, network connectivity, and library versions.
- IR LEDs: Test with a multimeter to ensure they’re receiving 5V.
- Microphone: Check for noise interference and adjust gain settings.
- Power Supply: Use a stable 3.3V regulator to avoid ESP32 resets.
---
**Conclusion**: These enhancements transform your ESP32 camera into a versatile surveillance system! Use motion detection for security, cloud
integration for remote access, and night vision for low-light environments. Add voice alerts and power management for a fully featured solution.
Experiment with combinations to suit your needs!
