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.
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)?
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.
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)
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.
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.