Plant watering system code for hourly moisture measurement

Hi,
I have the below code which I am using for my plant watering system.
It uses 4 moisture sensors and 4 separate pumps for watering the plants when the moisture level drops.

int waterpump1=3;
int waterpump2=4;
int waterpump3=5;
int waterpump4=6;

void setup() {
Serial.begin(9600);
pinMode(waterpump1, OUTPUT);
pinMode(waterpump2, OUTPUT);
pinMode(waterpump3, OUTPUT);
pinMode(waterpump4, OUTPUT);
}

void loop() {
int humidityraw1=analogRead(A0); //1023 to 0 ===> 0 to 100%
int humidityraw2=analogRead(A1); //1023 to 0 ===> 0 to 100%
int humidityraw3=analogRead(A2); //1023 to 0 ===> 0 to 100%
int humidityraw4=analogRead(A3); //1023 to 0 ===> 0 to 100%

int humidityreal1=map(humidityraw1, 1023, 0, 0, 100);
int humidityreal2=map(humidityraw2, 1023, 0, 0, 100);
int humidityreal3=map(humidityraw3, 1023, 0, 0, 100);
int humidityreal4=map(humidityraw4, 1023, 0, 0, 100);

Serial.println(humidityreal1);
delay(100);
Serial.println(humidityreal2);
delay(100);
Serial.println(humidityreal3);
delay(100);
Serial.println(humidityreal4);
delay(100);

if(humidityreal1 > 30)
{
digitalWrite(waterpump1, HIGH);
}else{
digitalWrite(waterpump1, LOW);
}
if(humidityreal2 > 30)
{
digitalWrite(waterpump2, HIGH);
}else{
digitalWrite(waterpump2, LOW);
}
if(humidityreal3 > 30)
{
digitalWrite(waterpump3, HIGH);
}else{
digitalWrite(waterpump3, LOW);
}
if(humidityreal4 > 30)
{
digitalWrite(waterpump4, HIGH);
}else{
digitalWrite(waterpump4, LOW);
}
}

I am looking to add the below

  1. Measurements taken only once in an hour. This is to get better life of the sensors as continuous voltage is corroding it too quickly. Timing need not have to be exact.

  2. When low moisture is detected by of the sensors, the respective pump operates for 5 seconds and stops. Moisture is then measured again and if the moisture level is still not up to the required level, the pump runs for another 5 secs.

Can someone help me with the code for the above please?

Thanks in advance..

New Text Document.txt (1.29 KB)

Moisture is then measured again and if the moisture level is still not up to the required level, the pump runs for another 5 secs.

You don't want to do this immediately after the pump stops. You need to allow time for the water to be absorbed before you take another reading. Otherwise you will just flood the plants. So do you think 5 second every hour, if needed, will always be enough? Probably, but if not, you could make it 10 or 20 seconds per hour as needed.

You should start to improve you code by learning to use arrays.

Will the Arduino need to do anything else except monitor the moisture and turn pumps on and off? And I mean absolutely anything else, including monitoring buttons to see if they have been pressed, for example. If not, you can simply use delay(), but if other actions might be needed during the hours between measurements, then you will need to learn the more advanced skill of using millis().

Thanks for the response..
You are right, its not a good idea to take another measurement immediately after the 5 sec watering.
However the 5 sec duration of watering seems fine for my application as the pots are small, also this is used for indoor plants and the soil dries after several days.

I am actually trying to disconnect power to the sensors during the 1 hr gap, does delay() help with that? There is no other input to the arduino during this 1hr period.

Sorry, I am bit new in these stuff and still learning :slight_smile:

No, delay() will not help with disconnecting power to the sensors. For help on that question you will need to provide more information about your circuit and sensors: schematics, links to product information etc.

Thanks for your time and response..
I am using the attached sensors and submersible pumps for sensing and watering.

https://www.aliexpress.com/item/32281470046.html
https://www.aliexpress.com/item/32843122639.html

Also, the schematic as attached.
S1 to S4 are the sensor modules..
C3 is the connector to the sensors
C2 is the connector to the pumps.

Also attached is the schematic of the sensor module

Schematic.pdf (40.2 KB)

Unfortunately, a very important piece of data has been omitted by the seller of the pumps, which is the current draw of the pump during normal running, and the maximum current draw (at startup or during stall if a small object gets into the pump and jams the motor). To know if your circuit is correct, you need to measure or estimate these currents.

As you are using the analog output of these sensors, the circuit boards supplied with them do not provide any advantage, and consume more current than is needed because of the led and chip they contain. My advice would be not to use these boards. Connect the sensor probe itself directly to the analog inputs, each with a 10K resistor to form a voltage divider. This will give equivalent results as when using the board, but lower current consumption. To further reduce current consumption, connect an Arduino output pin to power all 5 of the probes, for the small time it takes to read the value from each one, then cut the power again.


Wow, prefect.. thanks a lot for that..
very much appreciate the fact that you took your time to look into the details I sent and provide support..

Could you please guide on writing the code, for example I use the D7 output from arduino to turn power on and after a small delay like two minutes, turn off power to the sensors every hour

BTW, I checked the pumps, they take nearly 200 mA during normal running.

I didn't understand your question about using D7, please try to explain it again.

200mA normal current means that a pump could easily draw 1A+ at startup. This is too much for your 2N2222 transistors, and too much for your 7805 regulator, even if only one pump runs at any time.

Thanks once again for the response..

About the D7, What I mean is that, as you said, if I avoid the sensor module and connect the sensor probe itself directly to the analog inputs, each with a 10K resistor to form a voltage divider, and use a digital pin from the arduino ( for example D7) to go high to provide the Vcc to the sensors every hour and stay high for like 2 minutes and go low, I hope the system will work as I intended. I was asking you how the code should be edited in that case...

Regarding the power to the motors, as you mentioned, it does look to like 7805 cannot handle the 1+ A at startup.. even though it is working good as of now, it may fail anytime..

I am planning to do as follows

  • the pump rated voltage is 2.5 to 6V..
  • arduino requires 7 to 12 volts.. so, I am planning to use a DC to DC step up converter like the below to power the arduino. I will use a 5V 2A power supply to power the pump and the below converter will take power off the adapter and the converter will boost it to what is required for the arduino.

I hope the above would be good to go..
Please advice..

Ok, understand the D7 idea now. No need to power the sensors for 2 mins in my opinion. Power up, take measurements, power down. You will get different answers if you wait 2 mins, but I'm not convinced they are in any way better answers. Plus your sensors will corrode far faster because the power is on for longer.

Good news, you don't need 7 to 12V to power the Arduino. That's only if you power it through the barrel jack. If you have a 5V 2A PSU, you can power the Arduino through its 5V pin.

Also, powering motors and an Arduino with the same PSU can be tricky. When motors start up, they draw a big current and this can cause the psu's voltage to dip momentarily, which can cause the Arduino to reset. So put a big cap, e.g. 1000uF, across the 5V & GND lines near the transistors. These will absorb some of the shock when the motors power up.

Also, don't forget the flyback diodes for each motor, e.g. in4001, right across the pump terminals.

PaulRB:
Ok, understand the D7 idea now. No need to power the sensors for 2 mins in my opinion. Power up, take measurements, power down. You will get different answers if you wait 2 mins, but I'm not convinced they are in any way better answers. Plus your sensors will corrode far faster because the power is on for longer.

Can you please guide me on writing the code in order for pin D7 to go high for may be a few seconds and then go low every one hour?

PaulRB:
Good news, you don't need 7 to 12V to power the Arduino. That's only if you power it through the barrel jack. If you have a 5V 2A PSU, you can power the Arduino through its 5V pin.

Oh, OK.. thats good.. so I can avoid the DC to DC step up converter. :-[

Use digitalWrite() and delay().

Thanks a lot once again for all the support and for your time..

I am doing the hardware modifications which you advised, and trying to modify the code for taking measurement every hour..

Will post details once its done..

Thanks!!