This circuit is designed to control a plastic solenoid valve and a 5V mini water pump based on readings from a YL-69 humidity sensor. The system operates by checking the soil moisture level every 24 hours and activates the valve and pump if the moisture level exceeds a specified threshold. The Arduino UNO serves as the central controller, reading the sensor data and managing the power to the pump and valve accordingly.
/*
* This Arduino sketch controls a valve and a water pump based on
* readings from a humidity sensor. The valve and water pump are activated
* when the analog value from the humidity sensor exceeds a specified threshold. The
* system checks the moisture level every 24 hours and activates the valve
* and water pump accordingly.
*/
// Declaration of constants and pins
const int humidityDigitalPin = 2; // Digital output from the humidity sensor
const int humidityAnalogPin = A0; // Analog output from the humidity sensor
const int valvePin = 10; // Control pin for the valve
const int pumpPin = 9; // Control pin for the pump
const int threshold = 600; // Moisture threshold (0-1023), adjust based on soil
unsigned long wateringInterval = 86400000UL; // Watering interval: 24 hours (in ms)
unsigned long wateringDuration = 60000UL; // Pump and valve activation duration: 1 minute (in ms)
unsigned long previousMillis = 0; // Time of the last measurement
unsigned long pumpStartMillis = 0; // Pump start time
bool isWatering = false; // Indicates whether watering is in progress
void setup() {
pinMode(valvePin, OUTPUT); // Set the valve pin as an output
pinMode(pumpPin, OUTPUT); // Set the pump pin as an output
pinMode(humidityDigitalPin, INPUT); // Set the digital sensor pin
pinMode(humidityAnalogPin, INPUT); // Set the analog sensor pin
digitalWrite(valvePin, LOW); // Valve closed by default
digitalWrite(pumpPin, LOW); // Pump off by default
Serial.begin(9600); // For displaying information on the serial monitor
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time
// Check every 24 hours
if (currentMillis - previousMillis >= wateringInterval) {
previousMillis = currentMillis; // Update the time of the last measurement
int soilMoistureValue = analogRead(humidityAnalogPin); // Read the soil moisture
Serial.print("Soil Moisture: ");
Serial.println(soilMoistureValue);
if (soilMoistureValue > threshold) {
// Moisture is above the threshold, start watering
Serial.println("Low moisture, activating pump and valve!");
digitalWrite(valvePin, HIGH); // Open the valve
digitalWrite(pumpPin, HIGH); // Activate the pump
isWatering = true; // Mark that watering has started
pumpStartMillis = currentMillis; // Record the pump start time
}
}
// Check if watering is in progress and needs to stop
if (isWatering) {
if (currentMillis - pumpStartMillis >= wateringDuration) {
// Watering duration is over
Serial.println("Watering complete, stopping pump and valve.");
digitalWrite(valvePin, LOW); // Close the valve
digitalWrite(pumpPin, LOW); // Turn off the pump
isWatering = false; // Watering is finished
}
}
// You can perform other tasks here while watering is paused
// For example, you can check other sensors, manage an LCD display, etc.
}
This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes the pins connected to the humidity sensor, water pump, and solenoid valve, and sets up a serial communication for debugging purposes. The main loop checks the soil moisture level every 24 hours and activates the water pump and valve for a minute if the soil is too dry.