This circuit is designed to automate a watering system using an Arduino Nano as the central controller. The system monitors soil moisture levels using multiple DFRobot Capacitive Soil Moisture Sensors and controls water pumps to irrigate plants as needed. A water level float switch sensor is used to detect the presence of water in a reservoir, and a relay module is employed to control the water pumps. The system also includes a red LED indicator for signaling the water level status and a DIP switch for manual control over the watering process. The power supply is managed through a solar panel and a charge controller, with an 18650 battery holder for energy storage. A USB connection is provided for alternative power input.
// Definition of pins
const int soilMoistureSensors[4] = {A0, A1, A2, A3}; // Soil moisture sensor pins
const int relays[4] = {2, 3, 4, 5}; // Relay control pins
const int dipSwitch[4] = {8, 9, 10, 11}; // DIP switch pins for plant activation/deactivation
const int floatSensorPin = 6; // Float switch sensor pin
const int waterLackLed = 7; // Water lack LED indicator pin
// Moisture threshold below which the pump is activated
const int moistureThreshold = 400;
// Control variables
unsigned long lastIrrigationTime[4]; // Time in milliseconds of the last irrigation for each plant
const unsigned long interval24h = 86400000; // 24 hours in milliseconds
void setup() {
// Pin initialization
for (int i = 0; i < 4; i++) {
pinMode(relays[i], OUTPUT);
pinMode(dipSwitch[i], INPUT_PULLUP); // Activate internal pull-up resistor
digitalWrite(relays[i], LOW); // Initially, the pumps are off
lastIrrigationTime[i] = 0; // Initialize irrigation times to 0
}
pinMode(floatSensorPin, INPUT);
pinMode(waterLackLed, OUTPUT);
digitalWrite(waterLackLed, LOW); // Initially, the LED is off
}
void loop() {
bool isReservoirFull = digitalRead(floatSensorPin);
bool isPumpOn = false; // Variable to check if a pump is already on
if (!isReservoirFull) {
// If the reservoir is empty, turn on the LED and do not activate the pumps
digitalWrite(waterLackLed, HIGH);
for (int i = 0; i < 4; i++) {
digitalWrite(relays[i], LOW); // Turn off all pumps
}
} else {
// If the reservoir is full, turn off the LED and check the plants
digitalWrite(waterLackLed, LOW);
unsigned long currentTime = millis(); // Get the current time
// Loop to check each plant
for (int i = 0; i < 4; i++) {
// If the DIP switch is active, no pump is on, and 24 hours have passed since the last irrigation
if (!isPumpOn && digitalRead(dipSwitch[i]) == LOW && (currentTime - lastIrrigationTime[i]) >= interval24h) {
int moisture = analogRead(soilMoistureSensors[i]);
if (moisture < moistureThreshold) {
digitalWrite(relays[i], HIGH); // Turn on the pump
isPumpOn = true; // A pump is now on
lastIrrigationTime[i] = currentTime; // Update the time of the last irrigation
} else {
digitalWrite(relays[i], LOW); // Turn off the pump if the moisture is sufficient
}
} else {
digitalWrite(relays[i], LOW); // Turn off all other pumps
}
}
}
delay(1000); // Delay to avoid too frequent readings
}
This code is designed to run on the Arduino Nano and controls the automated watering system. It reads the soil moisture levels and the water level in the reservoir, and based on these readings, it decides whether to activate the water pumps. The DIP switches provide manual control over the system, allowing individual pumps to be turned on or off. The LED indicator is used to signal when the water level in the reservoir is low.