#include <DHTesp.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// DHT11 setup
#define DHTPIN 15 // Pin connected to the DHT11 on ESP32
DHTesp dht;
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns and 2 rows
// LED setup
#define RED_LED_PIN 13 // Pin connected to the red LED
#define GREEN_LED_PIN 12 // Pin connected to the green LED
#define BLUE_LED_PIN 14 // Pin connected to the blue LED
// Buzzer setup
#define BUZZER_PIN 25 // Pin connected to the buzzer
// LDR setup
#define LDR_PIN 34 // Analog pin connected to the LDR
void setup() {
Serial.begin(115200);
dht.setup(DHTPIN, DHTesp::DHT22); // Initialize the DHT sensor for DHT11
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor Test");
delay(2000);
lcd.clear();
// Initialize the LEDs
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
// Initialize the buzzer
pinMode(BUZZER_PIN, OUTPUT);
// Turn off all LEDs initially
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
noTone(BUZZER_PIN); // Ensure the buzzer is off initially
}
void loop() {
delay(2000); // Wait for 2 seconds between readings
// Read temperature and humidity from DHT11
float temperature = dht.getTemperature();
float humidity = dht.getHumidity();
// Read LDR value
int ldrValue = analogRead(LDR_PIN);
float lightLevel = map(ldrValue, 0, 4095, 0, 100); // Map to percentage (0-100%)
// Check if temperature and humidity readings are valid
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Error Reading");
lcd.setCursor(0, 1);
lcd.print("Sensor Data");
// Turn off all LEDs and buzzer in case of error
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
noTone(BUZZER_PIN); // Stop the buzzer
} else {
// Print temperature to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Light Level: ");
Serial.print(lightLevel);
Serial.println(" %");
// Control the LEDs and buzzer based on temperature
if (temperature < 20.0) {
// If temperature is below 20°C, turn on blue LED and play a low-frequency tone
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, HIGH);
tone(BUZZER_PIN, 500); // Play tone at 500 Hz
} else if (temperature >= 20.0 && temperature < 30.0) {
// If temperature is in the normal range (20°C to 30°C), turn on green LED and stop the buzzer
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(BLUE_LED_PIN, LOW);
noTone(BUZZER_PIN); // Stop the buzzer
} else {
// If temperature is above 30°C, turn on red LED and play a high-frequency tone
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
tone(BUZZER_PIN, 1500); // Play tone at 1500 Hz
}
// Display temperature, light level, and humidity on the LCD with better spacing
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.print(temperature, 1); // One decimal place for temperature
lcd.print("C ");
lcd.print("L:");
lcd.print(lightLevel, 0); // No decimal places for light level
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Hum:");
lcd.print(humidity, 1); // One decimal place for humidity
lcd.print("%");
}
}