Friday, August 4, 2017

Relays And Raspberrys - Automate Watering Your Plants Part 2

Instagram: @hightekgrow
Twitter: @HighTekGrow

[ Introduction ]

    If you have not read part one of this series I highly suggest you start there.  It can be found here.  I am going to quickly go back over hooking the moisture sensor up in this post as I will be using a Raspberry Pi this time around to show everyone how to do this on a Pi as well as a Beaglebone.  With that said it will be a quick run through on the sensor hookup and there will be information in the first post you should know that will not be here.  I will show you how to hook a water pump up to a relay, set up a bucket as a water reservoir, and run hoses to water your plants in order to keep them moist.  If you like work like this and would like to support more check out the patreon link above.

[ What You Need ]
  • 250V AC Relay
  • Raspberry Pi - You will need power and an SD card for the OS as well.  Poke around Google for instructions on setting up your Pi
  • Extension cord.  I recommend a long, heavy duty, three prong, 14 gauge or thicker extension cord.  The longer the cord the more room for error.
  • Moisture Sensor
  • Water Pump
  • Water hose to fit your pump and a T adapter to create the end that waters your plant.
  • Hole Punch
  • Wire Strippers
  • Wire Cutters
  • Hookup Wires
[ Connecting Moisture Sensor ]

    In the bottom image below you can see that we have six pins total on the board for our moisture sensor.  We will only be using five of the six pins.  The set of two pins gets connected to the moisture sensor seen in the top image below.  These may be connected in any order as seen in the images below.

    In the image above you can see that we are only using the first three pins on the four pin side.  These pins are our 3.3v power source, ground and our digital output pin.  The pin we are not using is the analog output.  This is used to get a specific moisture reading rather than a simple wet or dry.  The Raspberry Pi only offers digital pins making it tough to read the analog pin.  It is possible using pulse width modulation but that is a lesson for another day.  Comment below if you would like to learn about using PWM to read the analog pin and I will do another entry on it.  Below I have attached a picture of the pinout diagram for a Raspberry Pi 3.

   First with your Raspberry Pi powered down and unplugged (better safe than sorry) we will want to plug the first pin on the moisture sensors board (labeled vcc) to pin one in the diagram above(labeled 3.3V PWR).  To the left of VCC we have the GND pin.  This is the ground and needs to be connected to any of the pins on the Raspberry Pi labeled GND.  The last pin we need to connect is the pin labeled DO.  This pin gets connected to any of the pins labeled GPIO.  Keep track of the number in the label as this is how we will reference the pin in code.  For instance, if you connect DO to the pin labeled GPIO 4, in code at line four you will set the MOISTURE_SENSOR_PIN variable to 4.  Below is an image of the sensor connected to my Pi.

[ Running Power Through Relay ]

    I find the easiest way to run your power through the relay is by cutting the power cord in half and stripping away a good bit of the outer shielding in order to give you some room to work.  When you cut open the cord you will find three wires.  The white and green wires you will be connecting right back to each other.  The black wire you want to split and strip a small bit from each end(this is very important only strip a very small bit).  Below is an image of what the final result looks like.  I soldered the green and white wires back together and put heat shrink around them but you don't have to go that far if you don't want to.  Ensure they are connected very well, you don't want to have to deal with them disconnecting randomly and killing your plants.

    Now on to the black wire.  This is our power wire and we must run it through the relay.  The relay will then either connect or disconnect the contact to allow the power to flow or not based on what our software tells it to do.  This is how we will be turning our water pump on and off, by connecting the power cord for the water pump to the extension cord running through our relay.  In the picture below you can see that each relay has three ports.  We only ever need to use two.  We can set the contact normally open or normally closed based on how we connect the wires.  For this purpose, we will be using the left two for normally open.  If you would like to learn more about this write a comment below and I will do another entry on it. In the image below you can see how we connect to the common and normally open with both ends of our power wire.  It doesn't matter what side goes where as long as it looks like the image below.

 [ Connecting Relay To Pi ]

    Now that we have our extension cord running through or relay we need to connect the relay to the Pi via the four pins on the back of the relay board.  This will allow us to control the water pump from our Pi.  Above is an image of the four pins, I will only be using three as I am not using the second relay but if you repeat the steps above with a second extension cord you may also control another device in the same manner.  Reference the image above of the pinout for the Pi for help connecting these pins.  First, we will connect the left most pin labeled vcc.  This is our 5v power supply and should be connected to any of the pins labeled 5V PWR in the pinout image from the 'Connecting Moisture Sensor' section above.  To the right of this pin is the pin that will control of the left most relay.  This should be connected to one of the GPIO pins.  As before keep track of the number in the GPIO pin name as we will need it in code.  Next, we will connect to very right most pin or the ground.  This should be connected to any of the GND pins on the Pi.  Below are images of the relay and sensor connected to my Pi.

[ Water Reservoir And Drip System ]

    For my water reservoir, I used a five-gallon bucket and submerged my water pump inside the bucket (make sure you are using a submersible pump).  I then attacked a small bit of hose in a circle to a T and punched a few holes in it for the drip.  I recommend spending some time to make one that fits your pot perfect and playing with timing, pump speed and configuration/amount of punctures to suit your ideal needs.  You can also purchase a drip system like the one here and I highly recommend doing it if you have the spare cash.  Below is an image of my homemade solution but, this was a super quick build just for the sake of this tutorial and I would highly suggest spending more time on it or purchasing one.

    I also put the put I am watering on top of a fish tank just in case our software every has issues.  You can also put in all sorts of precautions like another relay in front of the one your pump is on that checks how long the water has been running and hard kills its power if it runs on for too long or any type of measure like that.  Adding checks in the software to kill the relay after the pumps have been running for more than a minute or so and then sleeping for a bit is the least I would add.  Below is an image of my final setup.

  Now we just need to plug in our Pi, connect the water pump to our extension cord and plug the extension cord into an outlet and we are good to start writing code!

[ Python For The Win ]

   I will also be writing the code for this version in Python.  Python is a great language for learning this stuff and is super easy to get setup and use.  The first thing you will need to do is make sure you have the Raspberry Pi GPIO library installed.  This can be done by running the command 'sudo apt-get install python-dev python-rpi.gpio'.  Now we are ready to start coding.  Below is the example code I have used to control my watering.

    The first two lines above are importing the libraries we will use to control the GPIO pins.  Lines four and five are where you need to do a little modification.  You need to change these values to the pins you used above when you connected your sensor and relay.  The rest is pretty self-explanatory and if you have specific questions on what is happening, leave them in the comments below.  The logic here is very simple if the soil is read as too dry, turn on the water pump for one minute and then sleep for five minutes to allow the soil to soak up the moisture then start all over again.

[ Video ]

    I apologize for the quality of the video and for the low light. I had to film this at night and I don't have much light on my porch.  I also had a lack of dry soil so I had to pull the sensor out of the soil to simulate a dry reading. I also could have messed with the potentiometer on the moisture sensors board as that sets the sensitivity but, I did not have my tools on hand and was in a rush to film with what little light I had left.  You can see the pump kick on for one minute water the plant and then it sleeps for 5 minutes to allow the moisture to spread and then starts the loop over again.  This is a very simple setup and I highly suggest you play around with the code and timing for the pump.  There is a ton to improve on in this code and I am just trying to give you an idea of ho you can accomplish these tasks.  Improving on it is half the fun.  The video can be found here.

No comments:

Post a Comment