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

NodeMCU ESP8266 Smart Door Security System with Color Sensor and Relay Control

View and Edit Project In Cirkit DesignerImage of NodeMCU ESP8266 Smart Door Security System with Color Sensor and Relay Control

Circuit Documentation

Summary

This circuit is designed to detect the color of an object using a TCS3472 color sensor and trigger a relay and buzzer based on the detected color. The system is controlled by a NodeMCU V3 ESP8266 microcontroller. The circuit also includes a door sensor and a buzzer module for additional functionalities.

Component List

  1. 1 Channel Relay 5V

    • Description: A relay module that allows a low-power microcontroller to control a high-power device.
    • 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 Wi-Fi 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 output.
    • 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

Documented Code

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

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

const int buzzerPin = D8; // Define the buzzer pin
const int relayPin = D7;  // Define the relay pin (corrected from D4 to D7)
const int ledPin = D4;    // Define the LED pin
int brownCount = 0; // Counter for empty canister color detections

void setup() {
    Serial.begin(115200);
    pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as an output
    pinMode(relayPin, OUTPUT);  // Set the relay pin as an output
    pinMode(ledPin, OUTPUT);    // Set the LED pin as an output
    digitalWrite(ledPin, LOW);  // Ensure the LED is off initially

    if (tcs.begin()) {
        Serial.println("Found sensor");
    } else {
        Serial.println("No TCS34725 found, check connections");
        while (1);
    }
}

void loop() {
    uint16_t r, g, b, c, colorTemp, lux;

    // Turn on the LED before reading the sensor
    digitalWrite(ledPin, HIGH);

    // Read color data from the sensor
    tcs.getRawData(&r, &g, &b, &c);
    colorTemp = tcs.calculateColorTemperature(r, g, b);
    lux = tcs.calculateLux(r, g, b);

    // Turn off the LED after reading the sensor
    digitalWrite(ledPin, LOW);

    // Print the color data to the serial monitor
    Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
    Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
    Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
    Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
    Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
    Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
    Serial.println(" ");

    // Check if the color is brown
    if (isBrown(r, g, b)) {
        brownCount++; // Increment the brown color detection counter

        if (brownCount % 10 == 0) {
            // Trigger the buzzer twice but for a longer duration
            for (int i = 0; i < 2; i++) {
                digitalWrite(buzzerPin, HIGH);
                digitalWrite(relayPin, HIGH);
                delay(1000); // Longer duration
                digitalWrite(buzzerPin, LOW);
                digitalWrite(relayPin, LOW);
                delay(200); // Short delay between beeps
            }
        } else {
            // Trigger the buzzer 3 times
            for (int i = 0; i < 3; i++) {
                digitalWrite(buzzerPin, HIGH);
                digitalWrite(relayPin, HIGH);
                delay(200);
                digitalWrite(buzzerPin, LOW);
                digitalWrite(relayPin, LOW);
                delay(200); // Short delay between beeps
            }
        }
    } else {
        // Trigger the buzzer once
        digitalWrite(buzzerPin, HIGH);
        digitalWrite(relayPin, HIGH);
        delay(200);
        digitalWrite(buzzerPin, LOW);
        digitalWrite(relayPin, LOW);
    }

    // Wait for 1 minute (60000 milliseconds) before the next reading
    delay(60000);
}

// Function to determine if the canister is empty
bool isBrown(uint16_t r, uint16_t g, uint16_t b) {
    // Define the range for empty Canister
    // These values might need to be adjusted based on your specific sensor and environment
    return (r > 1000 && r < 1500) && (g > 1100 && g < 1900) && (b > 750 && b < 1300);
}

This code initializes the TCS3472 color sensor and sets up the pins for the buzzer, relay, and LED. It reads the color data from the sensor and triggers the buzzer and relay based on the detected color. The isBrown function determines if the detected color matches the predefined range for an empty canister.