This circuit is an automated irrigation system that uses an Arduino UNO to control a water pump based on soil moisture levels. The system includes a capacitive soil moisture sensor, a relay module, a 5V mini water pump, an LCD screen for displaying information, and a 7.4V power source. The Arduino reads the soil moisture level and activates the water pump via the relay module when the soil is dry. The LCD screen displays the current moisture level and the irrigation status.
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Include the I2C LCD library
// Set the LCD address to 0x27 for a 16 chars and 2-line display
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize with I2C address
// Pin configurations
const int moistureSensorPin = A0; // Soil moisture sensor connected to A0 (analog pin)
const int relayPin = 4; // GPIO4 (D2 on NodeMCU) to control water pump
// Thresholds for irrigation control
const int dryThreshold = 500; // Value indicating dry soil
const int wetThreshold = 300; // Value indicating adequately moist soil
// Timing variables
unsigned long irrigationStartTime = 0; // To store when the irrigation started
bool irrigating = false; // To track if irrigation is in progress
const unsigned long irrigationDuration = 9200; // Duration of irrigation in milliseconds (9.20 seconds)
void setup() {
Serial.begin(9600); // Start serial communication for debugging
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.backlight(); // Turn on the LCD backlight
lcd.print("Irrigation"); // Display initial message
delay(2000); // Wait for 2 seconds
lcd.clear(); // Clear the display
pinMode(relayPin, OUTPUT); // Set relay pin as output
digitalWrite(relayPin, HIGH); // Ensure pump is off at startup (set relay to HIGH to turn off)
}
void loop() {
int moistureValue = analogRead(moistureSensorPin); // Read moisture level
// Display moisture value on LCD
lcd.setCursor(0, 0);
lcd.print("Moisture: ");
lcd.print(moistureValue); // Print the moisture value
lcd.print(" "); // Clear any trailing characters
// Control irrigation system based on moisture value
if (moistureValue > dryThreshold && !irrigating) {
// Start irrigation if soil is dry and not already irrigating
digitalWrite(relayPin, LOW); // Activate relay to turn on pump (active-low)
lcd.setCursor(0, 1);
lcd.print("Irrigating.. "); // Display irrigating message
irrigating = true; // Set irrigating flag
irrigationStartTime = millis(); // Record the time irrigation started
}
if (irrigating) {
// Check if the irrigation has been running for 9.2 seconds
if (millis() - irrigationStartTime >= irrigationDuration) {
digitalWrite(relayPin, HIGH); // Deactivate relay to turn off pump (active-low)
lcd.setCursor(0, 1);
lcd.print("Irrigation off "); // Display irrigation off message
irrigating = false; // Reset irrigating flag to allow next cycle if needed
}
}
Serial.print("Moisture Value: "); // Debug output to serial monitor
Serial.println(moistureValue); // Print moisture value to serial monitor
delay(1000); // Wait for 1 second before the next check
}
This code initializes the LCD screen and sets up the pins for the soil moisture sensor and relay module. It reads the soil moisture level and controls the water pump based on the moisture value. The LCD screen displays the current moisture level and the irrigation status. The system irrigates the soil for 9.2 seconds when the soil is dry and then turns off the pump.