#include <DHT.h>
// Define sensor pins
#define SOIL_MOISTURE_PIN A0 // Soil moisture sensor analog pin
#define LDR_PIN A1 // LDR analog pin
#define DHT_PIN 2 // DHT22 data pin
#define BUZZER_PIN 3 // Buzzer digital pin
// Define LED pins
#define RED_LED_PIN 5 // Red LED for low moisture
#define YELLOW_LED_PIN 6 // Yellow LED for moderate moisture
#define GREEN_LED_PIN 7 // Green LED for high moisture
// Initialize DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHT_PIN, DHTTYPE);
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Initialize DHT sensor
dht.begin();
// Initialize LED and buzzer pins
pinMode(RED_LED_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
// Read soil moisture sensor data
int soilMoisture = analogRead(SOIL_MOISTURE_PIN);
// Read light sensor (LDR) data
int lightLevel = analogRead(LDR_PIN);
// Read temperature and humidity from DHT22 sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Display sensor readings on Serial Monitor
Serial.print("Soil Moisture: ");
Serial.print(soilMoisture);
Serial.print("\tLight Level: ");
Serial.print(lightLevel);
Serial.print("\tTemperature: ");
Serial.print(temperature);
Serial.print("°C\tHumidity: ");
Serial.print(humidity);
Serial.println("%");
// Detect day or night using LDR
bool isDaytime = lightLevel > 300; // Example threshold for detecting daytime
// Adjust soil moisture threshold based on temperature
bool isSoilMoistureGood;
if (temperature > 30) { // Adjust threshold in high temperature
isSoilMoistureGood = soilMoisture > 400;
} else {
isSoilMoistureGood = soilMoisture > 300; // Normal threshold
}
// Determine healthy temperature and humidity ranges
bool isTemperatureGood = temperature >= 18 && temperature <= 25;
bool isHumidityGood = humidity >= 40 && humidity <= 70;
// Soil moisture range conditions with LED alerts
if (soilMoisture < 300) { // Low moisture (dry soil)
digitalWrite(RED_LED_PIN, HIGH); // Turn on Red LED
digitalWrite(YELLOW_LED_PIN, LOW); // Turn off Yellow LED
digitalWrite(GREEN_LED_PIN, LOW); // Turn off Green LED
Serial.println("Soil is dry! (Low moisture)");
} else if (soilMoisture >= 300 && soilMoisture <= 700) { // Moderate moisture
digitalWrite(RED_LED_PIN, LOW); // Turn off Red LED
digitalWrite(YELLOW_LED_PIN, HIGH);// Turn on Yellow LED
digitalWrite(GREEN_LED_PIN, LOW); // Turn off Green LED
Serial.println("Soil is moist! (Moderate moisture)");
} else { // High moisture (well-watered)
digitalWrite(RED_LED_PIN, LOW); // Turn off Red LED
digitalWrite(YELLOW_LED_PIN, LOW); // Turn off Yellow LED
digitalWrite(GREEN_LED_PIN, HIGH); // Turn on Green LED
Serial.println("Soil is well-watered! (High moisture)");
}
// Check overall plant health status
if (isSoilMoistureGood && isTemperatureGood && isHumidityGood) {
Serial.println("The plant environment is good and healthy.");
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer if conditions are good
} else {
Serial.println("Warning: The plant environment needs attention!");
// Environment-specific alerts
if (!isSoilMoistureGood && humidity < 40) {
Serial.println("Soil moisture is low and humidity is low; consider watering.");
digitalWrite(BUZZER_PIN, HIGH); // Buzzer alert for low moisture and humidity
} else if (!isSoilMoistureGood) {
Serial.println("Soil is dry, but humidity is high; monitor closely.");
digitalWrite(BUZZER_PIN, LOW); // No buzzer alert if only moisture is low but humidity is high
}
if (!isTemperatureGood) {
Serial.println("Temperature is outside the healthy range!");
if (isDaytime) digitalWrite(BUZZER_PIN, HIGH); // Only alert during daytime
}
if (!isHumidityGood) {
Serial.println("Humidity is outside the healthy range!");
if (isDaytime) digitalWrite(BUZZER_PIN, HIGH); // Only alert during daytime
}
}
// Heat wave alert
if (temperature > 35) {
Serial.println("Heat wave alert! Temperature is extremely high.");
digitalWrite(BUZZER_PIN, HIGH); // Buzzer alert for heat wave
}
delay(2000); // Wait 2 seconds before the next reading
}