#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h> // Use the ESP32Servo library instead of Servo
// DHT22 sensor setup
#define DHTPIN 14 // Pin where DHT22 is connected (GPIO14)
#define DHTTYPE DHT22 // Define DHT sensor type (DHT22)
// Define pins
#define LED_PIN 13 // Pin for LED
#define SERVO_PIN 12 // Pin for Servo motor control
#define TEMPERATURE_THRESHOLD 5 // Temperature threshold (5°C)
#define HUMIDITY_LOWER_THRESHOLD 85 // Lower bound for RH
#define HUMIDITY_UPPER_THRESHOLD 95 // Upper bound for RH
DHT dht(DHTPIN, DHTTYPE); // Create DHT object for DHT22 sensor
LiquidCrystal_I2C lcd(0x3F, 16, 2); // Adjust the I2C address if necessary
Servo fanServo; // Create Servo object for the fan
unsigned long startTime = 0; // Timer start
bool timerStarted = false; // To check if the timer has started
void setup() {
Serial.begin(9600);
// Initialize DHT22 sensor
dht.begin();
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
fanServo.attach(SERVO_PIN); // Attach the servo to the defined pin using ESP32Servo library
lcd.init();
lcd.backlight();
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Temp & Humidity:");
// Initialize fan (servo) to off
fanServo.write(0); // Set servo to 0° (fan off)
digitalWrite(LED_PIN, LOW); // Turn off the LED
}
void loop() {
delay(2000); // Delay between readings (adjust as needed)
// Read temperature and humidity from DHT22 sensor
float temperature = dht.readTemperature(); // Read temperature in Celsius
float humidity = dht.readHumidity(); // Read humidity in %
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT22 sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
lcd.setCursor(0, 1); // Set cursor to the second line
lcd.print(" "); // Clear the previous motor speed
// Logic for fan (servo) control based on temperature and humidity
if (temperature > TEMPERATURE_THRESHOLD || humidity < HUMIDITY_LOWER_THRESHOLD || humidity > HUMIDITY_UPPER_THRESHOLD) {
// Start timer only when temperature exceeds 5°C or humidity goes out of range
if (!timerStarted) {
startTime = millis(); // Capture the start time
timerStarted = true; // Timer is now running
}
// Calculate time elapsed since temperature exceeded threshold or humidity went out of range
unsigned long elapsedTime = (millis() - startTime) / 1000; // Time in seconds
// Turn on the fan (servo motor) and LED
fanServo.write(180); // Set servo to 180° (fan full speed)
digitalWrite(LED_PIN, HIGH); // Turn on the LED
// Display motor status and elapsed time
lcd.setCursor(0, 1);
lcd.print("Fan ON");
lcd.setCursor(8, 1);
lcd.print("Time: ");
lcd.print(elapsedTime);
lcd.print("s"); // Show seconds
} else {
// Reset timer if temperature is below 5°C and humidity is within range
timerStarted = false;
// Turn off the fan (servo motor) and LED
fanServo.write(0); // Set servo to 0° (fan off)
digitalWrite(LED_PIN, LOW); // Turn off the LED
// Display motor status
lcd.setCursor(0, 1);
lcd.print("Fan OFF ");
lcd.setCursor(8, 1);
lcd.print(" "); // Clear the time display
}
// Display temperature, humidity, and fan speed/strength on the LCD
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temperature);
lcd.print("C H:");
lcd.print(humidity);
lcd.print("%");
// Calculate and display fan strength as a percentage
int servoPosition = fanServo.read(); // Get current servo position
int fanStrength = map(servoPosition, 0, 180, 0, 100); // Map the servo position (0 to 180) to fan strength (0% to 100%)
lcd.setCursor(0, 1);
lcd.print("Fan Strength: ");
lcd.print(fanStrength);
lcd.print("%");
}