**4. Software Design**

#### *4.1. Software Design of the MCU*

To reduce power consumption, in addition to selecting low-power devices in the hardware design, the key modules also adopted an efficient power managemen<sup>t</sup> algorithm in the software design of the MCU. The working voltage of the soil moisture sensor device, which was one of the main energy consumption components of the system, was high. However, in practical applications, the sensor device does not need to work continuously for a long time. In a data acquisition and transmission cycle, it would be idle most of the time. Therefore, a 12 V power supply that enabled control was designed for the hardware. When the sensor device worked and effectively outputted data, the MCU controlled the MOS tube to be in the cut-off state and turned off the power supply of the sensor device to avoid continuous power consumption after data acquisition. The LoRa RF communication module was also a main energy-consuming unit, and the working currents corresponding to 5 dB and 20 dB transmission powers were 75 ma and 130 mA, respectively. When the module did not need to work, the MCU would put it to sleep.

The working flow of the MCU software is shown in Figure 4. After the initialization of the MCU and each module, the MCU controlled the MOS tube to be in the cut-off state, turned off the sensor power supply to reduce energy consumption, enabled the MCU to interrupt, and then entered the low power consumption mode. The timer interrupt function was used to realize the acquisition of sensor data, and the MCU timer could automatically overload the time constant, thereby accurately controlling each module to complete different tasks during different timer cycles (250 ms).

**Figure 4.** Software flow diagram and timer interrupt function.
