#include <Wire.h> // Include the Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include the LCD library for I2C interface
#include <NewPing.h> // Include the NewPing library for ultrasonic sensor
#include <DHT.h> // Include the DHT library for DHT22 sensor
#define DHTPIN 14 // Define the pin for DHT22 sensor
#define DHTTYPE DHT22 // Define the type of DHT sensor
#define TRIGGER_PIN 18 // Define the trigger pin for ultrasonic sensor
#define ECHO_PIN 17 // Define the echo pin for ultrasonic sensor
#define MAX_DISTANCE 200 // Define the maximum distance to measure (in cm)
#define BUZZER_PIN 12 // Define the pin for the buzzer
#define RED_LED_PIN 19 // Define the pin for the red LED
#define BLUE_LED_PIN 26 // Define the pin for the blue LED
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD with I2C address and dimensions
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Initialize ultrasonic sensor
void setup() {
Serial.begin(9600); // Start serial communication
dht.begin(); // Start DHT sensor
lcd.init(); // Initialize LCD
lcd.backlight(); // Turn on backlight
pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as output
pinMode(RED_LED_PIN, OUTPUT); // Set red LED pin as output
pinMode(BLUE_LED_PIN, OUTPUT); // Set blue LED pin as output
}
void loop() {
float temperature = dht.readTemperature(); // Read temperature from DHT sensor
float humidity = dht.readHumidity(); // Read humidity from DHT sensor
float distance = sonar.ping_cm(); // Read distance from ultrasonic sensor
// Display temperature and humidity on LCD
lcd.clear();
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("%");
// Delay to allow time for the user to read the display
delay(2000); // Adjust delay time as needed
// Check for danger conditions
if (temperature > 60 || humidity > 60 || distance < 20) { // If temperature or humidity is above 60, or distance is less than 20cm
digitalWrite(RED_LED_PIN, HIGH); // Turn on red LED
digitalWrite(BLUE_LED_PIN, LOW); // Turn off blue LED
lcd.clear(); // Clear the display before showing danger alert
lcd.setCursor(0, 0);
lcd.print("Danger!"); // Display danger on LCD
tone(BUZZER_PIN, 1000, 1000);
}
else {
digitalWrite(RED_LED_PIN, LOW); // Turn off red LED
digitalWrite(BLUE_LED_PIN, HIGH); // Turn on blue LED
lcd.clear(); // Clear the display before showing safe message
lcd.setCursor(0, 0);
lcd.print("Safe"); // Display safe on LCD
}
delay(1000); // Delay for stability
}