Arduino automatic plant waterer, relay works in setup() but not in loop().

Thread Starter

Gary Clarke

Joined Sep 11, 2019
17
Hi,

i'm developing an automatic plant-watering system with the Uno. It uses moisture-sensors to detect the need for watering and relays to switch pumps on and off. The relays, sensors and pumps all work on 5V, which comes from a bench PSU into a rail on a breadboard. The Uno's ground is connected to the 5V rail ground so that there is a common ground.

relay problem.png

I've written test code that monitors the moisture level (having converted it into a moisture percentage, akin to RH) and switches the relay to power the pump when the percentage is low, so it's simple code.

C:
int pump = 7;
int sensor = 1;

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(pump, OUTPUT);
  digitalWrite(pump, HIGH); Test the pump for 1 second, this works fine.
  delay(1000);
  digitalWrite(pump, LOW);
  }
void loop() {
  int val,mval;
  val = analogread(sensor); // read moisture sensor value
  mval = map(val,200,700,100,1); // convert to rough percentage
  lcd.setCursor(0,0);
  String str1 = String("RAW VALUE : ") + val + String(" "); // display sensor value
  lcd.print(str1);
  lcd.setCursor(0,1);
  String str2 = String("PERCENTAGE : ") + mval + String(" "); // display rough percentage value
  lcd.print(str2);
  Serial.println((String)"VAL : " + val + " MVAL : " + mval); // display values on serial too
  if (mval<=50) water_pump01(); // if the rough percentage is less than 51 then pump water
  delay(2000);
}
void water_pump01(){
  int val,mval;
  String str1,str2;
  digitalWrite(pump, HIGH); // turn the pump on, this is the current problem since it doesn't  go on
  Serial.println("PUMP ON"); // a message so that we know we've entered the pumping routine
  str1 = String("PUMP ON         ");
  lcd.setCursor(0,0);
  lcd.print(str1);
  val = analogread(sensor);
  mval = map(val,200,700,100,1);
  while (mval<70) { // while the moisture level hasn't reached 70% then keep pumping
  val = analogread(sensor);
  mval = map(val,200,700,100,1);
  Serial.println(val);
  Serial.println(mval);
  str2 = String("MOISTURE : ") + mval + String("  ");
  lcd.setCursor(0,1);
  lcd.print(str2);
  delay(2000);
  }
  // moisture level has reached 70% so turn pump off
  digitalWrite(pump, LOW);
  str2 = String("PUMP OFF :      "); // a message so we know pump is being turned off
  lcd.setCursor(0,0);
  lcd.print(str2);
  Serial.println("PUMP OFF");
  val = analogread(sensor); //connect sensor to Analog 0
  mval = map(val,200,700,100,1);
  str2 = String("MOISTURE : ")  + mval + String("  ");
  lcd.setCursor(0,1);
  lcd.print(str2);
  delay(1000);
  
  }
At the beginning of the test code i send the relay pin high for 1 second to test that it's working, this works fine, but when the relay is switched due to moisture levels being too low the relay doesn't switch, there's no click and no voltage at the outputs.

Thanks for reading, Gary.
 
Last edited by a moderator:

AlbertHall

Joined Jun 4, 2014
8,381
Pin 7 is defined as being the pump but then in the loop it is set as one of LCD pins.
Change one or the other to a different pin.
 

Thread Starter

Gary Clarke

Joined Sep 11, 2019
17
Thanks Albert, i can't believe i didn't notice that!

I've tried all the other unused pins now and it still fails to switch on.
 

Thread Starter

Gary Clarke

Joined Sep 11, 2019
17
The code modification was just me stepping through the unused D pins, so only the pump declaration at the beginning has changed.
 

AlbertHall

Joined Jun 4, 2014
8,381
Oddly, the 1-second pump test at the beginning works when powered by USB but not when powered by AC adapter.
Your diagram doesn't show how the breadboard gets its power/
When powered by adaptor everything else works, sensor readings, etc, but the relay doesn't operate?
is the adaptor feeding the breadboard power or the arduino?
If it is feeding the arduino, where is the breadboard getting its power?
 

Thread Starter

Gary Clarke

Joined Sep 11, 2019
17
Apologies, the breadboard is getting power from a bench PSU that does up to 18V @ 2A, i have it set to 5V output.
relay problem.png
Yes, when Uno powered by adapter all works fine (apart from the relay not switching, but it's LED is on when polled), but when powered by USB (and common ground removed) the test at the beginning doesn't work.
I think i may try powering it through Vin and see what happens.
 

Thread Starter

Gary Clarke

Joined Sep 11, 2019
17
I can't power this Elegoo Uno R3 clone via Vin!?

I've tried from a bench PSU and a fresh (tested) 9V battery, i've tried Vin with all 3 ground points but no joy.

Perhaps the clone isn't dependable, going to swap it out for the original Arduino Uno.
 

SamR

Joined Mar 19, 2019
1,175
As a general note, I would not depend on soil moisture to turn the water off. I would put it on a timer and also include a delay time before allowing the pump to start again. This will give the water time to equally saturate the soil and give a correct soil moisture reading. Otherwise, you will be creating a swamp. The Elegoo clone is a fully functional Uno in my experience. It does sound like a power problem so try another board.
 

djsfantasi

Joined Apr 11, 2010
5,614
When powering a UNO from the VIN pin, the voltage applied must be 5VDC as this pin bypasses the internal regulators. If you’ve applied any other voltage (like from a battery), you may have damaged the UNO. I power Arduinos with 9VDC from the barrel connector which uses the onboard power regulators. I have purchased the 5.1mm plugs in bulk to do this.
 

djsfantasi

Joined Apr 11, 2010
5,614
As far as your diagram goes, you have labeled your Arduino power supply as AC in. I’m assuming that’s a typo as the Arduino won’t use AC as a power supply

If the remainder of the wiring remains the same, it should work powered by USB. But if you remove the ground connection between the UNO and the breadboard thinking that it’s not needed since you’re powering the UNO through the USB, don’t do that. That connection is necessary.
 

Thread Starter

Gary Clarke

Joined Sep 11, 2019
17
Thanks Albert, i tried running the program on my original Arduino Uno board but it totally misbehaves, unsure why. I have an Arduino Nano somewhere, i'll dig it out and test with that, i'm not confident in the clone-board now that i've seen it can't be powered via Vin/GND.
 

djsfantasi

Joined Apr 11, 2010
5,614
Thanks Albert, i tried running the program on my original Arduino Uno board but it totally misbehaves, unsure why. I have an Arduino Nano somewhere, i'll dig it out and test with that, i'm not confident in the clone-board now that i've seen it can't be powered via Vin/GND.
Have you tried commenting out all lines pertaining to the LCD? One difficulty in using shields is ensuring there are no pin conflicts.
 

Thread Starter

Gary Clarke

Joined Sep 11, 2019
17
Have you tried commenting out all lines pertaining to the LCD? One difficulty in using shields is ensuring there are no pin conflicts.
Yes i stripped it down to bare bones code/hardware in an effort to simplify and isolate but no joy.

I thought i knew enough to take this project on but i don't! It's fun learning though :)

Thanks again djs.
 

SamR

Joined Mar 19, 2019
1,175
Simplify, simplify, simplify... Your code includes a LCD display not shown in your block diagram. Break it down. Trigger the pump at a set point from the sensor... Or just use a LED as an indicator for the pump relay input... go from there. As an aid to walking through the program, do you know how to use the serial monitor? It can be immensely valuable stepping through a program to ensure each input/output is working as it should.
 
Last edited:
Top