#include <Wire.h> // Include library to communicate with I2C devices
#include <LiquidCrystal_I2C.h> // Include LCD library to control I2C LCD
#include <DHT.h> // Include DHT sensor library
#include <ESP32Servo.h> // Include Servo motor library
// Define DHT sensor pin and type
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
// Initialize LCD (I2C) with address and size
LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD with I2C address 0x27, 20 columns and 4 rows
// Initialize Servo motors
Servo tempServo;
Servo humidityServo;
Servo buttonServo;
// Pin definitions for button, PIR sensor, LED, and buzzer
#define BUTTONPIN 14
#define PIRPIN 13
#define LEDPIN 12
#define BUZZERPIN 27
// Set thresholds for temperature and humidity
const float tempThreshold = 27.0; // Temperature threshold in Celsius
const float humThreshold = 60.0; // Humidity threshold in percentage
void setup() {
Serial.begin(115200); // Initialize serial monitor for debugging
// Initialize LCD screen
lcd.init(); // Set up LCD
lcd.backlight(); // Turn on backlight
lcd.clear(); // Clear screen
lcd.print("Initializing..."); // Display message
delay(2000); // Wait for 2 seconds
// Initialize DHT sensor
dht.begin();
Serial.println("DHT Sensor Initialized");
// Initialize servo motors for controlling devices based on temperature and humidity
tempServo.attach(18); // Temperature control servo
humidityServo.attach(19); // Humidity control servo
buttonServo.attach(23); // Button press control servo
// Set pin modes for inputs and outputs
pinMode(PIRPIN, INPUT); // PIR sensor input pin
pinMode(LEDPIN, OUTPUT); // LED output pin
pinMode(BUTTONPIN, INPUT_PULLUP); // Button input pin (with pull-up resistor)
pinMode(BUZZERPIN, OUTPUT); // Buzzer output pin
delay(2000); // Wait for 2 seconds to stabilize
lcd.clear(); // Clear the screen
}
void loop() {
// Read temperature and humidity from the DHT sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Print values to Serial Monitor for debugging
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Humidity: ");
Serial.println(humidity);
// Check if sensor readings are valid
if (isnan(temperature) || isnan(humidity)) {
lcd.setCursor(0, 0);
lcd.print("Sensor Error! "); // Display error if sensor reading fails
delay(2000); // Wait 2 seconds before retrying
return; // Skip the rest of the loop and try again
}
// Display temperature and humidity on the LCD without clearing the screen
lcd.setCursor(0, 0); // Set cursor to top-left of the screen
lcd.print("Temp: ");
lcd.print(temperature, 1); // Display temperature with 1 decimal place
lcd.print(" C");
lcd.setCursor(0, 1); // Move cursor to the next row
lcd.print("Humidity: ");
lcd.print(humidity, 1); // Display humidity with 1 decimal place
lcd.print(" %");
// Temperature Control
if (temperature >= tempThreshold) {
tempServo.write(90); // Move the servo when temp is above threshold
} else {
tempServo.write(0); // Reset the servo when temp is below threshold
}
// Humidity Control
if (humidity >= humThreshold) {
humidityServo.write(90); // Move the servo when humidity is above threshold
} else {
humidityServo.write(0); // Reset the servo when humidity is below threshold
}
// PIR Motion Detection
if (digitalRead(PIRPIN) == HIGH) {
digitalWrite(LEDPIN, HIGH); // Turn on LED when motion is detected
} else {
digitalWrite(LEDPIN, LOW); // Turn off LED when no motion
}
// Button Press Logic
if (digitalRead(BUTTONPIN) == LOW) { // Button is pressed
buttonServo.write(90); // Move the servo when button is pressed
tone(BUZZERPIN, 1000, 1000); // Activate buzzer for 500ms
delay(500); // Wait before continuing
buttonServo.write(0);
} else {
buttonServo.write(0); // Reset servo when button is not pressed
}
delay(1000); // Delay for stable readings and smooth operation
}