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:
- Arduino Nano will use the soil moisture sensor to detect when the soil is dry and then activate the relay to switch on the pump for watering the plants.
- When the soil moisture is sufficient, Arduino Nano will control the relay to switch off the pump.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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.
Additionally, some of these links are for products from our own brand, DIYables.
Additionally, some of these links are for products from our own brand, DIYables.
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:
- Arduino Nano - Soil Moisture Sensor tutorial
- Arduino Nano - Controls Pump tutorial
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Arduino Nano Code
/*
* This Arduino Nano code was developed by newbiely.com
*
* This Arduino Nano code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-nano/arduino-nano-irrigation
*/
#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 530 // 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.
COM6
The soil is DRY => turn pump ON
The soil is DRY => turn pump ON
The soil is DRY => turn pump ON
The soil is DRY => turn pump ON
The soil is WET => turn pump OFF
The soil is WET => turn pump OFF
The soil is WET => turn pump OFF
The soil is WET => turn pump OFF
Autoscroll
Clear output
9600 baud
Newline
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!