SOLVED - Arduino Controlled Ball Valve - Code Help

I have a 12V DC servo controlled ball valve, a 12V DC power supply, and two relays.

I need to use a wall mounted (mercury switch style) thermostat to tell the arduino to turn a ball valve.

I appologize I am not knowledgeable in arduino code, though I have tried to dabble a little.

I have attached a (very crude) wiring diagram as well.

Setup: Arduino checks for a completed/incomplete circuit by the thermostat. Completed (yes/no): send signal to relay to turn on for 3-5 (unsure yet) seconds to allow for full rotation of ball valve.

If the last signal sent was the same (last signal was a completed circuit), ignore sending power to turn the ball valve again if its not needed.

I have tried to find similar codes online that I could tweak, but havent worked it out yet. I appreciate any help. I figure this is a very straight code and shouldnt be too terribly hard... (I hope!)

(deleted)

The OP's original diagram:

Try this for proper wiring.

You change both relays to be either on or off to go open or closed. When the relays are opposite (one on, the other off) the valve will stay in that position unless it is a spring closing type.

I realize one of the colors red wires is supposed to be black, I messed that up in paint. As for the wiring, I see how it can short itself out if they switched at the same time, and I am greatful you altered my picture to make it work correctly. The valve I am using is not a spring type, it will stay how it is once the power is removed from it. Ideally I need power going to one relay for a few seconds to turn it clockwise, and then power going to another relay to turn it counterclockwise. That being said, I am having a hard time figuring out how that will work with this diagram and want to verify it will work that way.

Side note: I dont even know how to make the arduino test to see if a circuit is completed :confused:

I made a mistake with my circuit diagram. It should be arranged so that the valve does not move with both relays de-energized or energized, only moving with relays in opposite states (off-on and on-off).

A corrected version.

Here is a brief code to show operation:

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop() {
  // stop
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  delay(1000);
  // go OPEN direction
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  delay(3000); 
  // stop
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  delay(1000);
  // go CLOSED direction
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  delay(3000);
}

Thank you so! Much. That code is for the relay circuit correct? Would you be kind enough to write a code on how to check for a completed circuit? :slight_smile:

(deleted)

brandonb0013:
Would you be kind enough to write a code on how to check for a completed circuit? :slight_smile:

What do you mean by a "completed circuit"?
Tom.... :o

I'm sorry spycatcher2k. TomGeorge, I'm just trying to figure out how to make the Arduino know if two wires are connected or not. If the state changes then flip the relay. I can't seem to find anyone online who has done that. Thank you.

(deleted)

I guess I just need to think outside the box a little... that makes sense. I will see if I can figure out how to code that...

Thanks

brandonb0013:
I'm sorry spycatcher2k. TomGeorge, I'm just trying to figure out how to make the Arduino know if two wires are connected or not. If the state changes then flip the relay. I can't seem to find anyone online who has done that. Thank you.

If I understand you want to detect when a switch is turned ON or OFF?
You want to fit LIMIT Switches to the valve so you know if it is OPEN or CLOSED, and to stop it when it is fully open or fully closed?
Tom...

So the ball valve already has a servo motor on it. it just needs 12DC to make it turn. It stops when it is fully open or fully closed. So I just need to reverse the polarity to make it turn either direction. I planned on doing that with the relays. Powering one relay to turn it one direction, and powering the other relay to turn it the other direction. The relay only needs to be activated for about 3-5 seconds (I havnet timed it yet) for the valve to turn completely.

And the arduino is just using a wall mounted thermostat (mercury switch kind) to know when to turn the valve. When the switch completes the circuit it will tell the arduino to turn the valve open. When the circuit is opened it will tell the arduino to close the valve.

The arduino will be constantly checking if the circuit is open or closed and will trigger a relay for a period of time upon a state change of the circuit.

Thanks again everyone.

This is essentially the ball valve I have, just a different valve. Same actuator.

https://www.google.com/search?q=cwx-15n+ball+valve&rlz=1C1GCEA_enUS757US757&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiZwpuVjcTYAhUJq1MKHbLUAZoQ_AUICygC&biw=1268&bih=927#imgrc=qJOYRlFVVuw2bM:

Hi,
That actuator has built in limit switches, so you can't over drive it, so time ON is not super critical.
You just need 5Seconds or a bit more.

This will help with your button/switch.

Tom.. :slight_smile:

Thanks Tom. Yeah I've already messed with the valves and understand their workings. I'm more of a mechanical hands-on guy, not 1s and 0s. :slight_smile: I greatly appreciate everyone's help with this project.

I have made a lot of progress everyone. This is the code I have come up with thus far and it works perfectly except after the relay has turned on for 3 seconds then off, it will turn back on. I just need to figure out how to make it not turn the relay back on if the circuit is in the same state it was that originally triggered it. (If temperature hasnt changed, dont activate relay).

/*
  Button

  Turns on and off a light emitting diode(LED) connected to digital pin 13,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

  - Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

  created 2005
  by DojoDave <http://www.0j0.org>
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED/Relay on/open:
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      delay(1000);
  // go OPEN direction
      digitalWrite(3, HIGH);
      digitalWrite(4, LOW);
      delay(3000); 
  // stop);
  } else {
    // turn LED/Relay off/closed:
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      delay(1000);
  // go CLOSED direction
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
      delay(3000);
  // stop
}
}

I can close the thread now, I figured it out and it was much easier than expected. I just added another digitalWrite to the LOW pin on both the if and else statements and it turned back off when done after the 3 seconds. Also did lastbuttonState to keep it from running again

Thanks again everyone

Hi,
You can go up and edit the subject of this thread, put (SOLVED) next to the subject

Please post your final code so anyone else following this code can see your solution.
This way the thread will help other users.

Tom... :slight_smile:

Final Code:

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
  // read the state of the thermostat circuit:
  buttonState = digitalRead(buttonPin);

         digitalWrite(3, HIGH);  // to turn both relays off when it starts, without it they would both stay on
         digitalWrite(4, HIGH);  // to turn both relays off when it starts, without it they would both stay on

 if (buttonState != lastButtonState){
     // check if the cicuit is completed. If it is, the buttonState is HIGH, therefore thermostat is closed
     if (buttonState == LOW) {
      // turn Relay/valve open:
         digitalWrite(3, HIGH);
         digitalWrite(4, HIGH);
         delay(1000);
     // go OPEN direction
         digitalWrite(3, HIGH); 
         digitalWrite(4, LOW);
         delay(4000); 
         digitalWrite(4, HIGH);
         lastButtonState = buttonState;

     // stop);
  } 
     else {
     // turn Relay/valve closed:
         digitalWrite(3, HIGH);
         digitalWrite(4, HIGH);
         delay(1000);
     // go CLOSED direction
         digitalWrite(3, LOW);
         digitalWrite(4, HIGH); 
         delay(4000);
         digitalWrite(3, HIGH);
         lastButtonState = buttonState;

     // stop
}   }   }