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

ESP8266-Based Smart Door Security System with Color Sensor and Wi-Fi Control

View and Edit Project In Cirkit DesignerImage of ESP8266-Based Smart Door Security System with Color Sensor and Wi-Fi 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 ESP8266 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. 1 Channel Relay 5V

    • Description: A relay module that allows a microcontroller to control high voltage devices.
    • Pins: VCC, GND, IN, NC, COM, NO
  2. 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
  3. Door MC-38

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

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

    • Description: A microcontroller with built-in WiFi capabilities.
    • Pins: A0, GND, VU, S3, S2, S1, SC, S0, SK, 3V3, EN, RST, Vin, D0, D1, D2, D3, D4, D5, D6, D7, D8, RX, TX
  6. 5V PSU

    • Description: A power supply unit that provides 5V DC.
    • Pins: 5V+, GND, 5V OK, PE, N, L

Wiring Details

1 Channel Relay 5V

TCS3472 COLOR LIGHT-TO-DIGITAL CONVERTER with IR FILTER

Door MC-38

Buzzer Module

NodeMCU V3 ESP8266

5V PSU

Code Documentation

/*
 * This Arduino Sketch is designed for an ESP8266 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>
#include <ESP8266WiFi.h>

#define BUZZER_PIN D8
#define RELAY_PIN D7
#define DOOR_SENSOR_PIN D6
#define COLOR_SENSOR_LED_PIN D1

const char* ssid = "NPID2024";
const char* password = "NPID2024";

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 = 180000; // 3 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);

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  if (tcs.begin()) {
    Serial.println("Found color sensor");
    // Read initial color values
    tcs.getRawData(&initialR, &initialG, &initialB, &initialC);
  } else {
    Serial.println("No TCS34725 found ... entering idle mode");
    enterIdleMode();
  }
}

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);
    enterDeepSleep();
    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();
    }
  }

  enterDeepSleep();
}

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

void enterIdleMode() {
  while (true) {
    delay(1000); // MCU in idle mode
  }
}

void enterDeepSleep() {
  // Turn off the color sensor LED to save power
  digitalWrite(COLOR_SENSOR_LED_PIN, LOW);
  // Enter deep sleep for 3 minutes (180000000 microseconds)
  ESP.deepSleep(180000000);
}

This code is designed for an ESP8266 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