Friday, August 4, 2017

Moisture Sensors - Automate Watering Your Plants Part 1

Instagram: @hightekgrow
Twitter: @HighTekGrow

[ Introduction ]
    Due to popular demand, I have decided to do a write up on automating watering your plants.  Now you can feel free to go on vacation and leave your seedlings without needing someone to come water them.  The first part of the tutorial will be setting up and reading the moisture sensor.  For this demonstration I used a beaglebone black but, I only did this because I had one laying around not in use.  The same theory can be applied to the Raspberry Pi and everything will connect and work the same way.  I also chose to write the example code in Python as it will run on both systems.

[ Hookup The Sensor ]
   The sensors I am using can be found on Amazon here.  That link will bring you to a 5 pack of the exact sensors I use in this tutorial for $8.  They are also on prime so you don't have to wait weeks for them to come in from China.

    The first step is hooking the sensor up to the small board it comes with.  On the sensor that goes into the soil you will see two pins.  The small board that comes with each sensor contains all the resistors and everything else you need to use the sensor.  That board has two sets of pins, the 4 pin set is what you connect to your beaglebone or your pi and the other set is the 2 pins you connect the sensor to.  They can be connected in any order.  This can be seen in the image below.

    Next you will need to hook up the unit to your beaglebone or pi.  No matter what you are connecting the sensor to there are only three things requires.  A 3.3v power source, a ground and a digital or analog pin to read the sensor.  For this example, we will be using the digital output.  The difference here is that the digital sensor will always output a 0 or a 1.  If the sensor reads 0 it means the soil is moist.  On the other end if it reads 1 your soil is dry.  The level of moisture that triggers the sensors reading 0 can be adjusted using the small white potentiometer on the board.  The analog output will read out an exact moisture value as a floating point number.  This is much more precise but requires you to do a little testing with soils of different moistness to calibrate the device and know when to trigger turning on your water pump.  Comment below if you would like to go over analog readings and I will do another tutorial on analog readings from the same sensors.  In the image below you can see the three pins we will need.  They are the three labeled VCC, GND and DO.

  To connect to your device you need to first find the 3.3v power output pin on your device.  This gets connected to the VCC pin.  On a side note, you may also use a 5v but you would need to add a resistor in line.  You can read about this online or comment below if you would like to know more.  Next you need to connect the GND pin on the sensor to the ground pin on your device.  All thats left after that is connecting the DO pin to one of the gpio pins on your device.  Below is an example of the sensor plugged into my beaglebone black.

    In this image the very top left wire is the ground, the wire just below that is the 3.3v power source and the wire on the right side is connected to a GPIO pin.

[ The Code ]
    Here comes the fun part.  We need to write a little script to determine if we need to turn our pump on and water our plants or not.  Below is the code to check the sensor on a beaglebone black.  I will also provide sample code for a Raspberry pi as they will be almost identical.  The theory is the same its just a matter of semantics for the appropriate library for your device.

    The first line above is the import of the library I use to easily control the GPIO pins of the beaglebone black.  The next step here is the GPIO.setup call.  This call is setting up the pin in input mode.  This will allow us to take readings from the pin.  You should change the line that says "P8_7" to whatever pin you have connected to on your BBB.  Next, we will call the GPIO input method.  This is going to read the pin we provide and give us back the digital reading.  This value will either be 1 or 0 and the messages printed below will show you what the reading means.

    Below is code that will do the same thing but using a library for the Raspberry Pi.  If you have any questions or I did not explain anything clearly please comment below.  I will be sure to responsd!  The code snippet below was found here.  All credit goes there.  Here you would want to change the 11 being passed into the methods to whatever pin on the Pi you are connecting the sensor to.