#include <Servo.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
// Setup for DHT sensor
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Setup for LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Setup for servo motor
Servo servo;
void setup() {
// Initialize DHT sensor
dht.begin();
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize servo
servo.attach(9);
// Setup pin modes
pinMode(13, OUTPUT); // Pin for the status LED
pinMode(2, INPUT); // Pin for the infrared sensor
// Start serial communication
Serial.begin(9600);
}
void loop() {
// Reading from the DHT sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Displaying temperature and humidity on the LCD
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(humidity, 1);
lcd.print(" %");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature, 1);
lcd.print(" Cel");
// Reading from the infrared sensor
int infraredState = digitalRead(2);
// Logic for the infrared sensor and servo motor
if (infraredState == HIGH) {
digitalWrite(13, HIGH); // Turn on the LED
Serial.println("Presence: Yes"); // Presence detected
servo.write(90); // Move servo to 90 degrees
} else {
digitalWrite(13, LOW); // Turn off the LED
Serial.println("Presence: No"); // No presence detected
servo.write(0); // Return servo to 0 degrees
}
// Delay before the next reading
delay(10000); // 10 seconds delay
}