ESP8266-Based Smart Door Security System with Color Sensor and Wi-Fi Control
View and Edit Project In Cirkit Designer
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 Channel Relay 5V
- Description: A relay module that allows a microcontroller to control high voltage devices.
- Pins: VCC, GND, IN, NC, COM, NO
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
Door MC-38
- Description: A magnetic door sensor used to detect the opening and closing of a door.
- Pins: S, GND
Buzzer Module
- Description: A module that produces sound when activated.
- Pins: GND, Vcc, I/O
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
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
- VCC connected to 5V+ of 5V PSU
- GND connected to GND of 5V PSU
- IN connected to D7 of NodeMCU V3 ESP8266
TCS3472 COLOR LIGHT-TO-DIGITAL CONVERTER with IR FILTER
- LED connected to D1 of NodeMCU V3 ESP8266
- INT connected to D2 of NodeMCU V3 ESP8266
- SDA connected to D3 of NodeMCU V3 ESP8266
- SCL connected to D4 of NodeMCU V3 ESP8266
- 3V3 connected to 3V3 of NodeMCU V3 ESP8266
- GND connected to GND of NodeMCU V3 ESP8266
- VIN connected to 5V+ of 5V PSU
Door MC-38
- S connected to D6 of NodeMCU V3 ESP8266
- GND connected to GND of NodeMCU V3 ESP8266
Buzzer Module
- GND connected to GND of 5V PSU
- Vcc connected to 5V+ of 5V PSU
- I/O connected to D8 of NodeMCU V3 ESP8266
NodeMCU V3 ESP8266
- Vin connected to 5V+ of 5V PSU
- GND connected to GND of 5V PSU
5V PSU
- 5V+ connected to VCC of 1 Channel Relay 5V, VIN of TCS3472 COLOR LIGHT-TO-DIGITAL CONVERTER with IR FILTER, Vcc of Buzzer Module, and Vin of NodeMCU V3 ESP8266
- GND connected to GND of 1 Channel Relay 5V, GND of Buzzer Module, GND of NodeMCU V3 ESP8266, and GND of TCS3472 COLOR LIGHT-TO-DIGITAL CONVERTER with IR FILTER
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