Time-based pump and valve control

Hi everyone,
I am trying to irrigate three plant beds of a recirculation-type hydroponic system using one pump and some valves. A total of six valves are working in this system, where three valves are involved in irrigation and the other three for drainage. As it is a recirculating system, water comes back to the sump tank and is supplied to the plant beds. I prepared a simple time-based Arduino code for this system. But it is malfunctioning sometimes, specifically the irrigation comment for the 1st plant bed is executing twice sometimes instead of once. I attached the code here. Would anyone show me the problem? Thanks in advance.

int relay1 = 3;
int relay2 = 4;
int relay3 = 5;
int relay4 = 6;
int relay5 = 9;
int relay6 = 10;
int relay7 = 11;
int relay8 = 12;

void setup() {
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, HIGH);
  pinMode(relay2, OUTPUT);
  digitalWrite(relay2, HIGH);
  pinMode(relay3, OUTPUT);
  digitalWrite(relay3, HIGH);
  pinMode(relay4, OUTPUT);
  digitalWrite(relay4, HIGH);
  pinMode(relay5, OUTPUT);
  digitalWrite(relay5, HIGH);
  pinMode(relay6, OUTPUT);
  digitalWrite(relay6, HIGH);
  pinMode(relay7, OUTPUT);
  digitalWrite(relay7, HIGH);
  pinMode(relay8, OUTPUT);
  digitalWrite(relay8, HIGH);
  
}

void loop() {
// Irrigation  
  digitalWrite(relay1, LOW);   // pump ON for 1st plant bed
  digitalWrite(relay2, LOW);   // valve ON
  delay (97000);
  digitalWrite(relay1, HIGH);  //OFF
  digitalWrite(relay2, HIGH);
  delay (3000);
  
  digitalWrite(relay1, LOW);   // pump ON for 2nd plant bed
  digitalWrite(relay3, LOW);   // valve ON
  delay (97000);
  digitalWrite(relay1, HIGH);  //OFF
  digitalWrite(relay3, HIGH);
  delay (3000);

  digitalWrite(relay1, LOW);   // pump ON for 3rd plant bed
  digitalWrite(relay4, LOW);   // valve ON
  delay (97000);
  digitalWrite(relay1, HIGH);  //OFF + Irrigation interval
  digitalWrite(relay4, HIGH);
  delay (1800000);

//Drain
  digitalWrite(relay7, LOW);   // valve ON for 3rd plant bed
  delay (720000);
  digitalWrite(relay7, HIGH);  //OFF

  digitalWrite(relay5, LOW);   // valve ON for 1st and 2nd plant bed
  digitalWrite(relay6, LOW);
  delay (720000);
  digitalWrite(relay5, HIGH);  //OFF
  digitalWrite(relay6, HIGH);
  delay (360000);              // Interval
  
}

I don't see anything functionally wrong with your code. So it's down to a hardware malfunction. To help with that, we need a schematic and at least one photo of the installation. Please post.

1 Like

To be clear, I suspect a relay or solenoid voltage transient is bumping the Arduino, causing it to reset and execute your first section over again. If it's always the first section of code, Relay3 is suspect as that's where it turns something off(transient due to magnetic field collapse). What does it power?

1 Like

Read about array[]s and structs, also millis() timing and state machines.

Your code will be much smaller, easier to maintain and more reliable/responsive.

1 Like

@lastchancename While what you say is true, maybe we can focus on getting the problem solved? Yes, the OP's code is crude and could be much, much more simple and streamlined, but it does what is necessary, and I don't see a cause in his code for the symptom described.

2 Likes

Fixed that a bit for ya. :wink:

a7

2 Likes

Hi,
Can you post some images of your project so we can see your component layout?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, component names and pin labels.

What are you powering your project with?
What model Arduino are you using?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Thanks, Camsysca, for your quick response. I'm using an eight-channel Arduino relay, which is connected and powered by Arduino UNO. I attached a picture for your reference.

Hi,
Thanks for the pic, is that coil of black cable the DC cable to the UNO?
If so what voltage is it?

That coil can also pickup noise from the relays and valves switching.

Tom... :smiley: :coffee: :australia: :+1:
PS. Good layout for the signal wires from the UNO the relay module. :+1: :+1: :+1:

1 Like

How is the Uno being powered?

Can Serial.prints be added to the code for troubleshooting? I'd like to see a Serial.print in setup() for starters.

something like

voiding setups()
{
Serial.begin(115200);

lots of other code here

and at the end of setup()

Serial.println("setup() complete"); //this should only print once per power on
}
1 Like

How is the relay module being powered? Not clear.

A full schematic, as @TomGeorge sez hand drawn and photographed probably the easiest and entire adequate.

Power sources and connections are the key thing we need to see all of.

a7

1 Like

The relay is directly powered by the Arduino UNO

OK, that's gotta change. Provide 5 volts to the UNO and the relay boad from a good capacity power supply.

a7

1 Like

I just also noticed your huge delays, which I understand, but you can speed things up a bit for testing.

Well, not a bit. Make the thing go through the sequence way faster. Life too short.

Maybe you already do.

You could

# define K72 5000 // not 720000

and use K72 everywhere you had 720000 until the thing is working good.

Also, not sure it makes important here, but a good habit for large constants is to explicitly make them so, use 720000UL to force the literal to be an unsigned long.

# define K72 5000 // not 720000UL

HTH

a7

1 Like

It is better to power Relay module using an external power source rather than from Arduino. You can refer to below similar diagram to see how to use external power source for relay.


And see more in Arduino - 4-relay channel tutorial

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.