Arduino UNO Plant watering system not working

Hello,
I am a newbie and trying to make a plant watering system using Arduino UNO. Below is the code I used.
When I first switch on power, it works fine. After that it never turns on the pump even if the soil is dry. But if I switch it off and back on, it works for one more time. I tried to read the serial monitor, but the problem is, when I connect the USB cable, it starts working again.
I have kept a delay of 5 hours and the power to the sensor module is OFF before it reaches the 5 hrs to minimise corrosion of sensor probes.
I have also added a Cal switch to bypass the 5 hrs delay.
Any help is appreciated.. Thanks!!

void setup() {
  Serial.begin(9600);                   // initializes serial communication at 9600 bits per second
  pinMode(2, OUTPUT);                   // sets pins 2 to 5 for water pumps in output mode
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, INPUT);                    // Digital pin 6 for Cal switch
  pinMode(7, OUTPUT);                   // sets pin 7 for powering sensors
}

void loop() {                        
  
  digitalWrite(7, HIGH);                // sets the digital pin 7 high, to power sensors
  delay(2000);                          // waits for 2 seconds
 
  int sensorValue1 = analogRead(A0);    // read value of humidity sensor
  int sensorValue2 = analogRead(A1);
  int sensorValue3 = analogRead(A2);
  int sensorValue4 = analogRead(A3);
  Serial.println(sensorValue1);         // prints value to serial
  Serial.println(sensorValue2);
  Serial.println(sensorValue3);
  Serial.println(sensorValue4);
  
  if(sensorValue1 > 450){              // Value to be adjusted based on actual testing
    digitalWrite(2, HIGH);             // turns pump1 on if moisture is less than set value
    delay(500);                       // waits for 1sec
  }
  else{
    digitalWrite(2, LOW);              // does not turn on pump 1 if moisture level is sufficient
    delay(500);                       // waits for 1sec
  }
  
  
  if(sensorValue2 > 450){              // Value to be adjusted based on actual testing
    digitalWrite(3, HIGH);             // turns pump2 on if moisture is less than set value
    delay(500);                       // waits for 1sec
  }
  else{
    digitalWrite(3, LOW);              // does not turn on pump2 if moisture level is sufficient
    delay(500);                       // waits for 1sec
  }
  
  
  if(sensorValue3 > 450){              // Value to be adjusted based on actual testing
    digitalWrite(4, HIGH);             // turns pump3 on if moisture is less than set value
    delay(500);                       // waits for 1sec
  }
  else{
    digitalWrite(4, LOW);              // does not turn on pump3 if moisture level is sufficient
    delay(500);                       // waits for 1sec
  }
  
  
  if(sensorValue4 > 450){              // Value to be adjusted based on actual testing
    digitalWrite(5, HIGH);             // turns pump4 on if moisture is less than set value
    delay(500);                       // waits for 1sec
  }
  else{
    digitalWrite(5, LOW);              // does not turn on pump4 if moisture level is sufficient
    delay(500);                       // waits for 1sec
  }
   delay(5000);                        // waits for 5 seconds

   digitalWrite(7, LOW);               // sets the digital pin 7 off to turn off sensors
   digitalWrite(2, LOW);               // Turns off all pumps
   digitalWrite(3, LOW);
   digitalWrite(4, LOW);
   digitalWrite(5, LOW);
  if (digitalRead (6) == HIGH) {
    delay(18000000);                  // waits for 1 hour before taking next measurement
  }
  else{
    delay(0);
  }
}

Try this :

I simplified your logic a little. Maybe it'll work?

void setup() {
  Serial.begin(9600);                   // initializes serial communication at 9600 bits per second
  pinMode(2, OUTPUT);                   // sets pins 2 to 5 for water pumps in output mode
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(7, OUTPUT);                   // sets pin 7 for powering sensors
}

void loop() {                        
  
  digitalWrite(7, HIGH);                // sets the digital pin 7 high, to power sensors
  delay(2000);                          // waits for 2 seconds, maybe the sensors need this? I left it in.
 
  int sensorValue1 = analogRead(A0);    // read value of humidity sensors
  int sensorValue2 = analogRead(A1);
  int sensorValue3 = analogRead(A2);
  int sensorValue4 = analogRead(A3);
  digitalWrite(7, HIGH);                // Shuts them off..

  Serial.print("Reading 1 : ");Serial.println(sensorValue1);         // prints value to serial
  Serial.print("Reading 2 : ");Serial.println(sensorValue2);
  Serial.print("Reading 3 : ");Serial.println(sensorValue3);
  Serial.print("Reading 4 : ");Serial.println(sensorValue4);
  Serial.println();
  
  if(sensorValue1 > 450){              // Value to be adjusted based on actual testing
    digitalWrite(2, HIGH);             // turns pump1 on if moisture is less than set value
  }
  if(sensorValue2 > 450){              // Value to be adjusted based on actual testing
    digitalWrite(3, HIGH);             // turns pump2 on if moisture is less than set value
  }
  if(sensorValue3 > 450){              // Value to be adjusted based on actual testing
    digitalWrite(4, HIGH);             // turns pump3 on if moisture is less than set value
    delay(500);                       // waits for 1sec
  }
  if(sensorValue4 > 450){              // Value to be adjusted based on actual testing
    digitalWrite(5, HIGH);             // turns pump4 on if moisture is less than set value
  }
  delay(5000);                         // waits for 5 seconds (Watering time fort dry plants. Adjust to suit)

  digitalWrite(2, LOW);               // Turns off all pumps
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  
  delay(60000);                         // waits for a minute to "soak". (Adjust to suit)
}

-jim lee

Did you use pull-up or pull-down resistor for pin 6 ?

IoT_hobbyist:
Did you use pull-up or pull-down resistor for pin 6 ?

Yes I do

Haha that's what you get for asking an ambiguous question.

My late Mum always used to ask "Do you want tea or coffee, if so which?"

blomcrestlight:
Haha that's what you get for asking an ambiguous question.

My late Mum always used to ask "Do you want tea or coffee, if so which?"

:slight_smile: I mean the switch has two positions to choose between high and low using pull up / pull down resistors for pin 6

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Sure.. please find attached and thanks for the response

girishrajg:
Hello,
I am a newbie and trying to make a plant watering system using Arduino UNO. Below is the code I used.
When I first switch on power, it works fine. After that it never turns on the pump even if the soil is dry. But if I switch it off and back on, it works for one more time. I tried to read the serial monitor, but the problem is, when I connect the USB cable, it starts working again.
I have kept a delay of 5 hours and the power to the sensor module is OFF before it reaches the 5 hrs to minimise corrosion of sensor probes.
I have also added a Cal switch to bypass the 5 hrs delay.
Any help is appreciated.. Thanks!!

void setup() {

Serial.begin(9600);                   // initializes serial communication at 9600 bits per second
 pinMode(2, OUTPUT);                   // sets pins 2 to 5 for water pumps in output mode
 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(6, INPUT);                    // Digital pin 6 for Cal switch
 pinMode(7, OUTPUT);                   // sets pin 7 for powering sensors
}

void loop() {                        
 
 digitalWrite(7, HIGH);                // sets the digital pin 7 high, to power sensors
 delay(2000);                          // waits for 2 seconds

int sensorValue1 = analogRead(A0);    // read value of humidity sensor
 int sensorValue2 = analogRead(A1);
 int sensorValue3 = analogRead(A2);
 int sensorValue4 = analogRead(A3);
 Serial.println(sensorValue1);         // prints value to serial
 Serial.println(sensorValue2);
 Serial.println(sensorValue3);
 Serial.println(sensorValue4);
 
 if(sensorValue1 > 450){              // Value to be adjusted based on actual testing
   digitalWrite(2, HIGH);             // turns pump1 on if moisture is less than set value
   delay(500);                       // waits for 1sec
 }
 else{
   digitalWrite(2, LOW);              // does not turn on pump 1 if moisture level is sufficient
   delay(500);                       // waits for 1sec
 }
 
 
 if(sensorValue2 > 450){              // Value to be adjusted based on actual testing
   digitalWrite(3, HIGH);             // turns pump2 on if moisture is less than set value
   delay(500);                       // waits for 1sec
 }
 else{
   digitalWrite(3, LOW);              // does not turn on pump2 if moisture level is sufficient
   delay(500);                       // waits for 1sec
 }
 
 
 if(sensorValue3 > 450){              // Value to be adjusted based on actual testing
   digitalWrite(4, HIGH);             // turns pump3 on if moisture is less than set value
   delay(500);                       // waits for 1sec
 }
 else{
   digitalWrite(4, LOW);              // does not turn on pump3 if moisture level is sufficient
   delay(500);                       // waits for 1sec
 }
 
 
 if(sensorValue4 > 450){              // Value to be adjusted based on actual testing
   digitalWrite(5, HIGH);             // turns pump4 on if moisture is less than set value
   delay(500);                       // waits for 1sec
 }
 else{
   digitalWrite(5, LOW);              // does not turn on pump4 if moisture level is sufficient
   delay(500);                       // waits for 1sec
 }
  delay(5000);                        // waits for 5 seconds

digitalWrite(7, LOW);               // sets the digital pin 7 off to turn off sensors
  digitalWrite(2, LOW);               // Turns off all pumps
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
 if (digitalRead (6) == HIGH) {
   delay(18000000);                  // waits for 1 hour before taking next measurement
 }
 else{
   delay(0);
 }
}

jimLee:
Try this :

I simplified your logic a little. Maybe it'll work?

void setup() {

Serial.begin(9600);                   // initializes serial communication at 9600 bits per second
 pinMode(2, OUTPUT);                   // sets pins 2 to 5 for water pumps in output mode
 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(7, OUTPUT);                   // sets pin 7 for powering sensors
}

void loop() {                        
 
 digitalWrite(7, HIGH);                // sets the digital pin 7 high, to power sensors
 delay(2000);                          // waits for 2 seconds, maybe the sensors need this? I left it in.

int sensorValue1 = analogRead(A0);    // read value of humidity sensors
 int sensorValue2 = analogRead(A1);
 int sensorValue3 = analogRead(A2);
 int sensorValue4 = analogRead(A3);
 digitalWrite(7, HIGH);                // Shuts them off..

Serial.print("Reading 1 : ");Serial.println(sensorValue1);         // prints value to serial
 Serial.print("Reading 2 : ");Serial.println(sensorValue2);
 Serial.print("Reading 3 : ");Serial.println(sensorValue3);
 Serial.print("Reading 4 : ");Serial.println(sensorValue4);
 Serial.println();
 
 if(sensorValue1 > 450){              // Value to be adjusted based on actual testing
   digitalWrite(2, HIGH);             // turns pump1 on if moisture is less than set value
 }
 if(sensorValue2 > 450){              // Value to be adjusted based on actual testing
   digitalWrite(3, HIGH);             // turns pump2 on if moisture is less than set value
 }
 if(sensorValue3 > 450){              // Value to be adjusted based on actual testing
   digitalWrite(4, HIGH);             // turns pump3 on if moisture is less than set value
   delay(500);                       // waits for 1sec
 }
 if(sensorValue4 > 450){              // Value to be adjusted based on actual testing
   digitalWrite(5, HIGH);             // turns pump4 on if moisture is less than set value
 }
 delay(5000);                         // waits for 5 seconds (Watering time fort dry plants. Adjust to suit)

digitalWrite(2, LOW);               // Turns off all pumps
 digitalWrite(3, LOW);
 digitalWrite(4, LOW);
 digitalWrite(5, LOW);
 
 delay(60000);                         // waits for a minute to "soak". (Adjust to suit)
}




-jim lee

Thanks a lot for your response
I am sorry, but what is the below line for? or, did you mean LOW?
digitalWrite(7, HIGH); // Shuts them off..

I mean the switch has two positions to choose between high and low using pull up / pull down resistors for pin 6

Which now makes sense with a schematic.

But unnecessary: spst switch to ground from pin with pinMode(7, INPUT_PULLUP); would give you definite high and low for open and closed respectively. And I think IoT_hobbyist was expecting a spst which is what prompted his or her question.

You resistor's not actually a pullup in the sense meant here, whose purpose is to steer the pin in a known direction when the switch is open. I'm not sure why you have a resistor there anyway.

Hi,
Can you draw and post a picture of your entire circuit, showing power supplies and sensors and motors and labels and pin names.

Please;
Post a link to spec/data of your sensors.
Post a link to spec/data of your motor.
Why so many 2N2222 transistors for one motor?
Have you considered using a single MOSFET to turn the motor ON and OFF.

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
Can you draw and post a picture of your entire circuit, showing power supplies and sensors and motors and labels and pin names.

Please;
Post a link to spec/data of your sensors.
Post a link to spec/data of your motor.
Why so many 2N2222 transistors for one motor?
Have you considered using a single MOSFET to turn the motor ON and OFF.

Thanks.. Tom... :slight_smile:

Thanks for the response. . Its actually 4 sensors and 4 pumps. Each pump driven by one 2N2222
I will update the schematic and send

girishrajg:
Thanks a lot for your response
I am sorry, but what is the below line for? or, did you mean LOW?
digitalWrite(7, HIGH); // Shuts them off..

Oh yeah, LOW. Don't have the hardware to debug it. Did you try it?

-jim lee

TomGeorge:
Hi,
Can you draw and post a picture of your entire circuit, showing power supplies and sensors and motors and labels and pin names.

Please;
Post a link to spec/data of your sensors.
Post a link to spec/data of your motor.
Why so many 2N2222 transistors for one motor?
Have you considered using a single MOSFET to turn the motor ON and OFF.

Thanks.. Tom... :slight_smile:

The schematic is attached.
Connectors CN1 and CN2 are connected to the 4 sensors and goes to A0, A1, A2, A3 of arduino through voltage dividers.
Connector C1 is connected to the pumps (PINS 2,4,6,8 are common +VE and PINS 1,3,5,7 are the GND to each pump driven through the 4 transistors 2N2222).

Sensor details are in the below link
Sensor
I am not using the module which comes with it as I am not using the digital output. I have the voltage divider in the circuit to provide analogue signal to Arduino as per the sensor resistance values

Motor(Pump) details are in the below link
Pump

Each 2N2222 transistor drives one pump

My power supply is a 5V 3A adaptor. In order to power Arduino, I am using a DC-DC step up off the 5V supply. DC-DC step up is shown in the below link
Step Up

Hope I have provided all the required details..

How to inline an image


blomcrestlight:
How to inline an image


:slight_smile: thanks!!

If everything runs on 5V, why are you stepping 5V up to 12V, then feeding it to the Arduino barrel jack where it's stepped back down to 5V? How much current do the sensors draw? Four of them may be too much for one output pin. I don't see kickback suppression diodes across the pump motors. What are the values of R5 ~ R8?

Hi,
Is S1 really a double pole double throw switch?
Why have a switch that does nothing in the 12V supply?
sw11111.jpg

Tom... :slight_smile:

sw11111.jpg

girishrajg:
:slight_smile: thanks!!

For the future, and this might just be me, I find it quite difficult to read with that very small grid turned on: gives it almost like a grey background so low contrast. If there's an update, could you turn the grid off before publishing?

Hi,
Sensor;
watersens.jpg
Pump;

Tom.. :slight_smile:

watersens.jpg