Page 1 of 1

How to switch a water pump with a soil moisture sensor?

Posted: Wed Apr 13, 2016 3:18 pm
by malcolmvella1993
Hi,

I am doing a school project which is an automatic watering plant system. Basically i want to switch a 12v water pump when the moisture sensor detects dry soil.

I found a modmypi tutorial which sends an email notification however i want to switch on the water pump for a specific time.

(I can only use DIGITAL output on the moisture sensor)

http://www.modmypi.com/blog/raspberry-p ... n-tutorial

I would appreciate if someone could help me

Regards,

Malcolm

Re: How to switch a water pump with a soil moisture sensor?

Posted: Wed Apr 13, 2016 4:31 pm
by Davies

Code: Select all

import RPi.GPIO as GPIO, time
GPIO.setmode(GPIO.BCM)


def RCtime(RCpin):
    reading = 0
    GPIO.setup(18, GPIO.OUT)
    GPIO.output(18, GPIO.LOW)
    time.sleep(0.5)
    GPIO.setup(18, GPIO.IN)
    while GPIO.input(18) == GPIO.LOW:
        reading += 1
    return reading
would read that moisture sensor on gpio 18 but they are somewhat unstable(or mine was) so i ended up taking 10 readings then dividing that by 10, i also powered the moisture sensor only when a reading was taken by a relay on gpio 21

Code: Select all

GPIO.setup(21, GPIO.OUT, initial=1)
def maths():
    global current_moisture
            GPIO.output(21, 0)
            time.sleep(0.5)
            time1 = RCtime(18)
            if time1 <= 100:
                time1 = RCtime(18)
            time.sleep(1)
            time2 = RCtime(18)
            if time2 <= 100:
                time2 = RCtime(18)
            time.sleep(1)
            time3 = RCtime(18)
            if time3 <= 100:
                time3 = RCtime(18)
            time.sleep(1)
            time4 = RCtime(18)
            if time4 <= 100:
                time4 = RCtime(18)
            time.sleep(1)
            time5 = RCtime(18)
            if time5 <= 100:
                time5 = RCtime(18)
            time.sleep(1)
            time6 = RCtime(18)
            if time6 <= 100:
                time6 = RCtime(18)
            time.sleep(1)
            time7 = RCtime(18)
            if time7 <= 100:
                time7 = RCtime(18)
            time.sleep(1)
            time8 = RCtime(18)
            if time8 <= 100:
                time8 = RCtime(18)
            time.sleep(1)
            time9 = RCtime(18)
            if time9 <= 100:
                time9 = RCtime(18)
            time.sleep(1)
            time10 = RCtime(18)
            if time10 <= 100:
                time10 = RCtime(18)
            time.sleep(0.5)
            GPIO.output(21, 1)
            current_moisture =  ((time1 + time2 + time3 + time4 + time5 + time6 + time7 + time8 + time9 + time10) / 10)
then, without testing.. if you wanted to say turn pump on when current_moisture is less than a known variable (your_input)

Code: Select all

GPIO.setup(26, GPIO.OUT, initial=1)
your_input = 1000
def pumper():
    global current_moisture, your_input
    while 1:
        maths()
        if your_input <= current_moisture:
            GPIO.output(26, 1)
            print "pump off"
            time.sleep(60)
        if your_input > current_moisture:
            GPIO.output(26, 0)
            print "pump on"
        time.sleep(1)
pumper()
i used octocouplers and circuitry that turned the relays on when GPIO was ground and off at 3.3v, a relay set in normally closed would work while code was running but would leave pump on when code was not running.
if you wanted the moisture sensor to go to sleep for longer when there was more water, without testing.. you may be able to add to the pumper def..

Code: Select all

if (your_input x 2) <= current_moisture:
    time.sleep(3600)
for an hours sleep.
im fairly new to python and programming so theres probably better code out there and i realise youve found a good guide, im just sharing the code of how i did it.
this type of relay board would work http://www.ebay.co.uk/itm/5V-10A-2-4-8- ... ihwf2ySuug