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.
ESP32-WROOM
1 Channel Relay 5V
TCS3472 COLOR LIGHT-TO-DIGITAL CONVERTER with IR FILTER
Door MC-38
Buzzer Module
/*
* 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"
#include "BluetoothSerial.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);
BluetoothSerial SerialBT;
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);
SerialBT.begin("ESP32_BT"); // Bluetooth device name
SerialBT.setPin("TAOM2024"); // Bluetooth password
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();
}
}
if (SerialBT.available()) {
String command = SerialBT.readStringUntil('\n');
processCommand(command);
}
}
void triggerBuzzerAndRelay() {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(RELAY_PIN, HIGH);
delay(500);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(RELAY_PIN, LOW);
}
void processCommand(String command) {
command.trim();
if (command.startsWith("SET_THRESHOLD ")) {
float newThreshold = command.substring(14).toFloat();
if (newThreshold > 0 && newThreshold <= 1) {
colorChangeThreshold = newThreshold;
SerialBT.println("Threshold set to " + String(newThreshold * 100) + "%");
} else {
SerialBT.println("Invalid threshold value. Please enter a value between 0 and 1.");
}
} else if (command.startsWith("SET_INTERVAL ")) {
unsigned long newInterval = command.substring(13).toInt() * 60000;
if (newInterval > 0) {
colorCheckInterval = newInterval;