This post is part of a series
Now things get more interesting - in this part of the project we are actually changing something, rather than passively reading values. We will be turning things on (primarily the water pumps) via a relay. I selected an 8 way 5V relay board for this, you can see the product link here
For the pumps I chose 5V USB submersible aquarium pumps, you can see them on this Amazon Link. I went with these low voltage pumps as I wanted to drive everything from a single USB hub. I ended up powering four of these pumps and the RPi itself from the USB hub. I may have been able to power the pumps from the RPi 5V output but I suspected it would be to much drain and I wanted to play with a relay board anyway. Now I know how to use it, I could swap the pumps for stronger ones - not that I need to, the 5V ones work quite fine
The relay board is 5V so you will need to power it from the 5V rails on your breakout board, then each relay requires just one connection to a GPIO pin, per relay. Write down which pins are associated with which relays. Below you can see the relay directly connected to the RPi, later I move these connections to the T Cobbler mentioned in the pre-reqs post
You can test the relays without having anything connected to the terminals, a red LED will light when you have activated the relay. Relays provide two configurations - NO (Normally Open) and NC (Normally Closed) pins - in my case I wanted to use the Normally Open ones so that when I activate the relay, the power to the pump is connected and the pump starts pumping.
Another simple python script can be used to activate the relays for a number of seconds
#!/usr/bin/python import RPi.GPIO as GPIO import time import os import sys #if sys.argv=='one': # one = read_temp(temp_sensor1) # print one channel=int(sys.argv) seconds=int(sys.argv) #GPIO.cleanup() GPIO.setmode(GPIO.BCM) # Quieten warnings GPIO.setwarnings(False) # the channel list below is in revers order channels = [ 23,24,25,8,7,12,16,20 ] GPIO.setup(channels, GPIO.OUT) # short wait, not particular reason time.sleep(2) GPIO.output(channel, GPIO.HIGH) print "channel ",channel," activated" time.sleep(seconds) GPIO.output(channel, GPIO.LOW) print "channel ",channel," de-activated"
To use this you can simply do
python /home/pi/water-system/pumpX.py 16 10
This will activate the relay connected to channel 16 for 10 seconds, you will see the red light next to the relay come on, then go off
To utilise the water pumps I had to cut the usb cables and strip down the two wires inside. the one wire (black) just gets immediately re-connected, and only the red one needs to be connected to the relay (twice - once in and once out), so that when the relay is activated the red cable is re-connected. Repeat this for all four pumps. The beauty of only using 5V pumps is that you arent risking electrocution
Once all four of your pumps are connected, connect a short piece of water pipe and submerge the pumps in a bucket of water and re-test. There is something very satisfying about typing a command into your terminal and watching water start pumping (or maybe its just me)