#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
const int moisturePin = A0; // Analog pin connected to the soil moisture sensor
// Define the calibration values
const int dryValue = 1023; // Raw sensor reading when soil is dry
const int wetValue = 300; // Raw sensor reading when soil is saturated
// Set the LCD address (0x3F for some I2C backpacks)
const int lcdAddress = 0x27;
// Create an instance of the LiquidCrystal_I2C class
LiquidCrystal_I2C lcd(lcdAddress, 20, 4); // 16 columns, 2 rows
// DHT22 sensor setup
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud
// Initialize the LCD
lcd.init();
// Turn on the backlight (if available on your LCD)
lcd.backlight(); // This line turns on the backlight
// Clear the LCD screen
lcd.clear();
// Text settings
lcd.setCursor(0, 0);
lcd.print("Moisture: "); // Change to "Moisture: " with a space after the colon
lcd.setCursor(0, 1);
lcd.print("Temp: "); // Change to "Temp: " with a space after the colon
lcd.setCursor(0, 3);
lcd.print("HUMID: ");
// Initialize DHT sensor
dht.begin();
}
void loop() {
int moistureValue = analogRead(moisturePin); // Read the raw analog value from the sensor
// Calculate the moisture level in percentage
float moisturePercentage = map(moistureValue, dryValue, wetValue, 0, 100.0);
// Display moisture percentage on LCD on the same line
lcd.setCursor(9, 0); // Set the cursor to the same row at column 9 (after "Moisture: ")
lcd.print(moisturePercentage, 1); // Print the moisture level in percentage with 1 decimal place
// Read DHT11 sensor values
float h = dht.readHumidity();
float temperature = dht.readTemperature();
// Display temperature on LCD on the same line
lcd.setCursor(5, 1); // Set the cursor to the second row at column 5 (after "Temp: ")
lcd.print(temperature, 1); // Print the temperature with 1 decimal place
lcd.setCursor(0,3);
lcd.print("HUMID: ");lcd.print(h);
// Print temperature to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("°C");
delay(2000);
Serial.print("Humidity: ");
Serial.print(h,1);
Serial.println("h");
delay(5000); // Delay for one second before taking another reading
}