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

ESP32-Based Smart Door Security System with Color Sensor and Relay Control

View and Edit Project In Cirkit DesignerImage of ESP32-Based Smart Door Security System with Color Sensor and Relay Control

Circuit Documentation

Summary

This circuit is designed to control a buzzer and a relay based on input from a TCS3472 color sensor and a Door MC-38 sensor using an ESP32 microcontroller. The buzzer and relay will be triggered every 5 minutes if there is a change in the color sensor. The system will stop if the door sensor is opened. If the door sensor is closed and the color sensor does not detect a change, the system will continue to trigger the buzzer and relay until the door sensor is opened and closed and the color sensor detects a change.

Component List

  1. ESP32-WROOM

    • Description: A powerful microcontroller with Wi-Fi and Bluetooth capabilities.
    • Pins: 3V3, EN, SP, SN, 34, 35, 32, 33, 25, 26, 27, 14, 12, GND, 13, 23, 22, TX0, RX0, 21, 19, 18, 5, 17, 16, 4, 0, 2, 15, SD1, SD0, CLK, 5V, CMD, SD3, SD2, USB
  2. 1 Channel Relay 5V

    • Description: A relay module that allows control of high voltage devices with a low voltage signal.
    • Pins: VCC, GND, IN, NC, COM, NO
  3. TCS3472 COLOR LIGHT-TO-DIGITAL CONVERTER with IR FILTER

    • Description: A color sensor that converts light to digital values.
    • Pins: LED, INT, SDA, SCL, 3V3, GND, VIN
  4. Door MC-38

    • Description: A magnetic door sensor used to detect the opening and closing of a door.
    • Pins: S, GND
  5. Buzzer Module

    • Description: A module that produces sound when activated.
    • Pins: GND, Vcc, I/O

Wiring Details

ESP32-WROOM

1 Channel Relay 5V

TCS3472 COLOR LIGHT-TO-DIGITAL CONVERTER with IR FILTER

Door MC-38

Buzzer Module

Code Documentation

/*
 * This Arduino Sketch is designed for an ESP32 microcontroller to control a buzzer
 * and a relay based on input from a TCS3472 color sensor and a Door MC-38 sensor.
 * The buzzer and relay will be triggered every 5 minutes if there is a change in
 * the color sensor. The system will stop if the door sensor is opened. If the
 * door sensor is closed and the color sensor does not detect a change, the
 * system will continue to trigger the buzzer and relay until the door sensor is
 * opened and closed and the color sensor detects a change.
 */

#include <Wire.h>
#include "Adafruit_TCS34725.h"

#define BUZZER_PIN 15
#define RELAY_PIN 17
#define DOOR_SENSOR_PIN 21
#define COLOR_SENSOR_LED_PIN 16

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

bool doorOpen = false;
uint16_t initialR, initialG, initialB, initialC;
const int debounceDelay = 50; // 50 milliseconds debounce delay
float colorChangeThreshold = 0.10; // 10% color change threshold
unsigned long colorCheckInterval = 300000; // 5 minutes in milliseconds

unsigned long lastDebounceTime = 0;
int lastDoorSensorState = HIGH;
int doorSensorState = HIGH;
unsigned long lastColorCheckTime = 0;

void setup() {
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(DOOR_SENSOR_PIN, INPUT);
  pinMode(COLOR_SENSOR_LED_PIN, OUTPUT);
  digitalWrite(COLOR_SENSOR_LED_PIN, HIGH); // Turn on the color sensor LED

  Serial.begin(115200);

  if (tcs.begin()) {
    Serial.println("Found color sensor");
    // Read initial color values
    tcs.getRawData(&initialR, &initialG, &initialB, &initialC);
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1); // Consider adding a more graceful error handling mechanism
  }
}

void loop() {
  int reading = digitalRead(DOOR_SENSOR_PIN);

  // Debounce the door sensor input
  if (reading != lastDoorSensorState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != doorSensorState) {
      doorSensorState = reading;
      doorOpen = (doorSensorState == LOW);
    }
  }

  lastDoorSensorState = reading;

  if (doorOpen) {
    digitalWrite(BUZZER_PIN, LOW);
    digitalWrite(RELAY_PIN, LOW);
    return;
  }

  if (millis() - lastColorCheckTime >= colorCheckInterval) {
    lastColorCheckTime = millis();
    uint16_t r, g, b, c;
    tcs.getRawData(&r, &g, &b, &c);
    bool colorChanged = (abs(r - initialR) > initialR * colorChangeThreshold ||
                         abs(g - initialG) > initialG * colorChangeThreshold ||
                         abs(b - initialB) > initialB * colorChangeThreshold);

    if (colorChanged) {
      triggerBuzzerAndRelay();
    }
  }
}

void triggerBuzzerAndRelay() {
  digitalWrite(BUZZER_PIN, HIGH);
  digitalWrite(RELAY_PIN, HIGH);
  delay(500);
  digitalWrite(BUZZER_PIN, LOW);
  digitalWrite(RELAY_PIN, LOW);
}

This code initializes the ESP32 microcontroller to read inputs from the TCS3472 color sensor and the Door MC-38 sensor. It controls a buzzer and a relay based on the sensor inputs. The buzzer and relay are triggered every 5 minutes if there is a change in the color sensor, and the system stops if the door sensor is opened.