#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <DHT.h>
Servo myservo;
// Define LCD object for LCD1
LiquidCrystal_I2C lcd1(0x27, 16, 2); // I2C address 0x27, 16 columns, 2 rows
// Define DHT22 sensor
#define DHT_PIN 2
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
// Define servo and LED pins
#define SERVO_PIN 4
#define RED_LED_PIN 8
#define YELLOW_LED_PIN 5
#define RELAY_PIN 3
// Define variables for user input
int greenButtonState = 0;
int redButtonState = 0;
void setup() {
// Initialize LCD1
lcd1.begin(16, 2);
// Initialize DHT sensor
dht.begin();
// Attach servo to its pin
myservo.attach(SERVO_PIN);
// Set LED pins as outputs
pinMode(RED_LED_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
// Set relay pin as an output
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
// Read temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Display temperature and humidity on LCD1
lcd1.clear();
lcd1.print("Temp: " + String(temperature) + "C");
lcd1.setCursor(0, 1);
lcd1.print("Humidity: " + String(humidity) + "%");
// Check temperature conditions for the fan (servo)
if (temperature > 40) {
myservo.write(90); // Turn on the fan
} else {
myservo.write(0); // Turn off the fan
}
// Check humidity conditions for the relay
if (humidity > 50 || humidity < 20) {
digitalWrite(RELAY_PIN, HIGH); // Turn on the relay
digitalWrite(RED_LED_PIN, HIGH); // Turn on the red LED
} else {
digitalWrite(RELAY_PIN, LOW); // Turn off the relay
digitalWrite(RED_LED_PIN, LOW); // Turn off the red LED
}
// Check light conditions for the yellow LED
int ldrValue = analogRead(A0);
if (ldrValue > 300) {
digitalWrite(YELLOW_LED_PIN, HIGH); // Turn on the yellow LED
} else {
digitalWrite(YELLOW_LED_PIN, LOW); // Turn off the yellow LED
}
// Other actions...
delay(1000); // Delay for stability
}