Osprey72
Posts: 26
Joined: Sun Apr 24, 2016 7:07 pm
Location: Michigan

How to keep a valve open?

Thu Oct 06, 2016 11:39 pm

I am not a full time coder yet so my attempt at this is obviously flawed because I cannot keep the valve open. Here is the code:

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(15, GPIO.IN)
GPIO.setup(29, GPIO.OUT)

GPIO.output(29, False)

input_value=GPIO.input(15)
print "Float running..."
if input_value == False:
	print "Water OFF, Valve OPEN"
	
	if input_value==True:
		print"Water ON, Valve CLOSED"
		time.sleep(600)
			
GPIO.cleanup()
The ultimate goal for this is to dispense liquid fertilizer to a planted tank on a cron job every 2 days, per the bottle instructions. Currently, I am filling the cap and doing the dosing. The valve is 12VDC and I was going to have the GPIO pin open and close a relay so I don't run 12V through my pi (lol). Help would be appreciated :)

MartinKiwiPi
Posts: 3
Joined: Fri Oct 07, 2016 3:50 pm

Re: How to keep a valve open?

Fri Oct 07, 2016 4:26 pm

Hmmm, the code is a little confusing,
The water is off when the valve is open?
Seems counter intuitive - I would expect water to flow when the valve is open

Python code uses indenting to control execution flow
Your second test will only get executed if the first test is true,
Clearly the input_value can't be false and true, so the sleep will never be executed

Just remove the indenting on the second if test

Code: Select all

if input_value == False:
   print "Water OFF, Valve OPEN"
   
if input_value==True:
   print"Water ON, Valve CLOSED"
   time.sleep(600)
You may find a PiFace Digital IO easier to use - it has two relays which can handle ~20v
http://www.piface.org.uk/products/piface_digital/

Osprey72
Posts: 26
Joined: Sun Apr 24, 2016 7:07 pm
Location: Michigan

Re: How to keep a valve open?

Sun Oct 09, 2016 3:36 pm

MartinKiwiPi wrote:Hmmm, the code is a little confusing,
The water is off when the valve is open?
Seems counter intuitive - I would expect water to flow when the valve is open

Python code uses indenting to control execution flow
Your second test will only get executed if the first test is true,
Clearly the input_value can't be false and true, so the sleep will never be executed

Just remove the indenting on the second if test

Code: Select all

if input_value == False:
   print "Water OFF, Valve OPEN"
   
if input_value==True:
   print"Water ON, Valve CLOSED"
   time.sleep(600)
You may find a PiFace Digital IO easier to use - it has two relays which can handle ~20v
http://www.piface.org.uk/products/piface_digital/
Ok, I should have been more descriptive. The water in question is the fresh water inbound from the house source. When the valve closes (a drain valve), the house water solenoid is opened and water refills the tank to the level the timer allows. I apologize for the missing description and any confusion it caused :( As for the logic, I see your point. I forgot to edit out the first "False" statement as I was trying to make it work based on what I knew...now I know better :) As for the relay, I have the Pi controlling the relay with 5v and only have my supply 12v current running when the relay is triggered so it is isolated from the Pi. Thanks for the input!!

User avatar
Cancelor
Posts: 746
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: How to keep a valve open?

Sun Oct 09, 2016 5:18 pm

It's hard to tell what you really want :? but this is what you have :)

Every time this runs it will set GPIO.output(29, False) and print Float running...

Assuming you have corrected the indentation then depending on input_value it will GPIO.cleanup() and exit immediately or after a delay.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

MartinKiwiPi
Posts: 3
Joined: Fri Oct 07, 2016 3:50 pm

Re: How to keep a valve open?

Mon Oct 10, 2016 1:19 pm

So you should have it running ok now?

Osprey72
Posts: 26
Joined: Sun Apr 24, 2016 7:07 pm
Location: Michigan

Re: How to keep a valve open?

Thu Oct 20, 2016 3:37 am

MartinKiwiPi wrote:So you should have it running ok now?
Somewhat...I've been racking my brain trying to figure out what I am doing wrong. Here is my updated code:

Code: Select all

#!/user/bin/python
import RPi.GPIO as GPIO
from time import sleep

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.IN) #Float switch
GPIO.setup(6, GPIO.OUT) #Relay to turn water on
GPIO.setup(13, GPIO.OUT) #Relay to open drain valve


if GPIO.input(16)==False:
	print"Water ON, Float is down"
	GPIO.output(6, GPIO.HIGH) #Water valve energized (on)
	sleep(10)
	GPIO.output(6, GPIO.LOW)
	
	if GPIO.input(16)==True:
		print "Water OFF Float is UP"
		GPIO.output(13, GPIO.HIGH) #Drain valve energized (open)
		sleep(15)
		GPIO.output(13, GPIO.LOW)

	else:
		GPIO.cleanup()
else:
	GPIO.cleanup()
When run, yes the water comes on but the drain valve opens too. No matter what I switch the bools to, both relays are energized, one right before the other. I have tried a break statement in between the 2 if statements and it says that the break is outside the loop. I add random print statements to block the break inside the loop, still get "it's out of the loop". I gave up on that. I have tried various GPIO pins, different bool combinations, tried various variables, I am just ready to kill this idea and manually do the dang water control. It appears that the only way I can shut off the relays is to insert the GPIO.cleanup() command...that's why it's in there twice. What am I doing wrong??? :oops:

sandcrabjarv
Posts: 18
Joined: Sun Jun 26, 2016 8:06 pm
Location: UK, sheffield
Contact: Website

Re: How to keep a valve open?

Thu Oct 20, 2016 7:43 am

This should work

Code: Select all

#!/user/bin/python

import RPi.GPIO as GPIO
from time import sleep

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)
GPIO.setup(16,GPIO.IN) #Float switch
GPIO.setup(6, GPIO.OUT) #Relay to turn water on
GPIO.setup(12, GPIO.OUT) #Relay to open drain valve

if GPIO.input(16)==False:
   print"Water ON, Float is down"
   GPIO.output(6, GPIO.HIGH) #Water valve energized (on)
   sleep(10)
   GPIO.output(6, GPIO.LOW)
   
elif GPIO.input(16)==True:
   print "Water OFF Float is UP"
   GPIO.output(12, GPIO.HIGH) #Drain valve energized (open)
   sleep(15)
   GPIO.output(12, GPIO.LOW)

else:
   GPIO.cleanup()
https://www.facebook.com/SandcrabJarv/ non-ferrous metal casting

Osprey72
Posts: 26
Joined: Sun Apr 24, 2016 7:07 pm
Location: Michigan

Re: How to keep a valve open?

Thu Oct 20, 2016 12:29 pm

sandcrabjarv wrote:This should work

Code: Select all

#!/user/bin/python

import RPi.GPIO as GPIO
from time import sleep

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)
GPIO.setup(16,GPIO.IN) #Float switch
GPIO.setup(6, GPIO.OUT) #Relay to turn water on
GPIO.setup(12, GPIO.OUT) #Relay to open drain valve

if GPIO.input(16)==False:
   print"Water ON, Float is down"
   GPIO.output(6, GPIO.HIGH) #Water valve energized (on)
   sleep(10)
   GPIO.output(6, GPIO.LOW)
   
elif GPIO.input(16)==True:
   print "Water OFF Float is UP"
   GPIO.output(12, GPIO.HIGH) #Drain valve energized (open)
   sleep(15)
   GPIO.output(12, GPIO.LOW)

else:
   GPIO.cleanup()
Ok, made the changes to your code...still the same results. I switched the wire over to 12, ran the code, both relays energized (not what I am wanting...want either the water to energize or the drain valve to energize but not both...would be defeating the purpose) and when the program ended, the relays are still energized-water still flowing and the drain is open.
(tried to share an image but couldn't get it to load)

sandcrabjarv
Posts: 18
Joined: Sun Jun 26, 2016 8:06 pm
Location: UK, sheffield
Contact: Website

Re: How to keep a valve open?

Thu Oct 20, 2016 1:52 pm

Ah forgot to change 12 back to 13 when testing sorry. The code works without a relay attached, are the relays close to the pi?

You could try changing the elif to this to see if its being false triggered

Code: Select all

elif GPIO.input(16)==True:
   sleep(2)
   if GPIO.input(16)==True:
      print "Water OFF Float is UP"
      GPIO.output(12, GPIO.HIGH) #Drain valve energized (open)
      sleep(15)
      GPIO.output(12, GPIO.LOW)
https://www.facebook.com/SandcrabJarv/ non-ferrous metal casting

Osprey72
Posts: 26
Joined: Sun Apr 24, 2016 7:07 pm
Location: Michigan

Re: How to keep a valve open?

Fri Oct 21, 2016 1:54 pm

Ok, installed your code...even tried a sleep(20) to see if anything changed...nope. Both relays are being energized still. The answer to your question is yes, the relays are close to the Pi. I have them mounted on a piece of plexiglas, here on my desk, with a custom wooden base. Judging by your question, are you thinking that being so close to each other...maybe the relays are picking up the electrical signal fro the Pi? I only have the 2 relays hooked up (out of 8). For fun, I will hook up a dummy relay and see if it gets energized...can't hurt to try.

update: ran the code with a dummy relay hooked up. The dummy didn't energize when I ran the code...something is telling the #7 relay to energize when it is not supposed to. Again, when the water is low (True condition), only relay #8 should energize (water on). When the condition turns False (float is up signalling a full tank), then only relay #7 should energize (drain valve open). I have it this way because it's done with a cron job. In theory, the Pi should read where the float is (down=True, up=False). Then, based upon the result, ONE relay should be triggered. Maybe I should just have the one relay (#8 for water on?) Still, if I am running all 8 relays, I need to know if this will affect future codes and how to stop it from happening.

Osprey72
Posts: 26
Joined: Sun Apr 24, 2016 7:07 pm
Location: Michigan

Re: How to keep a valve open?

Sat Oct 29, 2016 11:47 am

Alright!! I have found a successful medium ground. I have separated the two tasks into two separate programs. I slightly modified your code to keep running (loop) until the float triggers (full). The other program, unfortunately, still comes on at the same time as I cannot get them to stop triggering at the same time but it will govern the amount of total time the water will remain on per fill cycle (so the water doesn't stay on indefinitely). Thankfully, the input is much greater than the output (more water coming in than leaving thru the open valve when triggered). Codes to follow

Code: Select all

#!/user/bin/python
import RPi.GPIO as GPIO
from time import sleep
import sys

GPIO.setwarnings(False)

GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.IN) #Float input signal
GPIO.setup(6, GPIO.OUT) #Relay to turn water on/off

def water_control():
	while True:
		if GPIO.input(16)==False:
			print GPIO.input(16), "Float is down, turning the water on..."
			GPIO.output(6, GPIO.LOW) #Water valve energized (on)
			sleep(10)
			
		else:
			print "Float is UP, turning the water OFF"
			GPIO.output(6, GPIO.HIGH)
			GPIO.cleanup()
			break
water_control()

Code: Select all

#!/user/bin/python
import RPi.GPIO as GPIO
from time import sleep

GPIO.setwarnings(False)

GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.IN) #Float switch
GPIO.setup(12, GPIO.OUT) #Relay to open/close drain valve


def water_control():
	while True:
		if GPIO.input(16)==True:
			print"Float is down, closing valve"
			GPIO.output(12, GPIO.HIGH)
			sleep(300)
			GPIO.cleanup()
			break
	
		else:
			print "Float is UP, opening valve"
			GPIO.output(12, GPIO.LOW) #Drain valve energized (open)
			sleep(150)
			GPIO.output(12, GPIO.HIGH)
			GPIO.cleanup()
			break
			
water_control()

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 9 guests