Blog Post

AI-Managed Crop Irrigation

Machine LearningCrop irrigation with the Arduino Edge Control and Edge Impulse ML helps maximize crop yield while minimizing solid degradation and water waste.

Evan Rust

October 6, 2021

Read the Problems with modern farming practices sectionProblems with modern farming practices

The ability to grow massive amounts of food in recent times has only been possible with the advent of pesticides, mechanization, and using a lot of water. However, these practices are often unsustainable, thus ruining the environment and depleting valuable resources in the process. This project is meant to tackle water usage, as being able to pinpoint exactly where water is needed can prevent over-irrigation. 

Read the The advantages of a machine learning-based system sectionThe advantages of a machine learning-based system

Using a system that relies on machine learning can be far more adaptable compared to one that solely relies on hard thresholds or multiple linear regressions. Additionally, improvements can be made over time as more data is collected. By deploying models at the edge rather than on the cloud, a constant internet connection isn’t required, thereby allowing each device to function independently in a decentralized manner.

Read the What’s the hardware required? sectionWhat’s the hardware required?

This project utilizes an Arduino Edge Control board as its main controller, which has the following specifications:

  • An Arm Cortex-M4F processor (nRF52840) clocked at 64MHz with 1MB flash and 256KB RAM
  • 16x hydrostatic watermark sensor inputs
  • 8x latching relay command outputs with drivers and 8x without drivers
  • 4x 60V/2.5A solid state relays
  • Dual MKR sockets
  • Powered by a 12V SLA battery that can be recharged directly from solar panels

All of these features make the Arduino Edge Control extremely capable for smart agriculture applications. This project also makes use of an Arduino MKR WiFi 1010 board for sending telemetry to a local server, although it could be stored locally on the microSD card for future retrieval too. There’s a DHT22 temperature/humidity sensor attached to the MKR WiFI 1010 since an LCD screen takes up the other digital pins. 

Read the Collecting, sending, and storing telemetry sectionCollecting, sending, and storing telemetry

When the Arduino Edge Control first boots up, peripherals such as the LCD, watermark sensor, and MKR slot 2 are initialized. Then within the loop function, both the watermark and battery level values are read repeatedly and then sent to the MKR WiFi 1010 via I2C. Over on the MKR board there’s a handler for the I2C bus that gets called whenever new data is received. Once parsed back into a SensorValue object, this data is combined with readings from the DHT22 and formed into a serialized JSON string, which is sent as a POST request to a local server.

I wrote this server as a simple Python Flask application that holds a single endpoint called /gardeneEdge and accepts POST requests. After the JSON object is parsed from the incoming string, data is extracted and then stored within a CSV file for later analysis and training.

Read the Training a machine learning model sectionTraining a machine learning model

The Edge Impulse Studio offers a way to directly upload data from a CSV file, so I opted to go that route. My dataset consisted of 25 total samples that varied between 11 and 13 seconds in length with each one having the following headers:

  • Timestamp (time offset in milliseconds)
  • Watermark sensor reading
  • Battery level (can be used to determine night or day)
  • Temperature in degrees celsius
  • Relative humidity

With the data split almost evenly between needing water and not needing water, I moved onto designing the Impulse. The first block is a simple Time series data block that automatically split the samples into one-second chunks. Next is the raw data block that takes each sample’s axes and creates features from them that can be used to train the model. The Keras neural network was trained on these input features from the previous block and outputs the label it thinks corresponds with the current sensor readings - either water or no water. 

I was able to achieve an accuracy of 100% in both model training and the separate model validation page. To view and download this model yourself, you can visit visit its public link here.

Read the Deploying to the outdoor device sectionDeploying to the outdoor device

Getting the model from Edge Impulse to onboard the device was quite easy due to the ability to export it as an Arduino library. Once it was included in the original program, I made a couple of modifications that read in new data from the MKR 1010 WiFi, passed it to the model for inference, and then displayed the result. To further expand this project one might add a solenoid water valve and drive it from the Arduino Edge Control board using one of its solid state relays that can automatically water plants if needed.

Subscribe

Are you interested in bringing machine learning intelligence to your devices? We're happy to help.

Subscribe to our newsletter