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.
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?
@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.
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, 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.
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
}
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.
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.