#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#define DHT_PIN 4 // Connect DHT22 to ESP32 GPIO 4
#define LDR_PIN 34 // Connect LDR to ESP32 GPIO 34
#define YELLOW_LED_PIN 32 // Connect Yellow LED to ESP32 GPIO 32
#define RELAY_PIN 16 // Connect Relay to ESP32 GPIO 16
#define SERVO_PIN 23 // Connect Servo to ESP32 GPIO 23
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_ROWS 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_ROWS);
DHT dht(DHT_PIN, DHT22);
Servo myservo;
void setup() {
Serial.begin(115200);
lcd.begin(LCD_COLUMNS, LCD_ROWS);
dht.begin();
myservo.attach(SERVO_PIN);
pinMode(YELLOW_LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int ldrValue = analogRead(LDR_PIN);
// Display temperature and humidity on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(temperature) + "C");
lcd.setCursor(0, 1);
lcd.print("Humidity: " + String(humidity) + "%");
// Check temperature conditions for the fan (servo)
if (temperature > 40) {
myservo.write(360); // Turn on the fan
} else {
myservo.write(0); // Turn off the fan
}
// Check light conditions for the yellow LED
if (ldrValue > 1000) {
digitalWrite(YELLOW_LED_PIN, HIGH); // Turn on the yellow LED
} else {
digitalWrite(YELLOW_LED_PIN, LOW); // Turn off the yellow LED
}
// Check humidity conditions for the relay and red LED
if (humidity > 50 || humidity < 20) {
digitalWrite(RELAY_PIN, HIGH); // Turn on the relay
} else {
digitalWrite(RELAY_PIN, LOW); // Turn off the relay
}
delay(1000); // Delay for stability
}