#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 2 // DHT22 data pin connected to Arduino digital pin 2
#define DHTTYPE DHT22 // DHT22 sensor type
#define RELAY_PIN 8 // Digital pin connected to the relay module
DHT dht(DHTPIN, DHTTYPE);
const int trigPin = 9; // Ultrasonic sensor trigger pin
const int echoPin = 10; // Ultrasonic sensor echo pin
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns and 2 rows
const float temperatureThreshold = 50.0; // Set your temperature threshold in Celsius
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
dht.begin();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
// Measure temperature and humidity using DHT22
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Display temperature and humidity on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print(" %");
delay(2000); // Wait for a moment
// Measure water level using ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = (duration * 0.0343) / 2;
// Display water level on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Water Level:");
lcd.setCursor(0, 1);
lcd.print(distance);
lcd.print(" cm");
delay(2000); // Wait for a moment
// Check if temperature exceeds the threshold
if (temperature > temperatureThreshold) {
// If temperature is above the threshold, turn off the relay
digitalWrite(RELAY_PIN, LOW);
Serial.println("Turning off the system.");
} else {
// If temperature is below the threshold, keep the relay on
digitalWrite(RELAY_PIN, HIGH);
Serial.println("System is running.");
}
delay(2000);
}