#include <DHT.h>
#include <LiquidCrystal_I2C.h>
// Sensor Pins
#define DHTPIN 2 // DHT11 sensor connected to pin 2
#define DHTTYPE DHT11 // DHT 11
#define SOIL_PIN A0 // Soil moisture sensor on A0
#define PH_PIN A1 // pH sensor on A1
#define BUZZER_PIN 10 // Buzzer or LED connected to pin 10
#define RELAY_PIN 8 // Relay for pump/nutrient control
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD setup, I2C address 0x27
void setup() {
Serial.begin(9600); // Start serial communication
dht.begin(); // Initialize the DHT sensor
lcd.begin(16,2); // Initialize the LCD display
lcd.backlight(); // Turn on the backlight
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
lcd.print("Soil Monitor");
delay(1000);
}
void loop() {
// Read moisture value
int soilMoisture = analogRead(SOIL_PIN);
float moisturePercentage = map(soilMoisture, 1023, 0, 0, 100); // Convert to %
// Read pH value
int phValue = analogRead(PH_PIN);
float ph = (phValue * 5.0) / 1023; // Convert to pH (specific calibration needed)
// Read temperature and humidity
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
// Display on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Moisture: ");
lcd.print(moisturePercentage);
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print("C");
// Serial monitor output
Serial.print("Moisture: ");
Serial.print(moisturePercentage);
Serial.print("% pH: ");
Serial.print(ph);
Serial.print(" Temp: ");
Serial.print(temp);
Serial.println("C");
// Alerts and Actions
if (moisturePercentage < 30) {
digitalWrite(BUZZER_PIN, HIGH); // Trigger buzzer/LED if moisture is low
} else {
digitalWrite(BUZZER_PIN, LOW);
}
if (moisturePercentage < 20) {
digitalWrite(RELAY_PIN, HIGH); // Turn on water pump if moisture is critically low
} else {
digitalWrite(RELAY_PIN, LOW);
}
delay(2000); // Wait for 2 seconds before next reading
}