Arduino UNO R4 - Automatic Irrigation System

We'll learn how to build an automatic irrigation system for your garden using Arduino UNO R4, a soil moisture sensor, a relay, and a pump.

Hardware Preparation

1×Arduino UNO R4 WiFi or Arduino UNO R4 Minima
1×Alternatively, DIYables STEM V4 IoT, Compatible with Arduino Uno R4 WiFi
1×Alternatively, DIYables STEM V4B IoT, Compatible with Arduino Uno R4 WiFi
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Capacitive Soil Moisture Sensor
1×Relay
1×12V Pump
1×Vinyl Tube
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino UNO R4
1×Recommended: Sensors/Servo Expansion Shield for Arduino UNO R4
1×Recommended: Breadboard Shield for Arduino UNO R4
1×Recommended: Enclosure for Arduino UNO R4
1×Recommended: Power Splitter for Arduino UNO R4
1×Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included)
1×DIYables STEM V4B IoT Starter Kit (Arduino included)
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 .

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

Arduino Uno R4 WiFi Compatible Boards

Overview of soil moisture sensor and Pump

To learn about the pump and soil moisture sensor (pinout, operation, programming), refer to the tutorials listed below:

Wiring Diagram

The wiring diagram between Arduino UNO R4 soil moisture sensor Pump

This image is created using Fritzing. Click to enlarge image

Jumper wires are good for a quick test, but not for a final build. You can stack a Proto Screw Terminal Block Shield or a Screw Shield V2 on the Arduino UNO R4 and connect the wires to its screw terminals. This is how it looks with the Screw Shield V2:

The wiring diagram between Arduino UNO R4 soil moisture sensor Pump Screw Shield

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Uno R4 and other components.

Arduino UNO R4 Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-automatic-irrigation-system */ #define RELAY_PIN 2 // The Arduino Uno R4 pin that connects to relay #define MOISTURE_PIN A0 // The Arduino Uno R4 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

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Adjust your equipment to find the point where it changes from wet to dry, following the instructions here: Arduino UNO R4 - Calibrates Soil Moisture Sensor.
  • Enter the updated value for this point into the program.
  • Open the Serial Monitor in the Arduino IDE.
  • Load the program onto your Arduino UNO R4.
  • Check the Serial Monitor to see what it displays.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
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
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2

Code Explanation

Check the explanations in the source code comments, line by line!

Relay vs MOSFET Module for the Pump

This project uses a relay to start and stop the pump. A 12V DC pump can also be driven by a MOSFET module.

Relay MOSFET Module
Pump control start and stop only start, stop, and change the flow (PWM)
Switching sound clicks none
Switching time slow (milliseconds) very fast
Wear the contacts get old with use no moving parts to wear
Works with AC and DC pumps DC pumps only
Pump supply voltage what the relay is rated for 6V or higher
Isolation yes yes, with a PC817 optocoupler
Choose it for AC pumps or plain ON/OFF DC pumps where you want to set the flow

For watering plants, the relay is enough. The MOSFET module is useful if you want a gentle flow, so the water does not wash the soil away. A pump has a motor coil inside, so add a flyback diode (1N4007 or 1N5819) across the pump when you use the MOSFET module. AC pumps need the relay.

To see how to wire and code a pump with both a relay and a MOSFET module, see the Arduino UNO R4 - Controls Pump tutorial.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!