In an era where home security is paramount, building a smart home security system with Arduino can be both an educational and practical project. This system will use motion detection and a camera module to capture images when movement is detected, enhancing your home's security.
Components Needed
To build this project, you'll need the following components:
- Arduino Uno
- PIR motion sensor
- ESP32-CAM module
- MicroSD card for the ESP32-CAM
- Jumper wires
- Breadboard
- 5V power supply or USB cable for Arduino
Step-by-Step Guide
Step 1: Set Up the PIR Motion Sensor
The Passive Infrared (PIR) motion sensor detects movement by measuring infrared light from objects in its field of view.
- Connect the VCC pin of the PIR sensor to the 5V pin on the Arduino.
- Connect the GND pin to the GND pin on the Arduino.
- Connect the OUT pin to any digital input pin on the Arduino (e.g., D2).
Step 2: Prepare the ESP32-CAM Module
The ESP32-CAM module will capture images when motion is detected. It has an integrated camera and a microSD card slot for storing images.
- Insert the microSD card into the ESP32-CAM module.
- Connect the 5V pin on the ESP32-CAM to the 5V pin on the Arduino.
- Connect the GND pin to the GND pin on the Arduino.
- Connect the RX pin to the TX pin on the Arduino and the TX pin to the RX pin on the Arduino for serial communication.
Step 3: Assemble the Circuit
Ensure all connections are secure. Double-check the wiring to ensure everything is connected correctly.
Step 4: Install Libraries and Board in the Arduino IDE
You'll need to install a few libraries and the ESP32 board in the Arduino IDE:
- Go to File -> Preferences and add the following URL to the Additional Boards Manager URLs field:
https://dl.espressif.com/dl/package_esp32_index.json
. - Go to Tools -> Board -> Boards Manager, search for "ESP32," and install the ESP32 board package.
- Install the necessary libraries for the ESP32-CAM module.
Step 5: Write the Arduino Code
Now, it’s time to write the code that will handle motion detection and image capture.
Code for the Arduino Uno:
const int pirPin = 2;
void setup() {
pinMode(pirPin, INPUT);
Serial.begin(115200);
}
void loop() {
int motionDetected = digitalRead(pirPin);
if (motionDetected == HIGH) {
Serial.println("Motion detected!");
delay(5000); // Delay to avoid multiple triggers
}
}
Code for the ESP32-CAM:
// Replace with your network credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
Serial.begin(115200);
// Initialize the camera
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = 5;
config.pin_d1 = 18;
config.pin_d2 = 19;
config.pin_d3 = 21;
config.pin_d4 = 36;
config.pin_d5 = 39;
config.pin_d6 = 34;
config.pin_d7 = 35;
config.pin_xclk = 0;
config.pin_pclk = 22;
config.pin_vsync = 25;
config.pin_href = 23;
config.pin_sscb_sda = 26;
config.pin_sscb_scl = 27;
config.pin_pwdn = 32;
config.pin_reset = -1;
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;
}
// 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 the SD card
if(!SD_MMC.begin()){
Serial.println("SD Card Mount Failed");
return;
}
uint8_t cardType = SD_MMC.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected");
}
void loop() {
if (Serial.available() > 0) {
String motionDetected = Serial.readString();
if (motionDetected.indexOf("Motion detected!") >= 0) {
capturePhotoSaveSD();
}
}
}
void capturePhotoSaveSD() {
camera_fb_t * fb = NULL;
// Take Picture with Camera
fb = esp_camera_fb_get();
if(!fb) {
Serial.println("Camera capture failed");
return;
}
// Save picture to microSD card
String path = "/picture.jpg";
fs::FS &fs = SD_MMC;
File file = fs.open(path.c_str(), FILE_WRITE);
if(!file){
Serial.println("Failed to open file in writing mode");
}
else {
file.write(fb->buf, fb->len); // payload (image), payload length
Serial.printf("Saved file to path: %s\n", path.c_str());
file.close();
}
esp_camera_fb_return(fb);
}
Step 6: Upload the Code
Connect your Arduino Uno and ESP32-CAM to your computer via USB and upload the respective codes using the Arduino IDE. Ensure you select the correct board and port for each device.
Step 7: Test the System
Place the PIR motion sensor in a location where it can detect movement. Ensure the ESP32-CAM is positioned to capture images when motion is detected. Power up the system and simulate movement in front of the sensor. The system should detect motion and capture an image, saving it to the microSD card.
Conclusion
Congratulations! You've built a smart home security system using Arduino. This project not only enhances your understanding of sensors and camera modules but also provides a practical application for home security. You can further expand this project by adding internet connectivity to send alerts or images to your smartphone.
Stay secure and happy building!