I need some help with irrigation code

I'm currently attempting to code something that connects to a relay module and sends a signal to a water pump in order to irrigate some plants. However, I noticed that the pump would continuously "flicker", with the pump turning on for a little bit and then turning off a short while afterwards.

const int dry = 420;      // Moisture threshold to start watering
const int pumpPin = 12;   // Relay pin
const int soilSensor = A4; // Soil moisture sensor pin

void setup() {
  pinMode(pumpPin, OUTPUT);
  Serial.begin(9600);
  digitalWrite(pumpPin, HIGH); // Ensure pump is OFF at start
  delay(5000);
}

void loop() {
  int moisture = analogRead(soilSensor);
  Serial.println("Soil Moisture: " + String(moisture));
  delay(2000); // Small delay to stabilize readings

  if (moisture <= dry) { // Start watering if too dry
    Serial.println("Watering starts now... Moisture is " + String(moisture));
    digitalWrite(pumpPin, LOW); // Turn ON pump

    delay(30000); // Keep watering for 7.5 sec

    digitalWrite(pumpPin, HIGH); // Turn OFF pump
    Serial.println("Done watering. Waiting for soil to absorb water...");

    delay(10000); // Wait for water to absorb

    // **🔹 Double-check moisture level AFTER watering**
    moisture = analogRead(soilSensor);
    Serial.println("Checking moisture after watering: " + String(moisture));

    // **If soil is STILL dry, wait before checking again**
    if (moisture <= dry) {
      Serial.println("Soil still appears dry, waiting 30 seconds before checking again.");
      delay(30000);
    } else {
      Serial.println("Soil is wet enough, normal checking will resume.");
      delay(20000);
    }
  } else {
    delay(10000); // Default delay between checks
  }
}

Any sort of help in this issue would be appreciated, as this is the 6th version of the code, and it's still flickering.

Thanks in advance.

That may indicate it's a hardware issue, not software, so show us a schematic. For exam[ple, are you powering the relay from the 5V pin of the Arduino(presuming it's an Uno, you don't say)?

To better understand and be able to help, publish the schematic of your project, detailing the part related to how you power the components.

and, as a general suggestion, lose the ‘delay’s, and implement a state machine using millis((emphasised text

To both questions, yes.

We often see problems when people run relays from the 5v source on the Uno. The regulator doesn't have the oompf for it.
What is your source of power - 12V? 8V? It matters, because the higher the voltage, the hotter the regulator gets for a given output current on that pin.

And if the pump is also on that pin, forget it right now, go buy a power supply.

1 Like

I mainly use either a 5V plug or my laptop as power.

Is the moisture sensor reading while watering? If so, the readings could oscillate. Normally, the moisture sensor is discontinued just before the start of a watering session and not reactivated until a few minutes or even hours after. (I am assuming it is NOT hydroponic) Turn water on according to a table that takes into account kind of plant, stage of development (some want more water before fruit set) and temperature plus time of year (if it's the hottest time of year, maybe add 20% or more extra water)

I'm sure you get better support here if you make a simple drawing illustrating how your devices are powered/wired.

1 Like


Here is the schematic.

What is the pump's voltage and current ( or wattage) ratings?

By your drawing, thank you, you show you are running the pump from the Arduino 5 volt pin. NO! NO! Use a separate power supply for any motor.

Did you notice that posting the schematic made a difference in the help?

I agree with @Paul_KD7HB.

Arduino is not a power supply.
It can even provide a few milliamps for some modules,
but it is not capable of providing current to drive motors.

You can even damage your Arduino if you use it to drive motors.

Post #6. Really.

You need a flyback diode for the pump. Otherwise when the pump is switched off, the negative voltage it generates could damage the Arduino or the sensor.

1N400x will work. Anode to ground.

Unfortunately, I need to make do with the things I have.

const int dry = 500;      // Moisture threshold to start watering
const int wet = 700;      // Moisture threshold to stop watering
const int pumpPin = 12;   // Relay pin (LOW-level trigger)
const int soilSensor = A4; // Soil moisture sensor pin

bool isWatering = false;  // Track watering state

void setup() {
  pinMode(pumpPin, OUTPUT);
  Serial.begin(9600);
  digitalWrite(pumpPin, HIGH); // Ensure pump is OFF at start
  delay(5000);
}

// Function to get stable moisture reading
int getStableMoisture() {
  int sum = 0;
  for (int i = 0; i < 5; i++) { // Take 5 readings
    sum += analogRead(soilSensor);
    delay(100);
  }
  return sum / 5; // Return averaged value
}

void loop() {
  int moisture = getStableMoisture();
  Serial.println("Soil Moisture: " + String(moisture));
  delay(2000); // Small delay to stabilize readings

  if (!isWatering && moisture <= dry) { // Start watering if too dry
    Serial.println("Watering starts now... Moisture is " + String(moisture));
    digitalWrite(pumpPin, LOW); // Turn ON pump
    isWatering = true;
  } else if (isWatering && moisture >= wet) { // Stop watering if sufficiently wet
    Serial.println("Stopping watering... Moisture is " + String(moisture));
    digitalWrite(pumpPin, HIGH); // Turn OFF pump
    isWatering = false;
  }

  delay(10000); // Delay between checks
}

Also, this is my 8th version.

Unfortunately, since you cannot fix a problem presented to you, I see little point in continuing to assist. The problem statement clearly indicates a power problem, we identified it, and you're not fixing it. I'm done.

The things limitation I can understand, but there's no reason you can't add or modify or start all over again with the code.

Which now looks plausible.

Do you have a spare LED and appropriate series current-limiting resistor? A good way to see if you have a wiring or power issue is to remove anything like a real pump attached.

Instead, just hang the LED and resistor off the output pin the will one day turn on and off the pump. Wire them between the pin and ground.

It is likely that the LED will react properly to changes on the moisture sensor.

a7

Sorry, didn't mean to put that part about the code in, I can change that no problem.

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