Homemade Garden Irrigation System From A Simple Relay Module

An excellent example of home automation is a self-operating irrigation system. Whether for a large garden or a small flower pot, you never want to do it manually. Fortunately, creating a system that handles this task autonomously with additional remote control and telemetry capabilities is straightforward with unicontrol when coupled with a standard ESP8266-based relay module.

Consider the following target:

  • Four independent lines (a common strategy for larger areas due to limited water pressure)
  • The system is programmed to automatically open each line successively for 10 minutes starting at 5:00 AM, but only if certain conditions are met:
    • There has been no recent rainfall
    • The soil moisture is below a predetermined threshold
  • Additionally, the system supports a physical button for manual control. Pressing this button will:
    • Initiate the complete 4-line cycle if it is not running
    • End the cycle if the last line is active
    • Proceed to the next line otherwise

The ESP12F-X4 relay module introduced here, in conjunction with suitable sensors, can effectively perform this task.

Budget

ItemPcs Unit price
(€)
ayatec unicontrol mini18
ESP12F 4-Channel relay module18 – 20
Soil moisture sensor (analog output)11
Infrared obstacle sensor module11 – 2
Jumper cables1< 1
Resistor – 1x10k, 1x 100k, 1x 220k1 < 1
Rain sensor (logical output)15 – 40
0.96” TFT Display (optional)11 – 2
Push button1< 1

Depending on the chosen parts, you may expect cost of this setup in the range of €25 – €70.

X4 Relay Module Peripherals
X4 Relay Module Peripherals
Examples of available analog soil moisture sensors
Examples of available analog soil moisture sensors
Common rain sensors with logical output signal
Common rain sensors with logical output signal

Module Assembly

Normally, you should avoid using D3 and D4 pins as inputs if they can unintentionally assume a LOW state at boot, as this would cause a boot failure. A push button with a normally open state, for example, does not violate this.

When printing the board’s enclosure, do not forget to open the pre-cut holes for the connector and, possibly, the display. Check the dedicated article for more details.

Start by attaching the connector and the optional display to the upper part of the enclosure. Using a glue gun is usually sufficient.

The most significant challenge of this assembly, however, is managing the inputs. Given that the X4 relay module lacks ready-to-use input connectors or a voltage divider for the analog input, these elements need to be prepared ahead of the final assembly.

Analog input:

As per the schematics and accompanying images, solder the 10k, 100k, and 220k resistors to the ADC, 3V3, and GND pins, respectively.

To improve sturdiness and prevent unwanted contacts, apply a glue gun or a suitable type of resin.

You may notice four resistors soldered in the gallery, contrary to the three in the schematic. This is because a 220k resistor was unavailable, so I used a series composed of a 200k and a 20k resistor instead.

Now, connect the sensor’s legs to Pin 1 (A0) and Pin 4 (GND) of the connector, respectively. The direction of the sensor is not relevant.

Digital inputs:

The remaining two pins on the external connector are linked to the digital inputs D3 (IO0) and D7 (IO13). Though their connection is straightforward, you may need to solder their respective cables directly onto the connector, as you might not find enough space to simply plug in the jumper cables.

As the button is present on the D3 pin, it is critical that you use a button with a normally open state. Otherwise, the relay module will not boot if the button is unpressed following any restart. Connect one leg of the button to Pin 2 of the connector (D3) and the other to Pin 4 of the connector (GND). Direction is not relevant.

Pin 3 of the connector (D7) expects one leg of the rain sensor (with the other leg on connector’s Pin 4). The sensor is expected to close the circuit in case of recent rain and remain open otherwise (the NO type). If your sensor’s logic is opposite, you will need to reflect this in the setup of the process Autostart (see below). Again, the direction of the sensor should not be relevant. You should, however, check your sensor’s manual for confirmation.

Display:

This is as simple as it gets. The display only requires four F-F jumper cables, connecting the following display and on-board pins:

  • VCC to 3V3 or 5V
  • GND to GND
  • SCL to IO5
  • SDA to IO4

Setup

The following sections will assume your ESP8266 has been successfully flashed and accessed. For instructions on flashing the board with the unicontrol software, you can refer to the introductory tutorial specifically written for this module. Afterward, you can follow the First boot tutorial to set up the Wi-Fi and establish a connection to the device.

If you do not wish to manually set up your device, you can download the backup files with settings used in this tutorial from the link below:

For guidance on working with the backup files, please refer to our user guide. Please, make sure that you import all files.

Peripherals

Once you’ve accessed the module via the Interface, navigate to the Peripheral menu and ensure that:

  • D3 and D7 are set as Logical IN
  • A0 is set as Analog
  • D0, D5, D6, and D8 are set as General OUT (relays)

Having a Display on D1 and D2 is optional, but it greatly enhances the user experience.

D0 is HIGH at boot, meaning the connected relay will close for a brief moment during each reboot. If this is not acceptable, you may need to use a different pin.

Also, don’t forget to Edit the A0’s Mapping if needed. This will determine the translation of the sensor’s output voltage into values used by the processes. For detailed information on how to accomplish this, refer to our user guide.

Peripherals set-up
Peripherals set-up
A0 Edit window
A0 Edit window

Memory slot

To keep track of the cycle so that the button functions properly, we need to use a Memory slot that will determine the course of action for other processes. The slot Mem 1 will assume following values:

  • 0 – the cycle is off
  • 1 – the cycle is on with the active first line
  • 2 – the cycle is on with the active second line
  • 3 – the cycle is on with the active third line
  • 4 – the cycle is on with the active fourth line

For this purpose, create two processes:

These two processes will be invoked as secondary outputs by other processes and will manage the recording of the cycle’s progress. MemINC will add 1 to the memory slot Mem 1, while MemRST will reset it back to 0. The current state of Mem 1 will then be used as conditions in the main processes.

Auxiliary Mem processes
Auxiliary Mem processes

When defining auxiliary processes, which are intended solely for use as Secondary outputs by other processes, it’s typical not to use any Constraint, Input, or Event.

Pump sequence

The core of the relay module setup, however, will be a sequence of processes controlling the pump relays (we will use the sequencing technique described here). Start by creating four processes and name them Line_1 through Line_4. For each of them, set:

Additionally, set the Secondary outputs as follows:

ProcessSecondary outputResponse
Line_1Mem_INCOff -> On
Line_1Line_2Off -> On
Line_2Mem_INCOff -> On
Line_2Line_3Off -> On
Line_3Mem_INCOff -> On
Line_3Line_4Off -> On
Line_4Mem_RSTOff -> On
The setup described above will result in the following behavior:
  • None of the processes can be initiated on their own. They must be started by a user or another process.
  • Each process runs for exactly 3 seconds. Upon turning itself OFF, the Mem 1 memory slot is incremented by 1 and the next process is turned ON in the case of the Line_1, Line_2, and Line_3 processes. In the case of the Line_4 process, the Mem 1 is reset to 0 and no further action is taken.
Example of the line process setup.
Example of the line process setup.

With the described setup, pressing On in the Line_1 process will result in the following workflow:

A mock-up installation of the relay module used for testing and demonstration.

Button

While the physical button is not crucial for the setup, it enhances the user-friendliness of the irrigation system by providing immediate and simple physical access to perform virtually any task. As there is only one available button due to limited inputs, we need to differentiate the button’s function based on the current Mem 1 value. This can only be achieved by creating five independent processes, constrained by Mem 1. Name them Button_0 through Button_5.

For these processes, use the following setup (with the rest of the settings kept at their default values, as usual):

As the button is present on the D3 pin, it is critical that you use a button with the normally open state. Otherwise, the device will not boot with the button unpressed following any restart.

Please note that the Mem constraint conditions need to be expressed as inequalities. If you need to input an equality, you can achieve the same using two inequalities (see below examples).

Button process example
Button process example
Button_0
Button_1
Button_2
Button_3
Button_4

Autostart process

So far, the cycle can only be launched manually. To automate it, we need to add a process that will initiate the Line_1 process each morning at 05:00 if there was no recent rainfall and the soil is not wet. To achieve this, create the following process (default otherwise):

This process represents the core of the automation itself, with three key inputs – timer, variable constraint, and main input. Based on your own preferences, you may define different times when the irrigation should be launched (up to four independent time windows or even more with another process), the D7 constraint (defining the value representing recent rainfall according to your sensor), and the A0 condition representing the soil moisture threshold.

Additionally, if you wish to collect telemetry on soil moisture, you may also set set following in the Input section:

There are many combinations of how the functionalities of the Autostart process could be achieved or further improved. The description here is just one of many options.

MQTT setup

As usual, I will be using my preferred testing tools, the test.mosquitto.org broker and the IoT MQTT Panel app, with the following placeholder settings in the Wireless/MQTT menu:

  • Connection: Enabled
  • Broker: test.mosquitto.org
  • Port: 1883
  • User: empty
  • Topic Level 1: johndoe5896
  • Topic Level 2: garden
  • Topic Level 3: irrigation

Please bear in mind that the broker detailed above is only suitable for testing purposes and you should choose a more secure option for operation. Also, do not forget to customize the placeholder topic root johndoe5896/garden/irrigation to minimize the risk of interference with other users.

You may send/receive following useful messages:

  • ON button – sends Payload 1 to the Topic johndoe5896/garden/irrigation/sub/Line_[X]/outset
  • OFF button – sends Payload 0 to the Topic johndoe5896/garden/irrigation/sub/Line_[X]/outset
  • Relay State LED – receives the current relay state from Topic johndoe5896/garden/irrigation/pub/Line_[X]/output with Payload 0 (relay off) or 1 (relay on)
  • Progress bar – receives the current Mem 1 value from Topic johndoe5896/garden/irrigation/pub/MEM1/output with Payload0 (relay off) or 1 (relay on)
  • Moisture data – if the Moisture process is used, you may receive the soil moisture level as a Payload from Topic johndoe5896/garden/irrigation/pub/Autostart/input
  • Device State LED – receives the current device state from Topic johndoe5896/garden/irrigation/pub/conn with Payload 0 (offline) or 1 (online)
  • Device IP address – receives the device’s current local network IP address as a Payload from Topic johndoe5896/garden/irrigation/pub/ip

Although the irrigation setup is fully automated and does not require intervention from the user, you may still find very useful to continuously monitor the telemetry or occasionally access it manually. You can check our user guide for a full reference on MQTT protocol options.

Smartphone dashboard example
Smartphone dashboard example
Example of a simple NodeRED dashboard constructed from the MQTT telemetry described above
Example of a simple NodeRED dashboard constructed from the MQTT telemetry described above

Finalizations

Finally, once everything is tested and checked, your brand-new relay module can be installed in its dedicated place.

Below, you can download the backup files with settings used in this tutorial:

For guidance on working with the backup files, please refer to our user guide. Please, make sure that you import all files.

Were you successful in creating your own homemade irrigation system?

Ideas for further improvement

1

With more advanced hardware like our in-house PCB, replacing the standard relay module, you may combine this also with the rain tank level monitor, its automatic refill and additional fail-safes to prevent pumps from running dry.

2

The PRO version of the software will let you define much more pre-set schemes of irrigation, differentiated by, for example, ground moisture or temperature. Additionally, the PRO version will let you access the relays directly, without the necessity to defined special processes.

3

When using the relay MQTT telemetry in NodeRED, you can easily keep track of the past cycles or estimated water consumption.

Leave a Comment