#include #include #include #define DHTPIN 7 // DHT11 Data Pin connected to D7 #define LDRPIN A0 // LDR Pin connected to A0 (analog) #define MICPIN A1 // Microphone Pin connected to A1 (analog) #define DHTTYPE DHT11 // DHT11 Sensor Type DHT dht(DHTPIN, DHTTYPE); LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD (20x4, I2C address 0x27) #define BUZZER_PIN 6 // Buzzer pin set to 6 #define LED_HUMIDITY 2 // LED for High Humidity #define LED_TEMP_EXTREME 3 // LED for Extreme Temperature #define LED_SOUND_HIGH 4 // LED for High Sound Level unsigned long previousMillis = 0; const long interval = 2000; // Switch the display every 2 seconds (2000ms) bool displayTempHumid = true; // Flag to toggle between temperature/humidity and light/sound // Set thresholds for triggering events #define TEMP_THRESHOLD 30 // Extreme temperature threshold in Celsius (30°C) #define SOUND_THRESHOLD 800 // High sound level threshold (analog value from microphone) #define HUMIDITY_THRESHOLD 60 // High humidity threshold (%) void setup() { Serial.begin(9600); lcd.begin(); // Initialize the LCD without specifying dimensions lcd.backlight(); dht.begin(); // Set up the pins for LEDs and Buzzer pinMode(BUZZER_PIN, OUTPUT); // Ensure the buzzer pin is set to OUTPUT pinMode(LED_HUMIDITY, OUTPUT); pinMode(LED_TEMP_EXTREME, OUTPUT); pinMode(LED_SOUND_HIGH, OUTPUT); lcd.setCursor(0, 0); lcd.print("Initializing..."); delay(1000); lcd.clear(); } void loop() { // Read sensor data float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); int lightLevel = analogRead(LDRPIN); int soundLevel = analogRead(MICPIN); float soundDB = map(soundLevel, 0, 1023, 30, 90); // Simulated dB-like value // Update the LCD 16 times per second unsigned long currentMillis = millis(); // Refresh the display every 2 seconds if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // Update the display time lcd.clear(); // Toggle between displaying temperature/humidity and light/sound if (displayTempHumid) { lcd.setCursor(0, 0); lcd.print("Temp: "); lcd.print(temperature); lcd.print("C"); lcd.setCursor(0, 1); lcd.print("Humidity: "); lcd.print(humidity); lcd.print("%"); } else { lcd.setCursor(0, 0); lcd.print("Light: "); lcd.print(lightLevel); lcd.setCursor(0, 1); lcd.print("Sound: "); lcd.print(soundDB); } displayTempHumid = !displayTempHumid; // Toggle the display } // Check for thresholds and trigger LEDs and buzzer bool buzzerTriggered = false; String triggeredSensor = ""; // Check if temperature exceeds the extreme threshold if (temperature > TEMP_THRESHOLD) { digitalWrite(LED_TEMP_EXTREME, HIGH); // Turn on extreme temperature LED triggeredSensor = "Extreme Temperature"; buzzerTriggered = true; } else { digitalWrite(LED_TEMP_EXTREME, LOW); // Turn off extreme temperature LED } // Check if sound exceeds the high sound threshold if (soundLevel > SOUND_THRESHOLD) { digitalWrite(LED_SOUND_HIGH, HIGH); // Turn on high sound LED triggeredSensor = "High Sound Level"; buzzerTriggered = true; } else { digitalWrite(LED_SOUND_HIGH, LOW); // Turn off high sound LED } // Check if humidity exceeds the high humidity threshold if (humidity > HUMIDITY_THRESHOLD) { digitalWrite(LED_HUMIDITY, HIGH); // Turn on high humidity LED triggeredSensor = "High Humidity"; buzzerTriggered = true; } else { digitalWrite(LED_HUMIDITY, LOW); // Turn off high humidity LED } // Trigger buzzer and print which sensor caused the trigger if (buzzerTriggered) { Serial.print("Buzzer Triggered by: "); Serial.println(triggeredSensor); digitalWrite(BUZZER_PIN, HIGH); // Turn on buzzer delay(1000); // Buzzer beeps for 1 second digitalWrite(BUZZER_PIN, LOW); // Turn off buzzer after 1 second // Debugging output Serial.println("Buzzer is ON for 1 second."); } delay(62); // A small delay to help update the LCD 16 times per second }