watering plants or not

This my first attempt at putting a sketch together and building the Arduino system. Mostly got hints off the internet and the Arduino site.
I,m having problems with my automatic watering system in my greenhouse. Its supplied by a 12volt car battery which is charged by solar power.

It’s a mega 2560 with a 8 relay board (with a separate power supply from the battery) , the relays work a pump and 6 water solenoids.

What I want is the pump to come on and then the relays open a set time then pump stops and relays close. Should be simple ?

The 6 moisture sensors are the usual cheap ones each with a inline module board with leds . They are powered from the mega 5 volt. Is 6 sensors too much for the mega ?

Because I would like to use a LCD shield the analogue pins A8 to A13 are used for the sensors , similarly the digital pins are 30 to 42 for the relays

On my bench when I have it running it works reasonably well but when at first boot up all the relays (energise) open. They close one by one as the sketch runs. This can lead to the plants being flooded

Then it seems to forget what its supposed to do then after a while reports sensors are wet when they are bone dry, it some times gives itself a shake then starts working nearly the way it I think it should .

I thought that the mega was faulty so got another but it’s the same so I think it points to the code.
OR it could be the sensors acting up but they report 1024 as the sensor resistance which they should when dry.
Any advice would be welcome as it’s a month or two since I restarted this from last year.

Code here , This is a shortened version as its meant to water 6 plants

water sketch.doc (24.5 KB)

Please give a link to the products, if possible a link to where you bought it.
For example the soil moisture sensors. The cheap ones that everyone uses don't work very well. The capacitive sensors do work.
Which LCD shield do you use ?

Show a sketch in your post between code tags:

// Mymosture sketch. Mega 2560 ver

const int relayPin1 = 30;// d pin 30
const int relayPin2 = 32; //d pin 32
const int pumpPin9 = 46;  // pump switch/relay

int thresholdMoist = 900;

int sensor_pin1 = A8;
int sensor_pin2 = A9;

int output_value1;
int output_value2;

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

  digitalWrite(relayPin1, HIGH);
  pinMode(relayPin1, OUTPUT);
  digitalWrite(relayPin2, HIGH);
  pinMode(relayPin2, OUTPUT);
  
  digitalWrite(pumpPin9, HIGH);
  pinMode(pumpPin9, OUTPUT);
}

void loop() {

  // sensor1
  output_value1 = analogRead(sensor_pin1);
  output_value1 = map(output_value1, 1024, 0,0,100);
   Serial.print("Plant 1 ");
  Serial.print(output_value1);
  Serial.println("% wet");
 
  if (analogRead (output_value1) > (thresholdMoist))
  {
    digitalWrite(relayPin1, LOW); //opens the water valve if the moist sensor detects dryness ie 900-1023.
    delay(500);
    digitalWrite(pumpPin9, LOW);//turn on the pump after valve is open
    Serial.println("Watering 1");
    delay(5000); //The valve/relay will be on for as many miliseconds as in the brackets.
  }
  if (analogRead (output_value1) < (thresholdMoist))
  {
     Serial.println("Plant 1 OK ");
    digitalWrite(pumpPin9, HIGH); //turns pump off before valve shuts
    delay(500);
    digitalWrite(relayPin1, HIGH); // shuts off the valve.
    delay(2000);
  }

  //sensor2
  output_value2 = analogRead(sensor_pin2);
  output_value2 = map(output_value2, 1024, 0, 0,100);
  Serial.print("Plant 2 ");
  Serial.print(output_value2);
  Serial.println("% wet");
 
  if (analogRead(output_value2) < (thresholdMoist))
  {
    digitalWrite(relayPin2, LOW);
    delay(500);
    digitalWrite(pumpPin9, LOW);
    Serial.println("Watering 2");
    delay(5000);
  }
  if (analogRead(output_value2) > (thresholdMoist))
  {
    
    Serial.println("Plant 2 OK ");
    digitalWrite(pumpPin9, HIGH);
    delay(500);
    digitalWrite(relayPin2, HIGH); 
    delay(2000);
  }
}

Could you use better names for the variables ?
For example "output_value1" could be "the_soil_moisture_in_percentage_number_one" or "moisture1".
Your 'thresholdMoist' is 900, that is not a percentage !
It is not allowed to get the value of an analog pin and use that value as a pin number. That is not a pin number.
You have to make the data flow smoother. Read the analog pin just once. Calculate the percentage. After that use only the percentage in your sketch.
Then you can test if that percentage is for example below 20% and you need to turn on the pump.

If a sketch has a number of pieces of code that are the same in functionality, then you might make a function for that code or use an array (or both).

Hi,
First thing: exactly what relay board?

MOST relay boards turn ON when the signal from the Arduino is LOW. There are reasons for that... See THIS PAGE and scroll down to THIS PART

---------------------( COPY )---------------------
IMPORTANT NOTE: There is a issue with start-up of Arduino programs that control these relays. All of these 2,4, or 8 relay boards input controls are Active LOW, meaning that setting a pin LOW turns them ON. To assure that no relays activate at Reset or Power-On until you want them to, the initialization sequence in SETUP should be:

digitalWrite(Relay, HIGH);
pinMode(Relay, OUTPUT);
This design is intentional, so that it is possible to guarantee that at power-on of a system, or system reset, that no relays activate except when expected under program control. There may be pumps, lights etc attached and chaos could ensue if this was not controlled definitively for each output port being used. Then in the main Loop section turn relays On or Off as needed...
-----------------( END COPY )----------------------

thanks Kopel for replying.
Since its been over a year since I started this project I do not remember/know where I got the relay board.( no name on bottom ) Same with the LCD shield (probably Chinese jobbies) but I'm trying this the now without the LCD shield as I want to get the basics right before I get too elaborate.

" Show a sketch in your post between code tags: " Not sure what you mean here .?

I could use better variable names , the shorter they are the better , will alter them to clearly reflect more of their purpose .

If I change thresholdmoist from a number to a % that should work better ? ( ie 900 = 12% ? ) I'll need to work on that !

" If a sketch has a number of pieces of code that are the same in functionality, then you might make a function for that code or use an array (or both)." ,
Do you mean that instead of writing out that section 6 times I could use something to read the same section 6 times increasing the sensor number and relay numbers by one each time ?

Thanks, you've gave me something's to think about and ideas to try to work it out .

Hi Terryking228.
Its a songle board like the one on your instructions page . 8 channel.

In my void setup I have :-

digitalWrite(relayPin1, HIGH);
pinMode(relayPin1, OUTPUT);
digitalWrite(relayPin2, HIGH);
pinMode(relayPin2, OUTPUT);

digitalWrite(pumpPin9, HIGH);
pinMode(pumpPin9, OUTPUT);

That does not stop them (leds on relay board) lighting up when first booted
After the code runs through the loop they go out when the code reads the instructions for that section and then works normally for a while . The message that the relays should be HIGH does not seem to get through to them .

Thanks

Scroll down to number 7 for code tags explanation: How to use this forum - please read. - Installation & Troubleshooting - Arduino Forum

If you had those moisture sensors in the soil for more than a few months you might want to extract them and clean them and inspect them they do wear out and you may need new ones

eccky:
On my bench when I have it running it works reasonably well but when at first boot up all the relays (energise) open. They close one by one as the sketch runs. This can lead to the plants being flooded.

This definitely means back to the drawing board for a while. What you probably want is your relays to be NOT energised at boot-up (power-up) of the arduino - so the solenoids won't be pulling the valves into the watering state.

So in the setup() area of the arduino, it's necessary to ensure that the output pins that are going to control the relays are initialised to a state where the relays remain de-energised during arduino start-up. And also need to run some tests to see what happens when the system is doing its watering activity, and then the arduino suddenly turns off, or glitches. Some kind of extra monitoring systems might be needed to keep an eye on the arduino. Or these systems can keep an eye on each other.

For the moisture sensors..... could perhaps put a few of the same sensors in the same spot ----- and perhaps use a 'majority rules' system....where if say 4 sensors indicate dry, and 1 sensor indicates wet, then majority rules...... and start watering.

Those cheap PCB like resistive type moisture sensors will wear out quickly if you leave them powered on at all times. Months at best. All the copper from the PCB is then gone - and is now in your soil(!!!). Now of course copper is an essential trace element but it's also highly poisonous in higher concentrations, it's used to kill algae.

So get proper moisture sensors (capacitive type) or completely switch them off when not in use: disconnect both power lines, ground and Vcc, when not measuring. That should keep the copper where it should be.

Disconnecting is not enough because the measured value is changed by electrolysis. Even alternating the voltage and measure the resistance in both ways and average those values will work only for a limited time.

AliExpress has these very cheap capacitive soil moisture sensors. I think they use a 555 and create some kind of analog output. I have not tested them. They also seem to be very short.

I found some good images on the dfrobot site of an older version, and indeed the big IC is a 555. Part U2 could be a 3.0V regulator (based on the output range of 0-3V, seemingly independent of the supply voltage of 3.3-5V).

I wonder what that part T4 is, that you can see on the aliexpress link - T normally means "transformer" but that doesn't make sense for a part with just two connections.

That is a diode, it is black in the DFRobot version, but still a diode.
Here is a schematic: Welcome caffeineowl.com - BlueHost.com.