Arduino Nano - Irrigation

This tutorial instructs you how to construct an automated irrigation system for your garden by using Arduino Nano, a soil moisture sensor, a relay, and a pump. Specifically:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Capacitive Soil Moisture Sensor
1×12V Pump
1×Vinyl Tube
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Buy Note: Many soil moisture sensors available in the market are unreliable, regardless of their version. We strongly recommend buying the sensor from the DIYables brand using the link provided above. We tested it, and it worked reliably.

Overview of soil moisture sensor and Pump

If you are unfamiliar with the pinout, functionality, and programming of a pump and soil moisture sensor, the following tutorials can help:

Wiring Diagram

The wiring diagram between Arduino Nano and soil moisture sensor Pump

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code

/* * This Arduino Nano code was developed by * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * */ #define RELAY_PIN 2 // The Arduino Nano pin that connects to relay #define MOISTURE_PIN A6 // The Arduino Nano pin that connects to AOUT pin of moisture sensor #define THRESHOLD 100 // CHANGE YOUR THRESHOLD HERE void setup() { Serial.begin(9600); pinMode(RELAY_PIN, OUTPUT); } void loop() { int value = analogRead(MOISTURE_PIN); // read the analog value from sensor if (value < THRESHOLD) { Serial.print("The soil is DRY => turn pump ON"); digitalWrite(RELAY_PIN, HIGH); } else { Serial.print("The soil is WET => turn pump OFF"); digitalWrite(RELAY_PIN, LOW); } Serial.print(" ("); Serial.print(value); Serial.println(")"); delay(500); }

Detailed Instructions

  • Calibrate the wet-dry THRESHOLD by following the instructions in Arduino Nano - Calibrates Soil Moisture Sensor.
  • Afterwards, update the calibrated THRESHOLD value in the code.
  • Open the Serial Monitor on the Arduino IDE and upload the code to the Arduino Nano.
  • Finally, view the result on the Serial Monitor.
The soil is DRY => turn pump ON (12) The soil is DRY => turn pump ON (21) The soil is DRY => turn pump ON (41) The soil is DRY => turn pump ON (64) The soil is WET => turn pump OFF (265) The soil is WET => turn pump OFF (311) The soil is WET => turn pump OFF (365) The soil is WET => turn pump OFF (375)
Autoscroll Show timestamp
Clear output
9600 baud  

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Video Tutorial