In today’s fast-paced life, keeping a potted plant can help relieve stress and anxiety. Many people grow flowers or green plants at home or in the office to purify the air and cultivate a sense of calm. However, due to business trips, travel, or other reasons, plant lovers are sometimes unable to take care of their plants in time. In the short term, this may affect normal plant growth; in the long term, it may even cause the plants to die.

Plants, just like pets, require careful care. They are extremely sensitive to soil moisture. If the soil moisture is too low, plants may suffer from dehydration; if it is too high, the roots may rot. So how can we water plants properly? In this lesson, we will use the UNIHIKER, a soil moisture sensor, and a water pump to build an IoT plant irrigation system. The system monitors soil moisture 24 hours a day and automatically irrigates plants, effectively solving the problem of not being able to water them in time.
Goal
Display soil moisture conditions on the UNIHIKER screen using two plant state images (Water Deficient and Healthy), and send real-time soil moisture data to the SIoT platform. To enable more user-friendly irrigation control, two control modes are provided: Manual Control and Automatic Control.
Manual Control: Use the “On” and “Off” buttons in the manual control area to turn the water pump on or off.
Automatic Control: Use the “+” and “-” buttons in the automatic control area to adjust the soil moisture threshold suitable for plant growth. When the detected soil moisture value is lower than the threshold, the system automatically turns on the water pump; otherwise, it turns the pump off.

Get to Know
1.Learn how to use the soil moisture sensor and understand its working principle
2.Learn how to use the relay and water pump and understand their working principles
Materials List
Hardware:

Software:
Mind+ Programming Software x1(download at https://mindplus.cc/en)

Hands-on Practice
In this lesson, you will learn how to use the soil moisture sensor, relay, and water pump, and then build an IoT plant irrigation system using these components. The system will be completed through three tasks.
Note: Task 2 and Task 3 implement two independent control functions.
Task 1: Detect Soil Moisture and Control the Water Pump
In this task, you will mainly learn how to use the soil moisture sensor to obtain soil moisture values, and how to use a relay to control the water pump.
Task 2: Build a Manual Control System
Use the “On” and “Off” buttons to turn the water pump on or off.
Task 3: Build an Automatic Control System
Use the “+” and “-” buttons to adjust the soil moisture threshold. The system will compare the real-time soil moisture value detected by the soil moisture sensor with the threshold. When the soil moisture value is lower than the threshold, the water pump is turned on automatically; otherwise, the pump is turned off. Finally, the detected soil moisture value and the control status will be sent to the SIoT platform.
Task 1: Detect Soil Moisture and Control the Water Pump
1.Hardware Connection
The soil moisture sensor is a simple sensor used to detect moisture in soil. A relay is an electronic component commonly used in automatic control circuits and can be regarded as an “automatic switch.” Connect the soil moisture sensor to pin P21 of the UNIHIKER.
Connect the relay to pin P24 of the UNIHIKER. Connect the water pump to the VOUT terminal of the relay, and connect the battery box to the VIN terminal of the relay. After completing the hardware connections, use a USB cable to connect the UNIHIKER to the computer.
Note: For details about the soil moisture sensor, relay, and water pump, please refer to the Knowledge Base.

2.Software Preparation
Open Mind+ and follow the diagram below to complete the software preparation process.

3.Write a Program
This task focuses on learning how to read soil moisture values and how to control the water pump using a relay.
(1) Reading the Soil Moisture Sensor Value
The soil moisture sensor module is marked with “A”, indicating that it is an analog sensor.

To read the analog signal from the soil moisture sensor, use the “Read analog pin” block and select pin P21.

Then use the “show text” block to show the analog value detected by the soil moisture sensor on the UNIHIKER screen.

(2)Controlling the Water Pump with a Relay
After connecting the relay to pin P24, how can we use it to control the water pump?
The relay module is marked with “D”, indicating that it is controlled by a digital signal.

Use the “Add button” block to add two buttons labeled “On” and “Off” on the UNIHIKER screen, and set their click callback functions to button_click1 and button_click2, respectively.

Click the ON button on the UNIHIKER to set the relay to output a HIGH level; click the Off button to set the relay to output a LOW level. Therefore, “when the click callback function button_click1 is triggered,” use the “Set Digital Pin” block to set pin P24 to output a HIGH level. “When the click callback function button_click2 is triggered”, use the “Set Digital Pin” block to set pin P24 to output a LOW level.

4.Run the Program
Click Run. After the program runs successfully, the UNIHIKER displays the analog value detected by the soil moisture sensor in real time.
And then, click the ON button on the UNIHIKER: the indicator on the relay module lights up and the water pump starts working. Click the OFF button: the indicator turns off and the water pump stops working.
Notes:
1.Make sure the switch on the battery holder is set to ON.
2. To avoid damage, the water pump must be placed in water and must not run dry.
3. For information about the NO and NC terminals on the relay module, refer to the Knowledge Base.

5.Have a try
Can we control the water pump based on soil moisture values?
For example: When the soil moisture value is less than 500, turn on the water pump. When the value is greater than or equal to 500, turn off the water pump. Try to implement this logic by yourself!
Task 2: Build a Manual Control System
1.Write a Program
Before writing the program, let’s first analyze what functions need to be implemented in this task. First, design the overall UI on the UNIHIKER screen. Next, set up the plant status display area, which switches between the Healthy and Water Deficient images based on the value detected by the soil moisture sensor. Then, create the control status display area. Finally, build the manual control panel, which includes the manual control button, a pump status display, and ON/OFF buttons to control the water pump.

(1) Overall UI Design
To simplify the program, static elements such as frames and fixed text are integrated into a background image. First, import the required images from the image resource library into the file system.

Use the “show image” block to show the background image on the UNIHIKER.

In the control status area, use the “show text” block to show Manual Control.

(2)Plant Status Switching
Use the “show image” block in the plant status area to show plant conditions using two images: Water Deficient and Healthy. If the detected soil moisture value is less than 500, update the image to Water Deficient. If the value is greater than or equal to 500, update the image to Healthy.

(3)Button-Based Water Pump Control
The manual control panel contains three buttons: the manual control button and the pump ON/OFF buttons. Use the “add button” block to add these buttons to the panel.

Below the manual control button, display “Water Pump” and initialize the pump status as off.

Click the ON button in the manual control panel to turn on the water pump. “When the click callback function button_click2 is triggered”, use the “Set Digital Pin” block to set pin P24 to output a HIGH level, and update the pump status to on.
Click the OFF button in the manual control panel to turn off the water pump. “When the click callback function button_click3 is triggered”, use the “Set Digital Pin” block to set pin P24 to output a LOW level, and update the pump status to off. The complete program is shown below:

2.Run the Program
Click Run. After the program starts: When soil moisture is less than 500, the Water Deficient image is displayed. When soil moisture is greater than or equal to 500, the Healthy image is displayed. Clicking ON turns on the pump and updates the pump status. Clicking OFF turns off the pump and updates the pump status.

Task 3: Build an Automatic Control System
1.Write a Program
Task 3 is an independent control system with the following functions:
- Adjust the soil moisture threshold using “+” and “-” buttons
- Compare the detected soil moisture value with the threshold
- Automatically control the water pump
- Upload soil moisture data and control status to SIoT

(1)Overall UI Design
The automatic control interface is almost the same as the manual control interface, except for an additional moisture threshold display. Therefore, you can modify the program from Task 2 directly.

(2) Threshold Adjustment Design
Make a new variable named “threshold” and initialize it to 500. Declare this variable as global within the callback functions button_click2 and button_click3.

Press “+”: increase the threshold by 1;Press “-”: decrease the threshold by 1. Use the Update Text Parameter block to display the updated threshold value.

In automatic control mode, how can automatic irrigation be achieved? Use the “if...then...”block to check whether the soil moisture value is less than the soil moisture threshold. If it is, turn on the water pump (set digital pin P24 to output a HIGH level) and update the pump status to on; otherwise, turn off the water pump (set pin P24 to output a LOW level) and update the pump status to off.

(3) Sending Data to the SIoT Platform
Long press the Home button on the UNIHIKER to enter the menu page, tap “Service Toggle”, and check whether the SIoT service is enabled. If it shows “Disabled”, tap the SIoT option and switch its status to “Enabled”.

To send data to the SIoT platform, corresponding sending blocks are required. However, such blocks are not available in the UNIHIKER’s default block list. What should we do? Click “Extensions”, find “MQTT-py” in the official library, and click it to complete the addition.

Complete the four steps of MQTT initialization – connection – staying connected – subscription. Here, you need to subscribe to two topics: “IrrigationSystem/moisture_value” and “IrrigationSystem/mode”.

Then, within the “forever” block, use the “MQTT Publish” block to send the soil moisture value detected by the soil moisture sensor to the “IrrigationSystem/moisture_alue”topic.
“When the click callback function button_click1 is triggered”, publish “auto” to the “IrrigationSystem/mode” topic. The complete program is shown below:

2.Run the Program
Click Run, then click the Automatic Control button to enter automatic mode. Use the “+” and “-” buttons to adjust the soil moisture threshold. When the detected value is below the threshold, the pump turns on automatically; otherwise, it turns off.

Log in to the SIoT platform to view real-time soil moisture data and the current control mode.

Knowledge Base
1. Working Principle of the Soil Moisture Sensor
The soil moisture sensor is a simple moisture sensor used to detect the water content in soil. The higher the water content in the soil, the higher the output value of the sensor; the lower the water content, the lower the output value. Therefore, the soil moisture sensor determines the level of soil moisture by measuring the amount of water present in the soil.
So how does a soil moisture sensor work?

As shown in the diagram, when the probe of the soil moisture sensor is suspended in the air, the circuit is in an open state, so the sensor outputs a value of 0. When the probe is inserted into the soil, different moisture levels in the soil result in different resistance values. The circuit becomes conductive, and the soil resistance is then converted into a voltage through the circuit, which is output as an analog soil moisture value.
2. Understanding the Relay
A relay is an electronically controlled device that can be understood as a switch driven by electricity rather than by hand. In this kit, a simple relay module is used, consisting of VIN, VOUT, and a control circuit.

The battery holder is connected to VIN, and the water pump is connected to VOUT.
By setting the control circuit to HIGH or LOW in the program, the pump connected to VOUT can be turned on or off.
The relay also supports NC and NO modes, which can be switched using a toggle switch:
- NO (Normally Open): disconnected when the relay is not powered
- NC (Normally Closed): connected when the relay is not powered
3. Understanding the Water Pump
How does the water pump work? Why does the pump start working when the relay outputs a HIGH level, and stop working when the relay outputs a LOW level? As an actuator, the water pump requires both a power supply and a control switch. The **battery holder** provides power to the water pump, while the relay acts as the control switch. Only through the relay can the water pump be turned on or off.

This pump is a DC water pump containing a DC motor. When the relay outputs a HIGH level, the pump is powered and the motor rotates. Water is drawn in through the inlet and discharged through the outlet, completing the pumping process.

Challenge
Task 2 and Task 3 are two independent control systems.
Try combining them into a single system that supports both Manual Control and Automatic Control at the same time. For example, place manual control on the left side of the UNIHIKER screen and automatic control on the right side.
Note: Corresponding background images are provided in the image resource library.









