#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <TimeLib.h>
// Define pin connections
const int soilMoisturePin = A0; // Analog pin for custom soil moisture sensor
const int pumpPin = 7; // Digital pin for water pump (LED in simulation)
const int growLightPin = 8; // Digital pin for grow light relay module
const int DHTPin = 2; // Digital pin for DHT sensor
const int buttonPumpPin = 3; // Digital pin for manual pump control button
const int buttonIncreasePin = 4; // Digital pin for increasing moisture threshold button
const int buttonDecreasePin = 5; // Digital pin for decreasing moisture threshold button
// DHT sensor type
#define DHTTYPE DHT22
DHT dht(DHTPin, DHTTYPE);
// Initialize the 20x4 LCD
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD address to 0x27 for a 20 chars and 4 lines display
// Threshold value for soil moisture
int moistureThreshold = 300;
// Grow light on and off hours
const int lightOnHour = 6; // Turn on grow light at 6 AM
const int lightOffHour = 18; // Turn off grow light at 6 PM
// Button states and debounce time
bool pumpManualControl = false;
unsigned long lastPumpDebounceTime = 0;
unsigned long lastIncreaseDebounceTime = 0;
unsigned long lastDecreaseDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup() {
// Initialize the serial monitor
Serial.begin(9600);
// Set pin modes
pinMode(pumpPin, OUTPUT);
pinMode(growLightPin, OUTPUT);
pinMode(buttonPumpPin, INPUT_PULLUP);
pinMode(buttonIncreasePin, INPUT_PULLUP);
pinMode(buttonDecreasePin, INPUT_PULLUP);
// Initially turn off the pump and grow light
digitalWrite(pumpPin, LOW);
digitalWrite(growLightPin, LOW);
// Initialize the DHT sensor
dht.begin();
// Initialize the 20x4 LCD
lcd.begin(20, 4); // Initialize the LCD with 20 columns and 4 rows
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("System Init...");
delay(2000); // Delay to show initialization message
// Set the initial time (set this to your current time)
setTime(8, 0, 0, 27, 12, 2024); // Example: 8:00 AM, December 27, 2024
}
void loop() {
// Read the value from the soil moisture sensor
int soilMoistureValue = analogRead(soilMoisturePin);
// Read temperature and humidity values from the DHT sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Print the soil moisture, temperature, and humidity values to the serial monitor
Serial.print("Soil Moisture Value: ");
Serial.println(soilMoistureValue);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Display the readings on the 20x4 LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Moisture: ");
lcd.print(soilMoistureValue);
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature, 1); // Limit to 1 decimal place
lcd.print("C Hum: ");
lcd.print(humidity, 1); // Limit to 1 decimal place
lcd.print("%");
lcd.setCursor(0, 2);
lcd.print("Threshold: ");
lcd.print(moistureThreshold);
lcd.setCursor(0, 3);
lcd.print("Pump Manual: ");
lcd.print(pumpManualControl ? "ON" : "OFF");
// Read button states
int buttonPumpState = digitalRead(buttonPumpPin);
int buttonIncreaseState = digitalRead(buttonIncreasePin);
int buttonDecreaseState = digitalRead(buttonDecreasePin);
// Check for manual pump control button press with debounce
if (buttonPumpState == LOW && (millis() - lastPumpDebounceTime) > debounceDelay) {
pumpManualControl = !pumpManualControl;
lastPumpDebounceTime = millis();
}
// Check for increase threshold button press with debounce
if (buttonIncreaseState == LOW && (millis() - lastIncreaseDebounceTime) > debounceDelay) {
moistureThreshold += 10; // Increase threshold by 10
lastIncreaseDebounceTime = millis();
}
// Check for decrease threshold button press with debounce
if (buttonDecreaseState == LOW && (millis() - lastDecreaseDebounceTime) > debounceDelay) {
moistureThreshold -= 10; // Decrease threshold by 10
lastDecreaseDebounceTime = millis();
}
// If soil moisture is below the threshold or manual control is activated, turn on the pump (LED)
if (soilMoistureValue < moistureThreshold || pumpManualControl) {
digitalWrite(pumpPin, HIGH); // Turn on the pump (LED)
Serial.println("Watering the plants...");
} else {
digitalWrite(pumpPin, LOW); // Turn off the pump (LED)
Serial.println("Soil is moist enough.");
}
// Control grow light based on the time of day using relay module
int currentHour = hour();
if (currentHour >= lightOnHour && currentHour < lightOffHour) {
digitalWrite(growLightPin, HIGH); // Activate relay to turn on grow light
Serial.println("Grow light is ON");
} else {
digitalWrite(growLightPin, LOW); // Deactivate relay to turn off grow light
Serial.println("Grow light is OFF");
}
// Wait for 10 seconds before the next reading
delay(10000);
}