Monday, March 16, 2009

Simple Arduino Fluid Control

Now that I am an actual iPhone user, I should be writing up a comparison with my i9 phone, but for now, another small Arduino project will have to suffice.

Diligent readers will remember that all this started with a desire on my part of make a solar pool heater using the the Arduino. Of course, the critical part of that is to be able to control valves & pumps and move liquid around.

This is my first project to start investigating this. This includes a 12 VDC solenoid valve that I bought off of eBay (click here for details) and a simple moisture detector circuit I previously used on another project. When the system starts up, the valve is closed and pushing the button off of Pin 12 opens the solenoid valve controlled by Pin 4. When the water level reaches the moisture detector on Pin 5, then the valve closes. To restart, you can lower lower the water level and push the button again and the cycle will run over.

Here is a video of the operation (keen observers will note that this is actually a more breadboardable iDuino not a proper "Arduino"):




Yes, the green colored water is in honor of Saint Patrick's Day and to make it easier to see.

Here is the circuit diagram:



I am gradually getting better with Eagle CAD!

The software is equally simple:

/*
Solenoid control
copyright Chris Armour 2009
 */

int ValvePin = 4;                // Solenoid valve connected to pin 4
int SwPin = 12;
int buttonWas = 0; // The state of the switch (pushed = 1, not pushed = 0) last time we looked
int buttonIs = 0; // Current state of the switch
int LEDPin = 13;
int MoistPin = 5;
int val = 0;

void setup()                    // run once, when the sketch starts
{
  pinMode(ValvePin, OUTPUT);      // sets the digital pin as output
  pinMode(SwPin, INPUT);
  pinMode(LEDPin, OUTPUT);
  Serial.begin(9600);
  buttonIs = digitalRead(SwPin); //Read the initial state of the switch!
}

//===========Functions=====================//
void getButton() { 
  buttonWas = buttonIs; // Set the old state of the button to be the current state since we're creating a new current state.
  buttonIs = digitalRead(SwPin); // Read the button state
} 

void openValve(){
    digitalWrite(ValvePin, HIGH);
    digitalWrite(LEDPin,HIGH);
    Serial.println("Valve Open");
}

void closeValve(){
  digitalWrite(ValvePin, LOW);
  digitalWrite(LEDPin,LOW);
  Serial.println("Valve closed");
}
  
//=================Main Loop===================//
void loop()                     // run over and over again
{
 getButton();
  
  if((buttonIs==1)&&(buttonWas==0)) { 
    openValve();
      }
  
  val = analogRead(MoistPin);
  
  if((val > 500)){ 
    closeValve();
      }
}


Putting the Valve Open & Valve Closed commands into functions greatly simplifies the main loop, which will make it easier to extend. The only tricky part was getting the momentary pushbutton switch initialized correctly. FOr some reason, it was always starting with the valve in the open state as if the button had been pushed. This requires you put the "buttonIs = digitalRead(SwPin);" into the setup section in order to get the right values set for the intial state of the switch. This seems a bit odd to me since I would have thought that this would be taken care of by the intial declaration of the variable. It must be something about the Arduiono boot up process that accidentally sets "buttonIs" to something unexpected!

Apologies for not posting this before, but here is a link to the original moisture detector circuit with a detailed explanation -> http://www.botanicalls.com/archived_kits/twitter/. A bit dated (like this article), but lots of good details.

That it for now. The next step will be to add in a pump so that when the water level is reached and the valve shuts the pump turns on and send the water back to the reservoir. This would make this a perpetual cycle of draining and pumping.

14 comments:

  1. nice work the first arduino code i trulty understand. i am trying to make a 8x8x8 led matrix. this gives me some assurance that i can pull if of. thanks

    ReplyDelete
  2. Thanks! I find a lot of examples on the web are pretty complex because they do the correct & proper thing of handling error states - which you need to in the real world! However, since I am usually just doing a demo project I don't do all the error checking I should.

    Good luck with the project. I think if you Google around you will find a few LED matrix projects for the Arduino.

    ReplyDelete
  3. Exactly which solenoid valve did you use for this project? I'm trying to build a device that automatically discharges the contents of a CO2 cartridge on command from an Arduino.

    ReplyDelete
  4. @Nick - I bought this off of eBay from a seller called "valves4projects". Here is a link to his store ->

    http://stores.ebay.ca/valves4projects.

    I believe the one I used was a 12 volt 1/4 inch unit like this ->

    http://cgi.ebay.ca/1-4-Electric-Solenoid-Valve-12-volt-Air-Water-BBTF-/290488726108?pt=LH_DefaultDomain_0&hash=item43a27a325c.

    I recall him being a very good eBay merchant and you can ask him what would be the best fit for your project. The circuit above shows how to interface to the 12 volt solenoid.

    ReplyDelete
  5. Hi. The video is not working.
    Thanks for sharing.

    ReplyDelete
    Replies
    1. Thanks, you have flagged to me that ecent changes from Google have probably screwed up half my blog postings! I will need to go through and fix them as time permits!!

      Delete
  6. Hi, great idea!, I was wondering if you can put the complete list of the items used to do the project, it will be of great help. :-)

    ReplyDelete
    Replies
    1. The parts list is below. Just search Google for availability, but either Mouser or Digikey or should have all these items.

      - 1 x Arduino - whichever you want to use
      - 1x momentary switch
      - 1 x TIP102 NPN Power Darlington Transistor
      - 1 x Sparkfun Breadboard power supply (PRT-00114)
      - 1 x 1N4004 Diode
      - 1 x 2N2222 NPN bipolar junction transistor
      - 1 x Variable resistor
      - 1 x 100 Ohm resistor
      - 1 x 2,2k Ohm resistor
      - 1 x 10k Ohm resistor
      - 1 x 1/4" NPT Electric Solenoid Valve 12-Volt DC (just search on eBay)

      Just follow the schematic and it should work.

      Delete
  7. Hi, I was wondering if you have a picture from above the diagram, because I can not understand very well how to connect all the components. Thanks in advance

    ReplyDelete
    Replies
    1. Sorry, no close ups. Again, this was nearly 5 years ago. Have a look at the link below on the moisture detector circuit.

      Delete
  8. Hey, good project, i have an issue with the diagram, I can't undertand how to make the wiring thing. I was wondering if you can help me by explaining the wiring, I hope you can help me and thanks in advance.

    ReplyDelete
    Replies
    1. Not sure what you are asking here. This should be just basic bread boarding and in the previous comment I gave a complete parts list. If you want to better understand the theory of operation, I suggest Googling for "Arduino Moisture Detector" and/or "Using Arduino with 12 VDC solenoids". I did this nearly five years ago now so I can't recall myself how this worked!

      Delete
  9. Hi, Could you please share link of Moister detector circuit schematic that you have used ?

    ReplyDelete
    Replies
    1. I am sorry I did not previously include that. The full description of the plant moisture detector project can be found at: http://www.botanicalls.com/archived_kits/twitter/.

      Delete