Water a Plant Automatically with an Arduino
Water a plant by itself, with an Arduino Uno, a soil moisture sensor and a relay. When the soil dries out, the relay opens a 12 V water valve; once the soil is wet enough, it closes it again.
It builds on the relay guide, which covers the module, its contacts and the diode across the coil. The sensor is SparkFun's soil moisture sensor: two probes in the soil, and a reading that rises as the soil gets wetter. Its numbers depend on your soil, so you set the two thresholds from your own readings.
Try it first, no parts needed
This is the whole project, running in your browser. Press Run simulation, then drag the soil's moisture down: under about 45 % the relay clicks on and the valve opens. Drag it back up, and the valve stays open until about 69 %.
The soil
The sketch reads the soil once a second.
Arduino Uno serial monitor115200 baud
What the code does
Once a second the sketch reads the soil. Below one number it starts watering; at or above a higher one it stops; in between it carries on as it was.
// Water a plant when its soil gets dry. A soil moisture sensor reads on A0; it is
// powered from pin 7 only while it reads, so its probes corrode slowly. A relay on
// pin 8 opens a 12 V water valve while the soil is dry.
const int SENSOR_POWER_PIN = 7;
const int SENSOR_PIN = A0;
const int RELAY_PIN = 8;
const int DRY = 400; // start watering below this reading
const int WET = 600; // stop watering at or above it
bool watering = false;
int readSoil() {
digitalWrite(SENSOR_POWER_PIN, HIGH);
delay(10); // let the reading settle
int reading = analogRead(SENSOR_PIN);
digitalWrite(SENSOR_POWER_PIN, LOW);
return reading;
}
void setup() {
Serial.begin(115200);
pinMode(SENSOR_POWER_PIN, OUTPUT);
digitalWrite(SENSOR_POWER_PIN, LOW);
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
int soil = readSoil();
if (soil < DRY) {
watering = true;
} else if (soil >= WET) {
watering = false;
}
digitalWrite(RELAY_PIN, watering ? HIGH : LOW);
Serial.print("Soil: ");
Serial.print(soil);
Serial.println(watering ? ", watering" : ", not watering");
delay(1000);
}const int DRY = 400; // start watering below this readingThe reading below which the soil counts as dry. Set it from your own soil; see set the thresholds.
const int WET = 600; // stop watering at or above itThe reading at which the soil is wet enough. It is higher than
DRY, so the valve doesn't flick on and off while a reading wavers around one number.digitalWrite(SENSOR_POWER_PIN, HIGH);Powers the sensor from pin 7 only while it reads, then waits 10 ms for the reading to settle. Powered all the time, its probes corrode in the wet soil.
int reading = analogRead(SENSOR_PIN);Reads the sensor on A0: 0 for dry air, up to about 880 for the probes in water, at 5 V.
if (soil < DRY) { watering = true; } else if (soil >= WET) {Under
DRY, start watering; atWETor more, stop. Between the two,wateringkeeps its last value.digitalWrite(RELAY_PIN, watering ? HIGH : LOW);Turns the relay on while watering, which opens the valve, and off otherwise.
How the two thresholds work
A single threshold would make the valve chatter: as the water soaks in, the reading crosses the number, then wobbles back and forth over it, and each crossing clicks the relay. Two numbers leave a gap between them. The valve opens below the lower one and closes only at the upper one; inside the gap, it stays as it was.
The relay and the valve work as in the relay guide: pin 8 HIGH turns the relay on, COM joins NO, and the valve's coil gets 12 V. The valve is normally closed, so it opens only while it has power, and closes again by itself if the power goes. The figure under this follows the relay in the running circuit.
- Across the input
- 5.0 V
- Coil
- Energized
5.0 V across the input from pin 8: the coil is energized, COM connects to NO, and the valve gets 12 V.
Build it
Now with real parts. Wire it, get the code, then upload it and set the two thresholds for your soil.
You'll need
- An Arduino Uno and its USB cable
- A SparkFun soil moisture sensor (SEN-13322), or another with VCC, GND and SIG
- A 5 V relay module with an H/L jumper, set to H
- A 12 V water valve, normally closed, such as Adafruit's plastic water solenoid valve (product 997), and a 12 V DC supply that gives at least 350 mA
- A 1N4001 to 1N4007 diode
- Tubing from a water supply with some pressure, such as a tap: the valve needs at least 0.02 MPa (3 PSI)
- Software to upload the sketch: Cirkit Designer, free in your browser, or the Arduino IDE
Wire it
The sensor's three wires, the relay's three, then the 12 V circuit through the relay's contacts, as in the relay guide with the valve in place of the solenoid. Wire the 12 V side with the supply unplugged, and keep the electronics away from the water.
Tap a connection to see it on the circuit.
From Connect to Show Sensor VCC Uno pin 7 Show Sensor GND Uno GND Show Sensor SIG Uno A0 Show Relay DC+ Uno 5V Show Relay DC− Uno GND Show Relay IN Uno pin 8 Show 12 V supply + Relay COM Show Relay NO Valve + Show Valve − 12 V supply − Show 1N4007 Across the valve, band on + Show Water flows one way through the valve. Fit the inlet to the supply, as the arrow on its body shows. Its coil has no polarity: whichever terminal you wire to NO is its + side, and the diode's band goes there.
Get the code
I'm using:Steps 2 and 3 follow the tool you pick.
The project opens with the sketch already in it. It needs no library.
The sketch needs no library. Copy it into a new sketch:
How it works line by line is in what the code does, above.
Upload and check it
Plug in your Uno. In Cirkit, open the Build tab, then Upload firmware: click Select Port and pick your Uno, then Upload to Device. Then open Upload output and serial monitor and click Start Serial Monitor. Plug in the 12 V supply last.
Show me where to click in Cirkit
Select a device port
Select the device port for uploading code and serial communications
COM3 (Arduino Uno)- Open Build
- Choose Upload firmware
- Click Select Port
- Pick COM3 (Arduino Uno)
- Click Upload to Device
- Open Upload output and serial monitor
- Click Start Serial Monitor
In the Arduino IDE, choose Arduino Uno and its port from the board menu, press Upload, then open the Serial Monitor and set it to 115200 baud. Plug in the 12 V supply last.
Show me where to click in the Arduino IDE
plant_watering.ino// Water a plant when its soil gets dry. A soil moisture sensor reads on A0; it is // powered from pin 7 only while it reads, so its probes corrode slowly. A relay on // pin 8 opens a 12 V water valve while the soil is dry. const int SENSOR_POWER_PIN = 7; const int SENSOR_PIN = A0; const int RELAY_PIN = 8;
OutputSerial Monitor115200 baudCompiling sketch...
Sketch uses 2390 bytes (7%) of program storage space. Maximum is 32256 bytes.
Global variables use 221 bytes (10%) of dynamic memory, leaving 1827 bytes for local variables. Maximum is 2048 bytes.
Uploading...
Soil: 440, not watering
Soil: 402, not watering
Soil: 396, watering
Soil: 512, watering
Soil: 604, not watering
Done uploading.
- Click the board menu
- Pick Arduino Uno on COM3
- Click Upload
- Wait for Done uploading
- Click Serial Monitor
- Check it is set to 115200 baud
It works when:
- The Serial Monitor prints a reading once a second: near 0 with the probes in the air.
- In the air, under 400, it prints
watering, the relay clicks on and the valve opens. - With the probes in a glass of water, the reading climbs past 600, it prints
not watering, and the relay clicks off.
Device SerialSoil: 440, not wateringSoil: 402, not wateringSoil: 396, wateringSoil: 512, wateringSoil: 604, not wateringOutputSerial MonitorSoil: 440, not wateringSoil: 402, not wateringSoil: 396, wateringSoil: 512, wateringSoil: 604, not wateringSet the thresholds for your soil
SparkFun's readings vary with the soil, so set
DRYandWETfrom yours. Push the probes into the plant's soil when it's as dry as you'd let it get and note the reading; water it well, wait a few minutes and note that one too. PutDRYa little above the first andWETa little below the second, and upload again.Not working? See troubleshooting.

Troubleshooting
Find your symptom; the checks under it run from the most common cause down.
The reading stays at 0, even in water
- The sensor's VCC is on pin 7, the pin the sketch powers it from, and its GND on GND.
- SIG is on A0.
It prints watering and the relay clicks, but no water flows
watering and the relay clicks, but no water flows- The water has pressure. The valve needs at least 0.02 MPa (3 PSI) to open; a bucket at the same height doesn't give it.
- The water goes in the inlet, the way the arrow on the valve points.
- The 12 V side is wired as in the relay guide: the valve on NO, the supply's + on COM, the diode's band on the + side.
It never stops watering
WETis above what your soil reads wet. Water it, read it, and setWETbelow that, as in set the thresholds.- The probes are deep enough to reach the wet soil.
The relay doesn't click
Check its power, its IN on pin 8 and its jumper on H, as in relay troubleshooting.
Make it yours
- Water in short bursts: water reaches the probes only as it soaks through the soil, so a long run can overshoot. Open the valve for a few seconds, then wait for it to soak in before reading again.
In place of the if and digitalWrite lines if (soil < DRY) { digitalWrite(RELAY_PIN, HIGH); // water for 5 s delay(5000); digitalWrite(RELAY_PIN, LOW); delay(60000); // let it soak in for a minute } - Read less often: SparkFun suggests reading much less often than once a second in a plant, and soil dries over hours.
In place of the delay at the end of loop() delay(60000); // read once a minute
FAQ
Why does the sensor get its power from pin 7?
So it has power only while it reads. SparkFun recommends it, and its own example powers the sensor from pin 7 the same way: with power on all the time, the probes corrode in the wet soil.
Can I use a pump instead of a valve?
Yes, a DC pump that runs from your supply: wire it in the valve's place on NO, with the diode across it. A pump needs no water pressure; check its voltage and current against the supply and the relay's contacts.
Can I run it on an ESP32?
The relay, yes, with the jumper on H; see the ESP32 guide. Power the sensor from a GPIO at 3.3 V and read SIG on an ADC pin: SparkFun's readings are lower at 3.3 V, and the ESP32's ADC counts differently, so set DRY and WET again from your own readings.
Simulating the plant watering in Cirkit Designer
The soil moisture slider is the soil's wetness as Cirkit's sensor takes it, 0 to 100 %. The sensor turns it into a voltage on SIG from 0 up to 0.7 V below its VCC, so 100 % reads about 880 at 5 V, as SparkFun's does in water; it gives nothing while pin 7 is LOW. It doesn't model the soil itself, corrosion or the 10 ms the reading takes to settle.
The relay is simulated as in the relay guide. The valve opens 0.15 s after its coil reaches 6 V and closes 0.3 s after it drops under 1.2 V; water flows while it is open with at least 20 kPa at its inlet, 200 kPa in this circuit. It doesn't model the flow rate or the kick its coil gives when switched off.
References
- SparkFun: soil moisture sensor hookup guide (powering it from a pin, readings, calibration)
- SparkFun soil moisture sensor (SEN-13322)
- Adafruit: plastic water solenoid valve, 12 V, 1/2" (product 997)
- FPD180A301 water inlet valve specification (Adafruit 997)
- K6JCA: schematic of a 1-channel relay module with high/low trigger
- Vishay 1N4001 to 1N4007 datasheet
- Arduino: analogRead()
- Arduino-ESP32: ADC (12-bit readings by default)

