Automatic Plant Watering System

Hello everyone. I have been following the automatic watering plant system described here: https://www.instructables.com/id/Easiest-Arduino-Smart-Plant-Watering/.

I have assembled everything and it seems to be working. However, after watering a dry plant for a given period, the pump goes off (that is fine) and the water goes back by gravity through the tubing. That should be fine. However, my solenoid valve lets the water flow back from the openings you see in the following picture:

I am not sure now if there is a problem in the code concerning the valve control or if I should close those openings.

My code is here:

    // set all moisture sensors PIN ID
    int moisture1 = A0;
    int moisture2 = A1;
    int moisture3 = A2;
    int moisture4 = A3;

    // set water relays
    int relay1 = 3;
    int relay2 = 4;
    int relay3 = 5;
    int relay4 = 6;

    //init valve nr
    int x = 0;

    // set water pump
    int pump = 2;

    //Water correct plant function
    void waterPlant(int x) {
    Serial.print("Opening: ");
    Serial.print(x);

    //Open valve x ==> x will contain value of correct relay[1-4]
    digitalWrite(x, HIGH);

    //We wait 0.5 seconds before opening pump so we are sure valve is open. This will prevent the pump from pushing water to a closed valve and risking the tubes to leak or wose, break loose.
    delay(500);

    //Open pump for 3 seconds
    digitalWrite(pump, HIGH);
    delay(5000);

    //close pump
    digitalWrite(pump, LOW);
    delay(500);
    digitalWrite(x, LOW);

    //Make sure all valves are closed again.
    closeAll();
    }
    
    void setup() {
    // declare relay as output
    pinMode(relay1, OUTPUT);
    pinMode(relay2, OUTPUT);
    pinMode(relay3, OUTPUT);
    pinMode(relay4, OUTPUT);

    // declare pump as output
    pinMode(pump, OUTPUT);

    // declare the ledPin as an OUTPUT:
    Serial.begin(9600);
    }
    
    void loop() {
    //print sensor values.
    Serial.print(F("Sensor 1: "));
    Serial.println(analogRead(moisture1));
    Serial.print(F("Sensor 2: "));
    Serial.println(analogRead(moisture2));
    Serial.print(F("Sensor 3: "));
    Serial.println(analogRead(moisture3));
    Serial.print(F("Sensor 4: "));
    Serial.println(analogRead(moisture4));
    Serial.println("Check moisture");

    //Go to moisture checkup function to se if plants need watering
    checkMoisture();

    // wait 8 hours and repeat the process
    Serial.print("wait 2 hours \n ++++++++++++++ END +++++++++++++ \n");
    delay(7200000);
    }

    // check which plant need water
    // and open the switch for that specific plant
    void checkMoisture() {
    if (analogRead(moisture1) <= 550) {
    waterPlant(relay1);
    }
    if (analogRead(moisture2) <= 1000) {
    waterPlant(relay2);
    }
    if (analogRead(moisture3) <= 550) {
    waterPlant(relay3);
    }
    if (analogRead(moisture4) <= 550) {
    waterPlant(relay4);
    }
    delay(100);
    if (analogRead(moisture1) <= 450 || analogRead(moisture2) <= 450 || analogRead(moisture3) <= 450 || analogRead(moisture4) <= 450) {
     checkMoisture();
     }
    else {
    closeAll();
    }
    }

    // turn pump & valves off just to be sure.
    void closeAll() {
    Serial.println("closing pump + valves!");
    digitalWrite(pump, LOW);
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);
    }

Thank you very much for your help.

I do not fully understand your question.

water flows when the pump is running
an energized valve allows water to flow to the plant
when the pump is shut off and the valve is shut off, water drains out of the lines....
I assume some goes back to the pump, some goes to the plant...... but that does not seem to work as there is no way for air to get into the line unless the valve has a 3rd opening. but that would let water flow when the valve is not energized and the pump was running.

it looks like your valve is what we call a 3-way valve.
or rather, 4 independent 3-way valves.

Each would have a common water supply. (pipe on the side)
and two paths for water to flow. (top and towards the camera)
one passes water is when there is no power on the valve , called normally open
the other passes water is when there is power on the valve. called normally closed.

The common line is clear as it is common to all valves as the supply.

Please post a link to that valve so we can figure out the use.

it is possible that one of the pipes is capped so it only allows water when the solenoid is energized.

I did find a similar valve that has the front ports blocked by a mounting plate. I assume the tubing connections were plugged and the mounting plate held the plug in place so that valve acted as a simple on/off valve.

more descriptions of what is happening is needed.

If writing LOW to the relay really does turn off the valve then there is nothing wrong with that portion of the code. Note that relay modules are often active LOW, ie they turn on when the input to them goes LOW. It would help considerably if the pin names reflected their purpose, ie solenoid1 instead of relay1

Also, although it does not matter to the program it always irks me when comments don't match the code. It is impossible to tell whether the code or comment is wrong.

  //Open pump for 3 seconds
  digitalWrite(pump, HIGH);
  delay(5000);

I think I maybe figured out what the issue was. There is a time delay of 0.5 sec between the closing of the relay and the pump shut down. Because of that, there is time for the liquid to come back through the opening.

They look like three way valves to me - that is when energised the water can flow one way and de energised it flows through the other port to direct the flow in another direction . If you google the make and model it should tell you . They may also only work properly with the flow in a particular direction . You can block off the third port.