

Electronic Hardware
In the above schematic I also drew in the relay board. Obviously if you use the same relay board that I did, you don't need to build any of this, but I wanted to include it so that anyone using a different relay could adapt the circuit to their own needs.
One last, but very important note, when I finally had the whole set-up finished, I tested it out. As soon as I sent power to the solenoid, my Pi emailed me that my smoke detector was alerting and gave me an audible alert that it was preparing to shut down. What!?!? These are all separate systems that should have nothing to do with each other except that the same Pi controls them all! Well, since I had run my 12V lines along the same path as the jumpers for all of my GPIO pins, as soon as I switched the solenoid on, the electrical noise on the 12V lines interfered with the signals on the other GPIOs, causing a moment of havoc for the Pi. This could have been solved by rerouting my wires, or by trying to smooth things over with capacitors, but I happened to have some ferrite core noise suppression filters lying around, so I tried one out. After testing several times, I can verify that this does in fact solve the problem. With the noise filter there are no issues, without it the Pi goes a little nuts. So keep this in mind when determining your cable management, or invest in some filters to solve the problem after the fact.
Plumbing Hardware
I hate plumbing, but it's a handy thing to know how to do. I know just enough about it to make me dangerous, but that probably applies to a lot of things. I won't go into too much detail here, because everyone's plumbing situation is going to be different, so there is no one-size-fits-all solution. I am going to try to focus on the few components that will be universally required, and then just skip to the final result. If you are not completely comfortable with altering the plumbing in your house, then you may want to look into another way to do this project. Plumbers are expensive, so mistakes could be costly, not to mention messy. You've been warned, so I accept no responsibility if you choose to proceed and accidentally flood your house.
Above is the basic set up for the plumbing piece. Starting on the left, the first piece we need is a cut-off valve. If my improvised plumbing leaks or brakes, or my Pi goes all Superman 3 on me, I need to have a way to cut off the water from it's source. Next comes the solenoid valve that will be controlled by the Pi. After that we need to use a check valve to prevent the water from the hose from draining back into my water pipes (this is probably required by code in most places). Finally, I used a T-junction and another cut-off valve to allow for draining the outdoor pipes. This valve will stay closed during normal operation, and will only be opened at the end of the season in order to drain the outdoor pipes so they don't freeze. The outlet for this valve will lead to a sink basin in my shop so that I don't have to mess with sewer lines and traps.

So that is the basic design. To the right is the fully installed product. To tap into the waterline I opted to slice into one of my newer Pex waterlines, which is much easier than trying to mess with 70 year old galvanized water pipe. For the line that continues outside I opted to use galvanized steel water pipe because I wanted something more rigid to support the solenoid valve, and because you can't run Pex outside. If you do something similar and use steel pipe outside, be sure to paint it to keep it from rusting.
Above is the basic set up for the plumbing piece. Starting on the left, the first piece we need is a cut-off valve. If my improvised plumbing leaks or brakes, or my Pi goes all Superman 3 on me, I need to have a way to cut off the water from it's source. Next comes the solenoid valve that will be controlled by the Pi. After that we need to use a check valve to prevent the water from the hose from draining back into my water pipes (this is probably required by code in most places). Finally, I used a T-junction and another cut-off valve to allow for draining the outdoor pipes. This valve will stay closed during normal operation, and will only be opened at the end of the season in order to drain the outdoor pipes so they don't freeze. The outlet for this valve will lead to a sink basin in my shop so that I don't have to mess with sewer lines and traps.

So that is the basic design. To the right is the fully installed product. To tap into the waterline I opted to slice into one of my newer Pex waterlines, which is much easier than trying to mess with 70 year old galvanized water pipe. For the line that continues outside I opted to use galvanized steel water pipe because I wanted something more rigid to support the solenoid valve, and because you can't run Pex outside. If you do something similar and use steel pipe outside, be sure to paint it to keep it from rusting.
Control Software
With everything in place, all I needed to do was to write a couple of short scripts to control the solenoid valve. This is the easy part. Here are the two python scripts called water-on.py and water-off.py:
#!/usr/bin/env python
# water-on.py
import RPi.GPIO as GPIO
from time import sleep
RELAY_PIN = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAY_PIN, GPIO.OUT)
GPIO.output(RELAY_PIN, 0)
sleep(1200)
GPIO.output(RELAY_PIN, 1)
#!/usr/bin/env python
# water-off.py
import RPi.GPIO as GPIO
RELAY_PIN = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAY_PIN, GPIO.OUT)
GPIO.output(RELAY_PIN, 1)
For the water-on.py script I added in a 20 minute timer before automatically turning the water back off as a fail-safe. As always, once the scripts are created using a text editor, they need to be made executable by using
chmod +x water-on.py
chmod +x water-off.py
I need to make sure that the GPIO pin that controls the relay isn't floating when the Pi reboots, so I need to run the water-off.py script on boot. I am going to do this by adding the following line to the /etc/rc.local file:
sudo /home/pi/water/water-off.py
This time I am also going to make the file executable without having to specify the whole path to the file by copying it to the /usr/bin folder like so:
cp /home/pi/water/water-on.py /usr/bin/water-on
cp /home/pi/water/water-off.py /usr/bin/water-off
Notice I dropped the .py when I added the file to /usr/bin. This is not necessary, but in Linux land file extensions are not required, and it saves me a little bit of typing. So now the water can be turned on with a simple command.
water-on
But the point of this was not to be able to turn on the water by typing a command, we could do that more easily by flipping a switch. The goal is to be able to schedule the water to turn on and off automatically. For this we will use Linux's built in scheduling system called Cron. To add a task to the cron table we use the following command:
crontab -e
The first time you edit the cron table you will be prompted to pick a text editor. I prefer vim, but it's purely a matter of personal taste. When the file is opened for the first time there is nothing but lines of comments explaining how Cron works. Since the directions are built into the cron table, I won't go into too much detail here. To turn the sprinklers on for 20 minutes every morning at 6:00am you would add the following two lines to the end of the cron table:
00 6 * * * water-on &
20 6 * * * water-off
Remember that I added a fail-safe to the water-on script to have it shut off after 20 minutes, so the water-off isn't 100% necessary, but it seems like good practice. If you wanted to run the sprinklers for more than 20 minutes you could either edit the sleep line of the water-on script, or just add another line to the cron table to turn the water back on after 20 minutes.
So that's it. Now every morning my Pi will wake up at 6am sharp and take care of my watering for me. Sure you can buy an off-the-shelf product that will do the same thing, but what's the fun in that? If anyone needs any help adapting this walk-through to your own projects just let me know in the comments and I'll do my best to help.
This is super dope, thanks for posting. I'm looking to do something similar with for anti-mosquito spraying. There's products out there, but where's the fun in that?
ReplyDeletenice project. A small question though. When there was/has been a rain shower, what do you do. Esp. when from home. I trust you have remote access to your RP.
ReplyDeleteYes, I have remote access, but the sprinklers usually kick on at 6:00 am, so usually I am in bed hitting the snooze button when it's watering. I currently have some soil moisture sensors on order. When they get here I am going to modify the scrip for watering to check the soil moisture to see if watering is necessary before turning on the sprinklers. I plan to update this post after I make the change. Good question. Thanks for your interest.
DeleteThis is awesome love to read this instructive information.I have also remote access.But yeah the sprinklers usually kick on early in the morning but anyhow it depends allot situation,If the air is dry, they will need more water. The warmer the temperature is, the more water they need.I have a beautiful garden with an working garden irrigation system and i always love to care my raspberries.
ReplyDeleteHello, i think that i saw you visited my blog so i came to “return the favor”.I am trying to find things to improve my website!I suppose its ok to use some of your ideas!! norlog pond
ReplyDeleteDo I need to change the current AC to DC?
ReplyDeleteYes, the Raspberry Pi requires 5V DC, and the electric solenoid requires 12V DC.
Delete