![How to build a smart irrigation system with the Particle M-SoM](https://i0.wp.com/blog.particle.io/wp-content/uploads/2024/07/IMG_0804.jpeg?fit=1350%2C1800&ssl=1)
The Problem
Whether it’s a sprinkler system at a golf course, a farm’s spray arm, or a drip watering setup in a greenhouse, irrigation is vital to how we grow plants. However, most approaches also introduce inefficiencies through either requiring human intervention, wasting water, or a combination of the two. It’s inefficient and often annoying to manage.
The Solution
This project aims to improve both of these metrics with the M-SoM and its IoT connectivity. In doing so, the Smart Irrigation System is able to not only look ahead at the forecast and avoid watering if it will rain soon, but also monitor and report live soil conditions through sensors. Once all of this data has been collected and analyzed, the System can then automatically activate specific valves until the optimal soil conditions are reached.
About the M-SoM
Because this system could be deployed away from a reliable WiFi connection, having onboard cellular connectivity was a must. The Particle M-SoM M404 was therefore a great choice, owing to its multi-radio WiFi, BLE 5, LTE-M, and GNSS capabilities- all in a compact M.2 form-factor. Its Realtek Arm Cortex-M33 CPU with 2MB of application flash and 3MB of available RAM also meant complex firmware could be deployed in the future without worrying about having to upgrade the processor.
An M.2 SoM Evaluation Board made things even more convenient by exposing a plethora of GPIO pins, built-in power regulation/LiPo battery charging, and even the potential for PoE support.
Additional hardware
Beyond the microcontroller are several additional components that facilitate the operation of four solenoid water valves and gather soil moisture information. First, there is a piece of perfboard containing a bank of four MOSFETs which can be switched natively by the 3.3V logic of the M-SoM.
There are also screw terminal connectors around the perimeter for attaching the solenoids, soil moisture sensors, and a hall-effect flow sensor that measures the instantaneous flow and total volume.
A screen at the front displays the system’s current state, including if a valve is active, if the M-SoM is connected to the Particle Cloud, and the percentage of soil moisture read by the sensors.
Building the garden and water tower
In order to demonstrate the platform, every valve sends its output to one of these 5×5-foot raised beds where the water can act as drip irrigation near each row of plants. Pumping water is expensive when it comes to energy usage and maintenance, so an elevated rain barrel acts as the pump by letting water accumulate and then freely flow downwards with the help of gravity. The tower itself is constructed from four posts set in concrete and then reinforced by a series of horizontal slats running around/between them.
Valves and power assembly
The power subsystem is split between low and higher-voltage devices, with the valves being sent current from the 12V LiPo battery and everything else running off the 3.7V LiPo cell connected to the SoM Evaluation board. Ordinarily, an irrigation system like this would source its power from the grid, but thanks to the attached solar panel and charge controller along with a battery backup, it can be deployed anywhere.
Particle Cloud integrations
Cloud connectivity is vital so that users can have uninterrupted insight into the system’s current readings and the ability to remotely activate specific valves when required. When the M-SoM boots, it will quickly initialize a Device-to-Cloud Ledger instance with the latest readings from the flow sensor, soil sensors, and valve activation values. From here, the Ledger instance will be updated by the device every minute to reflect any changes such as a new soil moisture percentage or a valve being automatically shut off.
When a user needs to turn a valve on or off manually, the device exposes a pair of Cloud Functions that each accepts the index of the valve being toggled. One important safety feature is the auto shutoff functionality where the controller will turn off a valve if it’s been active for more than five minutes. This is vital to avoid burning out the solenoids and wasting water if external control is suddenly unavailable.
Check out the full source code here.
The web application
The user-facing portion of the Smart Irrigation System is a Nuxt instance running both a server and a front-end Vue app. Every 15 seconds, the server queries the device’s Ledger instance to get the latest values and then promptly stores them in a couple PostgreSQL database tables. The other background process, which executes every five minutes, gets the current local forecast from the Weather.gov Weather API and checks if there is a decent chance of precipitation in the next six hours. If there is, the valves aren’t activated in anticipation of the upcoming rain. But if rain is not in the forecast, the soil sensors are checked to see if the soil is dry enough to warrant a watering cycle. All of this information is exposed on the web page as well as a widget for each valve to view its current metrics and remotely activate it with the press of a button.
Through this combination of wireless LTE-M connectivity, data collection, and forecasting, the System makes irrigation far more efficient than traditional methods by anticipating future rainfall and eschewing grid-based power for solar and a gravity-fed water source.