#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h> // Include the library for the DHT sensor
// Define constants for the DHT sensor
#define DHTPIN 15 // DHT22 sensor is connected to GPIO 15
#define DHTTYPE DHT22 // DHT22 type
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
const int ledPin = 13; // LED pin
LiquidCrystal_I2C lcd(0x27,12,3);
void setup()
{
Wire.begin(23, 22); // Initialize the I2C communication with SDA pin 23 and SCL pin 22.
Serial.begin(9600); /// Initialize the serial communication at a baud rate of 9600 bits per second.
lcd.init(); // initialize the lcd
lcd.backlight(); // turn on the backlight
pinMode(ledPin, OUTPUT);
dht.begin(); // Initialize the DHT sensor
}
void loop()
{
float temp = dht.readTemperature(); // Read temperature from DHT22
float hum = dht.readHumidity(); // Read humidity from DHT22
int16_t i = analogRead(34);
// Use the ternary operator to determine the moisture status based on the value of 'i'.
// If 'i' is less than 300, set 'msg' to "DRY"; if 'i' is greater than 700, set 'msg' to "WET"; otherwise, set 'msg' to "OK".
String msg = i < 300 ? "DRY" : i > 600 ? "WET" : "OK";
lcd.clear();
lcd.print("Moist:");
lcd.print(i, 1); // Print humidity with one decimal;
lcd.print(": ");
lcd.print(msg);
if(i<=300){
digitalWrite(ledPin, HIGH); // will turn on the LED(motor) if the soil is dry (i<300)
}
else {
digitalWrite(ledPin, LOW);
}
lcd.setCursor(0, 1); // Set cursor to the first column and second row
lcd.print("T:"); // Print "T:" (Temperature)
lcd.print(temp); // Print temperature with one decimal
lcd.print(" H:"); // Print " H:" (Humidity)
lcd.print(hum); // Print humidity with one decimal;
delay(500);
}