Relay guides · On this page

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 %.

Ready
Open in Cirkit Designer

The soil

The sketch reads the soil once a second.

50 %

Arduino Uno serial monitor115200 baud

Output appears here when the simulation runs.

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.

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;

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);
}
  1. const int DRY = 400;  // start watering below this reading

    The reading below which the soil counts as dry. Set it from your own soil; see set the thresholds.

  2. const int WET = 600;  // stop watering at or above it

    The 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.

  3. 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.

  4. 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.

  5. if (soil < DRY) {
      watering = true;
    } else if (soil >= WET) {

    Under DRY, start watering; at WET or more, stop. Between the two, watering keeps its last value.

  6. 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 sketch's two thresholds on analogRead's 0 to 1023: below 400, DRY, the valve opens; at 600, WET, or more, it closes; between them it stays as it was. SparkFun's sensor reads about 0 in dry air and about 880 in water at 5 V.

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.

The relay, liveExample: pin 8 HIGH, 5.0 VRun the circuit at the top, and this follows the relay as the sketch switches it.
Across the input
5.0 V
Coil
Energized
COMNCNO

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
  1. 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.

    FromConnect toShow
    Sensor VCCUno pin 7Show
    Sensor GNDUno GNDShow
    Sensor SIGUno A0Show
    Relay DC+Uno 5VShow
    Relay DC−Uno GNDShow
    Relay INUno pin 8Show
    12 V supply +Relay COMShow
    Relay NOValve +Show
    Valve −12 V supply −Show
    1N4007Across 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.

  2. Get the code

    I'm using:

    Steps 2 and 3 follow the tool you pick.

    Open in Cirkit Designer

    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.

  3. 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
    Automatic plant wateringFileEditExportHelp
    DesignCodeSimulateBuild
    1Parts & purchasing2Assemble3Upload firmware
    Arduino UnoEdit Code

    Board

    Arduino Uno Change

    2

    Port

    Select PortCOM3 Connected

    3

    Upload

    Upload to DeviceUploading...Upload successful

    4

    Check that it works

    Your project should now run as designed.

    Upload output and serial monitor
    Compile / Upload Output
    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.
    Device SerialStop

    Read serial data from your device.

    Start Serial Monitor
    Listening on COM3 at 115200 baud
    Soil: 440, not watering
    Soil: 402, not watering
    Soil: 396, watering
    Soil: 512, watering
    Soil: 604, not watering
    Select port

    Select a device port

    Select the device port for uploading code and serial communications

    COM3 (Arduino Uno)
    1. Open Build
    2. Choose Upload firmware
    3. Click Select Port
    4. Pick COM3 (Arduino Uno)
    5. Click Upload to Device
    6. Open Upload output and serial monitor
    7. 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
    Select BoardArduino Uno

    Select other board and port

    Arduino UnoCOM3
    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 baud

    Compiling 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.

    1. Click the board menu
    2. Pick Arduino Uno on COM3
    3. Click Upload
    4. Wait for Done uploading
    5. Click Serial Monitor
    6. 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.

    Set the thresholds for your soil

    SparkFun's readings vary with the soil, so set DRY and WET from 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. Put DRY a little above the first and WET a little below the second, and upload again.

    Not working? See troubleshooting.

    Next guideUsing an ESP32? It has its own guideThe same relay module on an ESP32: why the jumper goes on H for a 3.3 V GPIO, and the wiring from its 5V pin.

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

  • 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

  • WET is above what your soil reads wet. Water it, read it, and set WET below 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