#include <LiquidCrystal.h>
#include "DHT.h"
// Initialize the LCD with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Define the DHT sensor and its pin
#define DHTPIN 7 // Pin which is connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud
lcd.begin(16, 2); // Initialize the LCD
lcd.clear();
pinMode(6, OUTPUT); // Set pin 6 as an output for the water pump
digitalWrite(2, HIGH);
delay(1000); // Wait for a second
// Initialize DHT sensor
dht.begin();
// Initial message on LCD
lcd.setCursor(0, 0);
lcd.print("IRRIGATION");
lcd.setCursor(0, 1);
lcd.print("SYSTEM IS ON ");
// Print initial message to Serial Monitor
Serial.println("IRRIGATION SYSTEM IS ON");
delay(3000);
lcd.clear();
}
void loop() {
int value = analogRead(A0); // Read the analog value from the water sensor
Serial.print("Water sensor value: "); // Print the value to the Serial Monitor
Serial.println(value);
lcd.setCursor(0, 0);
// Update water pump status based on water sensor value
if (value < 300) { // Adjust the threshold value according to your sensor readings
digitalWrite(6, HIGH); // Turn the pump on
lcd.print("Water Pump is ON ");
Serial.println("Water Pump is ON");
} else {
digitalWrite(6, LOW); // Turn the pump off
lcd.print("Water Pump is OFF");
Serial.println("Water Pump is OFF");
}
// Update water level status on the LCD and Serial Monitor
lcd.setCursor(0, 1);
if (value < 300) {
lcd.print("Water Level: LOW ");
Serial.println("Water Level: LOW");
} else if (value >= 300 && value < 700) { // Mid-level range
lcd.print("Water Level: MID ");
Serial.println("Water Level: MID");
} else {
lcd.print("Water Level: HIGH");
Serial.println("Water Level: HIGH");
}
delay(2500); // Display the water pump and level status for 3 seconds
// Read temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print temperature and humidity to Serial Monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
// Display temperature and humidity on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print(" %");
delay(3000); // Display the temperature and humidity for 3 seconds
}