Soil moisture sensor with relay and pump

So I've been wanting to make a simple water irrigation system with my Arduino R3 development board, a relay, a pump and a soil moisture sensor.

The goal is to control the relay with 5V output from GPIO pin 10 and thus controlling the 9v circuit to the pump.

I followed a lot of tutorials on the internet but I can't seem to figure it out..

Everything is working perfectly if I run it the first time, the analog pin A0 is reading sensor data, if it is under a certain threshold it will turn on or off the GPIO pin 10 and activate the relay.

The pump turns on but If I take out the sensor out of my glass of water, the pump should stop, but it doesn't. The relay stays activated until I turn off the battery, then the relay clicks back to the off position. If I disconnect the battery from the system and let the Arduino program run, then then the relay is clicking on and off nicely. So the problem is probably the voltage in the 9V circuit keeping the relay somehow activated?

The text on the relay is like this:
KY-019 5v one channel relay
10A 250VAC, 10A 125VAC
10A 30VDC, 10A 28VDC
SRD-5VDC-SL-C

Is this the correct wiring for what I want to achieve?
The arduino board is powered by a 5V net adapter. Wall socket.

#define sensorPin A0
#define RELAY_PIN 10

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);
}


void loop() {
   // Read the Analog Input
   // 625 IS AIR = 0%
   // 280-290 = WATER = 100%
  int value = analogRead(sensorPin);
  
  // Print the value to the serial monitor
  Serial.println("Analog output: ");
  Serial.println(value);
  
  // Wait for 1 second before the next reading
  delay(3000);

  if(value > 400){
    Serial.println("The soil moisture is DRY => activate pump");
    digitalWrite(RELAY_PIN, HIGH);
    delay(2000);
  }else{
    Serial.println("The soil moisture is WET => deactivate the pump");
    digitalWrite(RELAY_PIN, LOW);
    delay(2000);
  }

  delay(1000);
}

The relay contacts - NO & NC - should be completely isolated from the control side of the relay. Have you checked the bottom of the relay module to see if there is a stray strand of wire? Is the surface the relay module sitting on non-conductive?

1 Like

Does it work without the pump connected? Note any wires over 25cm/10".

Which Arduino and how is it powered?

So, what is printed after you take the sensor out of water? Logic says you need to turn the pump OFF if out of water. Are you wanting to pump water when the soil is wet?

markd833 They are separated, and I have it on a wooden surface, so it shouldn't be conductive.

@Paul_KD7HB When the soil moisture sensor is in the water it reads about 290, when I take it out it reads about 620. So in the logic I wrote that if the value is above 400 it should turn on the pump because it is dry, when I put it back in the water it is under 400 so it should turn off.

gilshultz I will check if it works without pump. Without battery it works perfectly, but as soon as turn on the battery it will click on but won't turn off anymore.

@JCA34F It is an Arduino R3 development board it said. I've attached a picture.
The GPIO pin is giving 5V and 0V accordingly to my programmed routine so that one works.

When I disconnect the battery (it is a 6 battery pack with an ON/OFF switch) I hear the clicking perfectly of the relay, it turns on and off. But when I turn on the battery it will only click on but won't click off anymore, it is stuck there. I thought it was the battery pack but even when I power it off a wall outlet with 12v it's still stuck. I will check with another relay but as I remember I had the same problem there.


Can you show us a photo of the bottom of the relay board?

From a safety point of view, I would also move your extension block away from the cup of water and bucket of water.

In the following situation:
Place the sensor inside the glass of water, the relay should activate the pump.
Then remove the sensor from the cup.
Expect to print "There is probably a connection error or your DHT11 is defective."
In this situation the relay should be deactivated. Right?
If it is not deactivated with a DVM, measure the voltage at pin 10 of the Arduino and tell us the result.

If you don't have a DVM, you can use an LED with a resistor of +- 330 Ohms, connected between pin 10 and GND.

But! What is the reading when the sensor is dry and has been out of the water for several hours? That will be the dry soil value.

Does the relay module include the required protective diode across the coil?

Thanks Mark, normally I have it on the ground but for the photo I put it all together, but thanks for the tip! Attached is a photo of the bottom of the board.

I think I found the problem! @ruilviana
I was doing some tests with:

  • moisture sensor in water -> relay turns off -> reading on pin 10 = 0V without battery attached to relay/pump

  • moisture sensor out of water -> relay turns on -> reading on pin 10 = 4.8V without battery attached to relay/pump

Next:

  • moisture sensor in water -> relay turns off -> reading on pin 10 = 0V with battery attached to relay/pump

  • moisture sensor out of water -> relay turns on -> reading on pin 10 jumps between 5.3 and 5.8v with battery attached to relay/pump

  • turn back soil sensor in water -> pump keeps on running and reading on pin 10 is still ~5.3 V but drops back to 4.8V after a while (like two minutes) and when the voltage is low enough for the relay to turn off it turns off.

So I guess the relay is brining back power to the module... (upper two pictures)

I took another relay module but this one turns off at 5V and turns off at 0V, don't know if this is a problem though.. I reversed my code to turn pump on with 0V and pump off with 5V.

Thanks everybody for the replies, I guess I have to use my multi meter more to find these things and investigate a little deeper in relays. :slight_smile:



Hi! I'm also doing the exact same thing. My code can't run when I connect the pump to the arduino. Could somebody help?

Hi Ethan,

What I did was remove the conditions to trigger something, rather see what the moisture values do with the pumps running, because it influences the moisture sensor values and thus triggering the conditions in your code.

To solve the fluctuating readings you can try to solder a 0.1 uf keramic capacitor between the + and - poles of your wire to the pump, and place a 1N4007 diode in the reverse way on the same wire.

I also used a seperate power supply for the pumps, seperate power supply for the sensors and one for my ESP. That way I have no interference between them.

I also used 3.3V high level trigger relays for my ESP.

What I did wrong was when the pumps were running, the readings from the moisture sensor would be different, then it would trigger my code the relays would stay open because the moisture value was above X , even though you take it out of the water, or put it in for example, depending on your code.

Let me know if it works!

Great! It worked

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.