Smart Irrigation Using the Pi-oT 2

Smart Irrigation IoT with the Pi-oT 2

In this tutorial, we will be using the Pi-oT 2 to control a smart irrigation system with independent water outputs.

The goal of this project is to build a foundation of a watering system that responds to the soil moisture levels and can independently control the watering of each plant.

Using IoT to control irrigation provides growers with many benefits. As an example, we can improve plant health, increase yields, mitigate damage from over/under watering, and even save money by eliminating excess water usage. All of these benefits are realized by a simple feedback system that works on a per plant basis. In contrast, most traditional irrigation controllers work on a simple timer basis, with no actual feedback from the soil it is watering.

Above is a diagram of how we can connect pumps and moisture sensors to the Pi-oT 2 in order to set up our irrigation system.

 

For this tutorial, we're using a resistive moisture sensor which outputs a voltage relative to the moisture level. While these types of sensors are not ideal for exact moisture measurements, they're ideal for relative measurements, and will help us determine if our plant's soils are above or below the required moisture content.

To control the watering, we can use 5V submersible pumps to control the water flow. These will be placed in a water reservoir and will pump the water directly to the plants via commonly found irrigation drip lines. Alternatively, one could use normally closed solenoid valves connected directly to a typical garden hose.

To begin, we will import our libraries to control the digital outputs and analog inputs of the Pi-oT 2.

import RPi.GPIO as GPIO
from time import sleep
from gpiozero import MCP3208

 

Since the Pi-oT 2 has 6 open collector outputs, we can control up to 6 individual water pumps. The diagram below shows an example of how the Raspberry Pi GPIO can be used to control a larger current circuit via the open collectors.

Open Collector Raspberry Pi

Next we will set up our GPIOs to control the Digital Outputs. Each open collector output is controlled by a corresponding GPIO, outlined below.

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.OUT) #Digital Output #1
GPIO.setup(20, GPIO.OUT) #Digital Output #2
GPIO.setup(21, GPIO.OUT) #Digital Output #3
GPIO.setup(26, GPIO.OUT) #Digital Output #4
GPIO.setup(19, GPIO.OUT) #Digital Output #5
GPIO.setup(13, GPIO.OUT) #Digital Output #6

 

To read the sensor outputs, we use an Analog to Digital Converter, or ADC. An ADC converts an analog signal, like a 0-5V sensor output, to a digital value which we can interpret in our python script.

We can assign the appropriate ADC channel using the code below. The gpiozero module has support for the MCP3208 ADC which is used on the Pi-oT 2. The gpiozero module will map the ADC reading to a value of 0 to 1, in our case, 0 meaning '0' volts and '1' meaning 5 volts.

adc0 = MCP3208(channel = 0) #Analog input #0

 

We're now ready to make the logic of our system. The below function is provided as a starting point for your use case. We first enter an IF statement depending on the moisture level as read by the sensor. In our case, we chose a value of '0.3', or 1.5 volts.

Note that you should test your soil and adjust for an ideal moisture level given your plants needs, and that a reading of 1.5 volts is an arbitrary number chosen for this example.

We then turn out pump on by setting our corresponding GPIO to high with the 'True' value. The sleep function is used to allow the pump to run for 2 seconds, in this case, where it is turned back off, and we wait another 2 seconds before repeating the loop.

If the soil moisture is above our threshold, we print a confirmation and again wait 2 seconds to repeat the loop.

This loop will run endlessly unless exited with a keyboard interrupt.

def main():
	try:
		while True:
			if adc0.value > 0.3: #watering threshold
				moisture = adc0.value
				print(moisture)
				print('watering')
				GPIO.output(16, True) #turn pump on
				sleep(2) #water length
				GPIO.output(16, False) #turn pump off
				print('watered')
				sleep(2)
			else:
				print('soil saturated')
				sleep(2)
				moisture = adc0.value
				print(moisture)

	except KeyboardInterrupt: # If CTRL+C, exit cleanly:
		GPIO.cleanup() # cleanup GPIOs

 

From here, this python program can be tweaked to add more complicated logic, more advanced sensor sampling techniques, and even cloud control. You can access the full python program on our Github, if you like this tutorial don't forget to give the repo a star!

If you're interested in learning more about the Pi-oT 2 and it's capabilities, you can read more on its product page or the quick-start guide.

 

Leave a comment