8 Channel Watering System

Hello,
I designed an 8 channel self watering system using AT2560mega. There are 8 capacitive sensors connected to Analog A0 thru A7, and 8 digitalWrites controlling solenoids. I want the pump feeding the solenoids to be on anytime any of the solenoids are on, however the only time the pump energizes is when the last solenoid in the list is energized. I thought maybe setting up the pump loop separately but operating from the same capacitive analog signals, but it seems to be a lot of repetitious code. Any suggestions are welcome.

const int pumpSolenoid[8] = { 28, 30, 32, 34, 36, 44, 46, 48 };
const int moistureSensor[8] = { A0, A1, A2, A3, A4, A5, A6, A7 };
const int pumpRelay[1] = { 50 };
const int ON = LOW;
const int OFF = HIGH;

// Lower = wetter, 220 = completely wet, 513 = completely dry
const int moistureThreshold[8] = { 400, 400, 400, 400, 400, 400, 400, 400 }; // Adjust to each plant's needs

void setup() {
Serial.begin(9600);

for(int i = 0; i < 8; i++) {
pinMode(pumpSolenoid[i], OUTPUT);
digitalWrite(pumpSolenoid[i], OFF);
pinMode(pumpRelay[i], OUTPUT);
digitalWrite(pumpRelay[i], OFF);
pinMode(moistureSensor[i], INPUT);

}

delay(500);
}

void loop() {
int moistureLevel;

for(int i = 0; i < 8; i++) {
moistureLevel = analogRead(moistureSensor[i]);

Serial.print("MOISTURE LEVEL ");
Serial.print(i);
Serial.print(": ");
Serial.println(moistureLevel);

    // If the soil is too dry, turn the pump on. 
// Otherwise, turn the pump off
if(moistureLevel > moistureThreshold[i]) {
  digitalWrite(pumpSolenoid[i], ON), digitalWrite(pumpRelay[i], ON);
} else {
  digitalWrite(pumpSolenoid[i], OFF), digitalWrite(pumpRelay[i], OFF);  }

}

Serial.println();
delay(10000);
}

const int pumpRelay[1] = { 50 };
. . .

for(int i = 0; i < 8; i++) {
. . .
digitalWrite(pumpRelay[i], OFF);

Explain what you think is happening here.


Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

1 Like

I believe the constant integer of the pumpRelay (containing 1) is on digital 50 as an digitalWrite
second not certain of the "for(int i=0;" but the "integer is of the 8 channels or lessor
and the digitalWrite(pumppRelay[i], OFF); is commanding the digital out to be off. That's my best explication.
I have the digital outs connected to 5v 8 channel relay NO on the ground. The pump has single channel 5V relay NO breaking ground. The pump and solenoids are all 12v common parts controlled by relays. The mega is powered thru a buck converter from the 12v to 5v. LED indicator lights are powered through 3.3v arduino and the 8 channel relay.
I will have to draw an updated schematic.

const int pumpRelay[1] = { 50 };

The above is declaring a 1 (one) element array i.e. you will have pumpRelay[0]


for(int i = 0; i < 8; i++) {
. . .
digitalWrite(pumpRelay[i], OFF);

Now you will be ittering thru 0 to 7 inclusive.

What happens when you have:
digitalWrite(pumpRelay[i], OFF); // where i = 1 thru 7 ? :thinking:

To be honest, having "[i]" after the pump relay, I was anticipating that it would be defined by the (pumpRelay) in front of it.
As far as the const int pumpRelay[1] I assume it would have to be indicated as a 1 (one) indicating there is only a single digitalOut where as 0 (zero) would indicate there being none.
Can another letter be an indicator to replace "[i]"

Here you would end up with:
digitalWrite(pumpRelay[0], OFF);
digitalWrite(pumpRelay[1], OFF);
digitalWrite(pumpRelay[2], OFF);
digitalWrite(pumpRelay[3], OFF);
digitalWrite(pumpRelay[4], OFF);
digitalWrite(pumpRelay[5], OFF);
digitalWrite(pumpRelay[6], OFF);
digitalWrite(pumpRelay[7], OFF);


You could just.
digitalWrite(50, OFF);

And there in lies the issue. The pump relay is ONLY on when channel 8 is on. 1 thru 7 pump remains off.
So if I change the code where i=1 thru 8 in the ''for(int i = 0; i < 8; i++) {" //it would read all 8 channels and the ''else'' should turn the pump on?

Confused about your logic :woozy_face: .


Let’s slow down a bit.


Is this what you want ?

// If the soil is too dry, turn the pump on. 
// Otherwise, turn the pump off

if(moistureLevel > moistureThreshold[i]) 
{
  digitalWrite(pumpSolenoid[i], ON);
  digitalWrite(50, ON);
} 
else 
{
  digitalWrite(pumpSolenoid[i], OFF);
  digitalWrite(50, OFF);  
}

Without a schematic, we are just guessing what you have/are_doing.


You are going to have to deal with this:
digitalWrite(pumpRelay[i], OFF);

I believe that is what I want, and using the "50" instead of [i] would eliminate the confusion in reference to the designation of [i].
I will write this into code and give it a try, but I will also rewrite the schematic so it is much clearer.

A lot of the design has changed since the picture that I attached. I initially was breaking positive on the relays, and running the LEDs with 12v and a lot of resistors creating a ton of heat.

BTW

Many new people do not wire a common 8 channel relay board properly. (ddloyd)


BTW
When mounting components in an enclosure like that, they should be mounted on a back panel (ex: aluminum plate) then mount the back panel in the box.

Oh wow, I do not have the separate DC supply to the right! I noticed relays 7 & 8 are sometimes sticky and do not want to close right away. I believe this may cure that issue. Thank you for sharing your knowledge. I guess you can tell, I'm fairly new to Arduino programing, but find it fascinating.

Welcome,

You have only one pump so use const int pumpRelay = 50; then digitalWrite( pumpRelay, ...);

But, your problem is that you are turning off the pump when it should be on

Let's say sensor 1 is dry, so the pump turns on
But sensor 2 is wet so the pump turns off...

That is what your for loop is doing, the state of the pump will always be according to the value of the last sensor

You have to change this logic

if(moistureLevel > moistureThreshold[i]) {

Yes, we need to get this logic nailed down too :droplet:

Might also happen (and other weird things) if there's no flyback diodes installed.

image

Here is an example : t1023272.ino - Wokwi Arduino and ESP32 Simulator

1 Like

I think the OP should also consider to only have 1 (one) solenoid energized at a time so the pump stays effective.

Very interesting that you would say that. Again with channels 7&8, with all solenoids open 7&8 do not have strong water flow until other solenoids close

The program is following what you have described as far as the pump following the last sensor command only. I was attempting to have the pump ON if ANY of the sensors is signaling for on. Is there a command that would energize the pump relay if any of the solenoids are energized?

Look at the Wokwi example in my previous reply :slight_smile:

Maybe you need a more powerful pump and a relief valve