Automatic plant watering is one of the most popular first projects: a sensor in the soil, and when it dries out a pump switches on. But the first sensor most people buy fails within weeks. Let's see why, and build a watering project that runs for months without flooding everything.
The resistive type (the fork)
Two metal prongs with a current flowing between them through the soil. The wetter the soil, the lower the resistance. The module puts the fork with a fixed resistor as a voltage divider, so the voltage you read changes with moisture.
- Very cheap and easy.
- The problem: direct current through wet soil causes electrolysis, so the metal corrodes within days or weeks, and the reading goes wrong. You'll find one of the two prongs blackened or its coating gone.
The module usually comes with a small board carrying an LM393 and a potentiometer, with two outputs: A0 analog, and D0 digital, which switches when moisture crosses the threshold you set. That's exactly a comparator.
How to extend its life
Don't keep it powered all the time. Connect its VCC to a digital pin, switch it on a moment before reading and off afterwards:
digitalWrite(7, HIGH);
delay(10);
int v = analogRead(A0);
digitalWrite(7, LOW);
And read every few minutes, not every second. Soil doesn't dry out quickly. That way current flows through the fork for 10 ms every few minutes instead of 24 hours a day, and it lasts far longer. The module draws only a few milliamps, so an Arduino pin can power it.
The capacitive type
A sealed board with no exposed metal. It measures moisture through the change in capacitance: water changes the properties of the insulator around the traces inside the board.
- No corrosion, so it lasts much longer.
- A little more expensive, and I recommend it for any project that will keep running.
- Watch out: the top edge of the board isn't waterproof, so don't bury it up to the electronics. And some cheap versions have manufacturing faults, so test it before relying on it.
Faults common in cheap versions
- Doesn't work at 3.3 V: some versions carry an ordinary 555 chip that needs at least about 4.5 V. If you'll use it with an ESP32, try it on 3.3 V first, or power it from 5 V and note that its output may then exceed 3.3 V.
- Slow readings, or readings that don't come back: a resistor on the board isn't properly connected to ground. You'll see the reading take minutes to change.
- The reading changes with sun heat: somewhat normal. Mount it in shade if you can, and compare readings at the same time each day.
If you suspect a sensor, the calibration below exposes it: if the difference between air and water is very small, it's probably faulty.
Calibration
The numbers vary with the sensor and soil type, so do this:
- Read the sensor in air (completely dry). Note the number.
- Read it in a glass of water (up to the line). Note the number.
- Convert to a percentage:
map(v, dry, wet, 0, 100), withconstrainso the result stays within 0–100.
On the capacitive type, wet usually gives a lower number than dry, so watch the order.
Then test in the actual soil: water the plant well, wait half an hour, and note that reading as "wet enough". Wait a few days until the plant really needs water, and note that as "needs watering". Those two numbers matter more than the air and glass readings, because sandy and clay soils give very different readings for the same moisture.
Where to place the sensor
- At the roots, not at the surface. The surface dries quickly while the roots are still wet.
- A little away from where the water lands, so the reading doesn't spike wet at the moment of watering and then drop.
- Firmly: if the sensor moves or sits in an air gap, the reading will say dry.
- In a large bed, one sensor may not represent the whole bed. Two sensors in different places are better.
The watering project
- The pump needs a transistor or MOSFET or a relay, not an Arduino pin. A small 5 V pump draws 100–300 mA, and needs a protection diode across its terminals.
- Have a gap between the on and off thresholds (say on below 30% and off above 45%) so it doesn't keep switching.
- A maximum run time, so that if the sensor falls out of the soil, the pump doesn't flood everything.
- Power the pump from a separate supply with a common ground, so its start-up doesn't reset the Arduino.
const int PUMP = 8, SENSOR = A0, PWR = 7;
const int DRY = 520, WET = 260; // from your calibration
const int ON_BELOW = 30, OFF_ABOVE = 45; // percent
const unsigned long MAX_RUN = 20000; // at most 20 seconds of watering
bool pumping = false, fault = false;
unsigned long startedAt;
int moisture() {
digitalWrite(PWR, HIGH); delay(10);
int v = analogRead(SENSOR);
digitalWrite(PWR, LOW);
return constrain(map(v, DRY, WET, 0, 100), 0, 100);
}
void setup() {
pinMode(PUMP, OUTPUT); pinMode(PWR, OUTPUT);
Serial.begin(9600);
}
void loop() {
int m = moisture();
Serial.println(m);
if (!fault && !pumping && m < ON_BELOW) {
pumping = true; startedAt = millis(); digitalWrite(PUMP, HIGH);
}
if (pumping && m > OFF_ABOVE) { pumping = false; digitalWrite(PUMP, LOW); }
if (pumping && millis() - startedAt > MAX_RUN) {
pumping = false; fault = true; digitalWrite(PUMP, LOW); // problem: stop until someone checks
}
delay(pumping ? 1000 : 60000);
}
If the pump reaches its maximum run time and the soil is still dry, the code treats it as a fault (the sensor is out of the soil, the tank is empty, or the hose is cut) and won't run it again until you reset. That's far safer than letting it run every minute. You can add an LED or a buzzer to alert you.
And if you want to see the readings and control it from your phone, the ESP32 can show them on a web page. On a battery, have it wake every few minutes with deep sleep.
Pump or valve?
- A small submersible pump (3–6 V or 12 V): draws from a tank or bucket. Suited to pots and small areas. Don't run it dry, because the water itself cools it.
- A solenoid valve: fitted on a pressurised water line (a tap or a rooftop tank), opened and closed electrically. 12 V DC valves are common and draw about half an amp while open. It's a coil like a relay, so it needs a reverse protection diode. Note that most need a minimum water pressure to close properly, so they won't work well with a tank at a low height.
- A 220 V pump for a large bed: the Arduino drives a relay or contactor, and the pump is on its own fused mains circuit. Read mains safety before going near that part.
Don't let the pump run dry
If the pump draws from a tank, a simple level sensor in the tank (a float, or even two wires measuring continuity) stops it running when the tank is empty. Or use a distance sensor above the tank to know the level in centimetres.
FAQ
The reading jumps between consecutive readings? Average 5–10 readings, and a 100 nF capacitor between A0 and ground helps.
Can I connect several sensors to the same Arduino? Yes, each on its own analog pin, powered from different digital pins, or from one pin if their current is small.