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);
  buttonIs = digitalRead(SwPin); //Read the initial state of the switch!

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);
    Serial.println("Valve Open");

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

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 -> 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.


  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

  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.

  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.

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

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

    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.

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

    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!!

  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. :-)

    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.

  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

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

  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.

    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!

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

    1. I am sorry I did not previously include that. The full description of the plant moisture detector project can be found at: