[table id=3 column_widths=”50%|50%”/]
Why use a Raspberry Pi?
I wanted to add one more “zone” to my garden sprinkler system, that means one more separate pvc line that has its own electrically controlled faucet. But… the off-the-shelf sprinkler computer I had purchased a year earlier had no more open ports…
So what to do…buy a new one with more ports?? Those cost about $110 and controlling sprinkler valves is all they can do. Hmmm, I had one Raspberry Pi lying around ($35 or $5 for a Raspberry Pi zero) and for about $10 I could buy 2x 8-output relays, which means I could control 16 valves in my garden – more than I would probably ever need — and you can control so much more with a Raspberry Pi — even remotely when you are on a trip far away…
So what is required besides the standard garden sprinkler system:
- A Raspberry Pi with Raspbian (or Ubuntu) operating system on an 8GB (or lager) SD card.
- Two 8-output relays (for example this).
- A box to put everything into (for example this).
- A few ribbon cables (such as these).
- Optional: I mounted this fake security camera housing on the box and put a web cam inside of it, which is connected to the Raspberry Pi. In the post
Simple Home-Surveillance with OpenCV, Python and Flask on Raspberry Pi you can find a code that lets you use the camera. The highlight in this post is the Flask code, that lets you steam the video from your webcam on web browsers. If you have the right credentials you can see the video stream using your smartphone or any computer almost wherever you are.

Raspberry Pi based sprinkler control and home surveillance system.
For the garden sprinkler system you will need
- Anti-Siphon Valves such as these.
- PVC pipes such as these.
- Several spray-heads such as this (mostly not more than 5 per valve/zone or the pressure will be too low).
- A sprinkler cable like this with as many conductors as possible, and it is a good idea to have the cable through a pipe such as this.
Here a short video about the setup of the sprinkler system:
[embedyt] http://www.youtube.com/watch?v=RZMUHU1UHHw[/embedyt]
The hardest part of this project is certainly the digging of the trenches in which the PVC pipes get hidden from sight. But let us concentrate here only on the control part of the valves.
Following is a schematic of how to wire up the Raspberry Pi with the 8-port relays. I was using 2 relays but show here only one. You can use also smaller relays if you are sure that you won’t need more switches.
To control the GPIO pins as shown in above image, you need to install following packages:
sudo apt-get install python-rpi.gpio python-gpiozero
Below is the simple Python script to control the relay(s) for the sprinkler system:
# import the necessary packages import RPi.GPIO as GPIO import time # set the GPIO mode GPIO.setmode(GPIO.BCM) zone = [8, 7, 10, 9, 14, 15, 18, 23, 24, 25] ################################################# # List of zones with the times for each zone #### ################################################# #8 = House front left, Large tree, at walk way s8 = [zone[0], 200] #7 = House front left, Large tree, at garage wall s7 = [zone[1], 200] #10 = right corner of backyard behind shed s10 = [zone[2], 200] #9 = Backyard cherry trees s9 = [zone[3], 200] #14 = Orange Trees, Plum trees, Apple tree.. s14 = [zone[4], 200] #15 = Front right stripe s15 = [zone[5], 200] #18 = Cypresses left s18 = [zone[6], 200] #23 = planting boxes s23 = [zone[7], 200] #24 = Roses house wall s24 = [zone[8], 150] #25 = House wall at AC s25 = [zone[9], 150] def openCloseValves(valve, zeit): print("Opening Valve",valve," for ",zeit," seconds") time.sleep(5.0) GPIO.setup(valve, GPIO.OUT) GPIO.output(valve, GPIO.LOW) # Open valve time.sleep(zeit) GPIO.output(valve, GPIO.HIGH) # Close valve time.sleep(5.0) print("done...") def run(): openCloseValves(s8[0],s8[1]) openCloseValves(s7[0],s7[1]) openCloseValves(s10[0],s10[1]) openCloseValves(s9[0],s9[1]) openCloseValves(s14[0],s14[1]) openCloseValves(s15[0],s15[1]) openCloseValves(s18[0],s18[1]) openCloseValves(s23[0],s23[1]) openCloseValves(s24[0],s24[1]) openCloseValves(s25[0],s25[1]) # perform a bit of cleanup GPIO.cleanup() if __name__=='__main__': try: run() except: # Shut all valves... for num in zone: GPIO.setup(num, GPIO.OUT) GPIO.output(num, GPIO.HIGH) GPIO.cleanup() print("An Error occured OR user stopped routine...!!!") raise
This python routine starts the vales connected to the Raspberry Pi pins with the numbers listed in the array “zone” (8, 7, 14, 15, 18, 23, 24, 25), in the given order one after the other with a delay of 10sec between one valve closing and the next starting. Additionally “hard-coded” are here the watering times of the individual valves saved in the arrays s8 to s25. In a larger program it would make sense to put the numbers in a separate file that is read by the python script at execution.
To run this script at certain times of the day and certain days of the week a simple solution is to put a call in crontab.
For this I opened up a sudo crontab with
$ sudo crontab -e
First to make sure that all sprinklers are shut off in case the Raspberry Pi is rebooted and not one of them left in an “undefined” (leaking) state I added in the crontab the line:
@reboot python3.4 sudo /home/ubuntu/sprinkler/shutSprinkler.py
(Note that I was using for this script python3.4. Yours might be some other python version, but if version is higher 2.7 results should be same.)
The shutSprinkler.py script is a small script that just setts all pins of the Raspberry Pi in a HIGH state, which shuts off the relays that control the valves:
# import the necessary packages import RPi.GPIO as GPIO import time #import cv2 # load the input image and display it to our screen #print("click on the image and press any key to continue...") #image = cv2.imread("hoover_dam.jpg") #cv2.imshow("Image", image) #cv2.waitKey(0) #print("moving on...") # set the GPIO mode GPIO.setmode(GPIO.BCM) # loop over the LEDs on the TrafficHat and light each one # individually for i in (14,15,18,23,24,25,8,7,2,3,4,17,27,22,10,9): GPIO.setup(i, GPIO.OUT) time.sleep(0.1) GPIO.output(i, GPIO.HIGH) # perform a bit of cleanup GPIO.cleanup()
Finally back to the sudo crontab -e the following line can be added:
15 22 * * * nohup python3.4 /home/ubuntu/sprinkler/sprinklerSwitches.py
Here I say with (15 22 * * *) starting from right:
- In every day of the week
- in every month
- in every day of the month
- at 22h (= 10PM) and
- 15 min
execute the command that follows. The command is forced with “nohup” and “&” to run in the background so it is not stopped if a window is closed or a connection lost. Then using the absolute path the script sprinklerSwitches.py is executed with Python.
Summary
Shown here is how to build a Raspberry Pi based sprinkler controller, that lets you not only control a lot of sprinkler valves (more than most commercially available) for little money, but also is extendable to much more. In a later post we will discuss how to add to this a home surveillance system.
The Raspberry Pi has also the “built-in” bonus to be controllable over the internet which opens up vast possibilities…
Next
In the next post I would like to present a special LED light board that can be used for many art and presentation projects.
To be notified when future blog posts are published here, be sure to enter your email address in the form below!
I am looking forward to any comments and discussions. Please use the form below.
September 20, 2016 at 8:32 am
Dear Arri,
can you put rain sensor in your amaising project?
Thx
December 21, 2016 at 7:40 pm
Hi there! This is my first visit to your blog! We
aare a collection of volunteers and starting a new project in a community in the same niche.
Your blog provided us valuabble information to work on.
You have done a marvellous job!
December 21, 2016 at 8:07 pm
Thanks. Sounds like fun work you are planning…
December 21, 2016 at 7:56 pm
Saved as a favorite, I love your blog!
December 21, 2016 at 8:06 pm
Attractive portion of content. I just stumbled upon your web site and in accession capital
to assert that I get actually loved account your blog posts.
Any way I’ll be subscribing for your feeds and even I success you get entry to constantly quickly.
December 21, 2016 at 8:12 pm
naturally like your web-site however you have
to test the spelling on quite a few of your posts.
Several of them are rife with spelling issues and I in finding it very
troublesome to tell the reality on the other hand I’ll surely come again again.
December 22, 2016 at 9:55 pm
Thanks. Improvements are ongoing.
December 22, 2016 at 12:04 am
Sure, thanks.
December 22, 2016 at 12:05 am
Glad it helped. Good luck with your project.
July 11, 2018 at 10:38 am
Hey, thanks for this! I’m following this guide to replace an ancient RainBird sprinkler timer. Using an old RPi 1B, so the pinout is different — but it looks like any “gpio” pins are fine, though I’ll be able to use a max of 8 zones, since the 1B only has 8 gpio pins.
Thanks again!
July 11, 2018 at 11:25 am
Hi Roy,
Yes you can use any GPIOs. The ones I am showing here are just as example.
There are actually many ways to expand the GPIOs of your pi to control as many valves as you want.
I am working right now on one that involves multiple PIs.
July 11, 2018 at 12:57 pm
Do you know of any way to control N relays with <N gpio pins without using a mux?