#include <DHT.h>
#include <LiquidCrystal.h> // Library for LCD
#define DHTPIN 5
#define DHTTYPE DHT22
#define LED_PIN 3
#define BUZZER_PIN 8
#define LCD_RS 12 // Register select pin
#define LCD_EN 11 // Enable pin
#define LCD_D4 6 // Data pin 4 // Changed from 5 to avoid conflict with DHTPIN
#define LCD_D5 4 // Data pin 5
#define LCD_D6 3 // Data pin 6
#define LCD_D7 2 // Data pin 7
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Initialize the LCD
lcd.begin(16, 2); // 16x2 character LCD
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.setCursor(10, 0);
lcd.print("%");
lcd.setCursor(0,1); // Added this line to print temperature on the second row of the LCD
lcd.print("Temp: ");
lcd.setCursor(10,1);
lcd.print("C");
digitalWrite(LED_PIN, LOW); // Turn off the LED initially
noTone(BUZZER_PIN); // Turn off the buzzer initially
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read sensor data.");
return;
}
lcd.setCursor(10,0); // Moved this line to update the humidity value on the first row of the LCD
lcd.print(humidity);
lcd.setCursor(10,1); // Added this line to update the temperature value on the second row of the LCD
lcd.print(temperature);
if (temperature >= 20.0 && temperature <= 25.0 && humidity >=40 && humidity <=70) {
noTone(BUZZER_PIN); // Turn off the buzzer
digitalWrite(LED_PIN, LOW); // Turn off the LED
Serial.println("Safe Conditions");
} else if (temperature >=20.0 && temperature <=25.0) {
tone(BUZZER_PIN,200); // Changed from 100 to make it more distinct from polluted or dangerous conditions
digitalWrite(LED_PIN,HIGH); // Turn on the LED
Serial.println("Normal Conditions");
} else {
tone(BUZZER_PIN,100);
digitalWrite(LED_PIN,HIGH); // Turn on the LED
Serial.println("Polluted or Dangerous Conditions");
}
}