Controling a Solenoid Valve With an Arduino

110,572

134

43

Published

Introduction: Controling a Solenoid Valve With an Arduino

About: A crazy mix between a physician and a mad scientist...

I need to take some vacation time and unfortunately, while I don't have kids, I have bonsais.

Like kids, these little critters need constant care and especially, water.

Each day.

Argh!

Why can't they drink beer/wine by themselves?

Anyways... Faced with no one close enough or willing enough to take care of them, I decided to use solenoid valves and an Arduino to create my own watering system.

At this point, it is easier and cheaper to buy an industry-made timer.

However, the final project will include watering based on a schedule and/or humidity in the earth, and a wireless logging system that will tell me while I'm away if everything is working as expected.

But let's start with the basic project, as this may be more than enough for the regular plant...

Step 1: Backgound Information

I will not talk (again) in details of the back EMF, why it is nasty and why you need a flywheel diode, as I already discussed that in another instructable here. You need that diode!

Solenoid valve : It is nothing more than a valve controlled by an electromagnet. It is, like relays and motors, an inductive load (aka an IC buster, go read on back EMF if it is not already done!). They usually come in two flavors : Normally Open or Normally Closed. Normally refers to when-there-is-no-current-in-the-solenoid. If you put pressurized water in a NC (Normally Closed) solenoid valve, water will be blocked. If you power the magnet with the expected current / voltage, the valve will open and the water will flow.

For NO (Normally Open), it is exactly the contrary.

Now, this is not the only variable. When current is flowing in the magnet, it creates heat. Most solenoid valves are not for continuous use. They have to rest and cool down between uses or they will fry. Read the small prints before committing to buying! And think about you project : are you watering a garden once or twice a day or are you filling an Olympic swimming pool with a garden hose?

MOSFET : In a nutshell, a MOSFET is like a light switch. You apply some voltage to the GATE terminal and the resistance between the DRAIN and the SOURCE will drop, allowing a lot of current to pass. Heat will be generated by the tiny (but real) internal resistance and you will need to dispose of it or risk melting your MOSFET. Also, by design, nothing being perfect, they are quite vulnerable to static electricity.

Here, we will use a N-Channel MOSFET, so don't be surprised if you see it connected between the solenoid and the ground.

Power supply : As you can see, I only use one 12V lead-acid battery in this project to power both the Arduino and the solenoid. Usually, this is not a good idea...

Let me explain :

Your Arduino, according to the official site, can deal with 7-20 volts and they recommend 7-12V, explaining that more than 12V can result in the board overheating. The reason is that the Arduino uses a linear voltage regulator that will get rid of the extra voltage by making some heat with it. If you give 7V 0.5A to a linear regulator like the LM7805, you get 5V and 2Vx0.5A= 1W of heat. If you give it 12.6V (typical voltage for a fully-charged lead-acid battery), you need to get rid of 3.8W of heat. And do you see any heat sink on the board? No? Actually, the board is the (very limited) heat sink.

This is the reason why I used a 85% efficient DC-DC buck converter that I had previously made. As it is quite another project to built a switching regulator, I will provide you with two clues. The first one (the inspiration for my own project) was a project published in Nuts and Volts in June 2008 by Jim Stewart. I modified their original design using information provided in the official datasheet of the LM2576. If you are in a rush to built one, use the schematic on page 11 (figure 22) and read everything, paying extra attention on the layout guidelines, stick to the recommended hardware and it will work. If you are not in a rush, I'll try to publish an instructable on it when I'm finished with this watering thing...

Step 2: Getting the Stuff...

This a simple project... The toughest part was not to find is the solenoid valves, but the adapters...

Hardware

Solenoid valve : I got these 12V / 0.4 A NC valves quite cheap on Ebay . They are working fine on domestic pressure. However, I should have pay a little bit more for a valve threaded directly for garden hoses. See the next point...

Adapters : As my solenoid valve had threads for 1/2" (while a normal garden hose is 3/4") I needed two adapters and a female/female adapter to connect two hoses together. I got them from a general hardware store, Believe it or not, together these are the most expensive pieces of this project!

What I was looking for (now I know, but I had to try many different adapters!) was two 3/4" MH x 3/4" MIP x 1/2" tapped FIP. I have no idea what it means, but this is what you are looking for and you need two of them! You will also need a 3/4" hose x 3/4" hose solid bass double female swivel adapter.

A power source : I use a 12V lead acid battery for this project to power both the Arduino and the solenoid. The unidentified piece of circuitry is a home made switching regulator. Go back to the last step, background information, if you don't know why it is there!.

MOSFET : I used a FQP50N06L for this project. Looking at the datasheet, it is probably overkill here as nothing more than 12V 0.5A will be used.On the positive side, I don't have to worry for thermal derating...

LED (Any color) : Any LED will do, it is mostly for debugging purpose.It will be on when the MOSFET is on, but it should be obvious if it is working as some water should be flowing...

Resistor : I used a simple 330 Ohm resistor that was lying on my bench. The idea is to make sure no more than 20 mA will flow through the LED. If you omit the LED, omit the resistor.

Diode : That is the one guy you should not omit. A normal 1N4007 was used.

Wires : To connect everything

Breadboard : To support everything

Software

Arduino IDE

Step 3: Assembling the Stuff

As I said previously, this is an easy built.

  1. Get grounded and discharge all static electricity you could have on you. I work on conductive foam and I always touch some part of my working bench mostly made of steel.
  2. Put your MOSFET in your breadboard. The TO-220 packaging may need a little push to get the leads in the breadboard.
  3. With the black part of the MOSFET (with the writing) facing you, the pin on the left is the GATE, the one in the middle is the DRAIN and the one on the right is the SOURCE.
  4. Connect your Arduino digital pin 2 to the GATE
  5. Connect the positive terminal of the 12V battery to the positive side of the solenoid
  6. Connect the negative terminal of the solenoid to the DRAIN
  7. Connect the SOURCE to the negative terminal of the battery

If you chose to use a LED :

  1. Stick the LED in the breadboard
  2. Stick the resistor in the breadboard
  3. Connect the resistor to your Arduino digital pin 10
  4. Connect the negative terminal of you LED to GND

Don't power up anything just yet! Did you forget something? Where is your diode???

It should be connected between the solenoid terminals, so that the little line on the diode is closest to the positive terminal of the solenoid. I decided to put the diode very close to the solenoid because :

  1. There are two very convenient little holes waiting for the diode in the crimps;
  2. I remember reading something about putting diodes as close as possible to the inductive load, but I don't remember the details. Any references anyone?

Step 4: A Simple Sketch

This is the "blink" sketch adapted for a MOSFET. Load the code in the Arduino IDE and read the comments. This is *not* rocket science.

Step 5: To Heat Sink or Not to Heat Sink... the Same Redundant Question...

After trying my circuit at 28 degrees Celcius in the sun, I registered a max temp for the MOSFET of 35 degrees and a max temp of 55 degrees Celcius for the solenoid valve after three cycles of one minute back to back, with only five seconds without power (using the sketch in the previous step). Considering I will only use this circuit to water the plants for a minute or two each day, there is no need for a heat sink.

Now, if your idea is to transform your backyard into a swamp to have a pet gator, I strongly suggest :

  1. Find another pet (Gators hate Canadian winter);
  2. Check the temp both of your MOSFET and your solenoid;.
  3. Remember that temperature increase is usually not linear;
  4. Install a big fat heat sink and a fan to be on the safe side;

Step 6: Scale It Up!

My final design will involve 3 different zones, each controlled by a MOSFET. Even if you have tons of pressure in your hoses or very short ones, it is not a good thing to use all the solenoids at the same time. For one thing, if you are running this on batteries like me, the less juice you pull from your battery, the closest you will be to that expected Holy Grail of the Amp/Hour number written on the battery. Yep, manufacturers don't tell you this, but batteries are not linear : the more you ask from them, the less they will give you in the end!

To scale this to more zones, just use one digital pin per MOSFET. If you need more, it is possible, but I'm not there yet (I have a backyard, not a golf course!).

Talking of batteries. as the Arduino is drawing 42 mA 24/7 and my solenoid is drawing 400 mA for 2 minutes each day, my fully charged 12V 9 Amp/Hour should last more than our Montreal summer...

With a 9 Amp/Hour fully charged, you have access to 108 000 mW/h of energy (hum, probably less, as you don't want to deep discharge (and kill) you battery). However, considering the 4.8W for 2 minutes (hence 160 mW/h in average) plus the 0,247 W/h needed by the Arduino powered by a 85% efficient switching power supply, you have a consumption of 407 mW/h...

  • With one solenoid. it means 265 days of use;
  • With two solenoids, it means 190 days of use;
  • With three solenoids, it means 148 days of use.

The cool thing is that I just found a nice little 12V, 12 Amp/h battery... Should give me more than a summer even with three zones! So I'm not in such a hurry to design my next project; a solar charger!

Step 7: In Conclusion...

This is a work in progress ; the final version including a three zones and a much nicer sketch should be posted soon. But for now, you do have the tools and the knowledge to create your own watering system!

2 People Made This Project!

Recommendations

  • Oil Contest

    Oil Contest
  • Make it Move Contest

    Make it Move Contest
  • Woodworking Contest

    Woodworking Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

2 Questions

Could someone help me with this I'm working on this project with an Arduino Uno and using a 1.5inch valve will there be any problem to power it up and about the heat generated

Do you happen to have a sketch of what this looks like with everything connected in? Like a working drawing of sorts?

43 Comments

Hello, like you said, finding the right kind of adapters seems to be the hardest part for me right now. I have been testing out the 12 V DC solenoid valve I purchased online, I bought the adapters as shown in the picture below. For now, I connected a hose pipe directly to the input side adapter. But water leaks between the brass and stainless steel adapters on the input side. The output side is fine. I did not think I had to mimic your adapters exactly as you use them since I bought a hose pipe that fits well with the brass adapter I bought, and plan to use a hose clamp to keep it tightly in place. Any idea why I am seeing the water leak? Could it be because my Solenoid valve (looks low on quality)? Could you explain why you needed the female-female adapter? Is that on the input side or the output side?

solenoid valve.jpg
5 replies

it could either be the valve or one of the hoses not clamping on tightly enough.

be sure to screw them on quite tight to make sure that there's less chance. the value and fittings I got don't leak at all.

(and yep I do got it mounted on a used pringles can, it was just the right height for where I wanted the value)

20171111_010423.jpg

PTFE tape...lots of it. Just saying :)

I had to do some trial and error with various attachments and hose diameters. In the end I used clamps on any leaky joints and have my system up and running. It is all hooked up to a raspberry pi and waters multiple plants on a timed basis ( I can also turn it on remotely when I need to and for however long I need it to run.) Saved me when I had to travel recently. All my plants are alive and well.

I recently learned that hose threads could be of two types, one being tapered and not the other. I think your leak is coming from trying to mix the two.

I had to do some trial and error with various attachments and hose diameters. In the end I used clamps on any leaky joints and have my system up and running. It is all hooked up to a raspberry pi and waters multiple plants on a timed basis ( I can also turn it on remotely when I need to and for however long I need it to run.) Thanks for your help.

Good to know this is working now! I plan to revisit this project with an esp32.

Sir, could you please send me the circuit diagram to my mail 'muneermohammadali@gmail.com as I am working on a similar project. Thank you :-)

Could you please share the circuit diagram with me "g.rajksingh@gmail.com". I would really appreciate it. I am planning to work on a similar project and need this.

I'm running a similar project (early stage) but would like to use latching solenoid to limit wiring in my garden. Has anyone any idea where can I find a latching 1/2-3/4" plastic valve for a reasonable price? Some Chinese supplier?

I cannot see the connections, do you have lage view or circuit scale?

Can i get your email? I was brainstorming a way to manage multiple water supply valves in ground ( under mobile homes) using either arduino or pi to control valve then adding leak detection and excess flow reporting to activate valve but also needed wireless or remote activation to close the normally open valve with one with a home connected but normally closed otherwise. Im wanting to build a community project to help small operators connect to each valve through a GUI and open or close remotely and to receive flow meter ticks to compile and track usage.

Ideas welcome.

Eric

2 replies

Look around, already been done. There is a user in India who did pretty much the same for a small community where his parents live. I just don't remember the link...

I'll keep searching - if you find it please let me know - very interested.

Hi,

Being a newbie, I accidently bought the wrong MOSFET as it turns out. I bought an FQP27P06, which is according to its datasheet a P Channel MOSFET, whereas you are using an N Channel MOSFET.

I will try to make this project work anyway, with the tools I now have at hand. After sitting at my desk with my hands in my hair for a considerable amount of time, I now understand that the main operational difference is that N Channel MOSFETs let current flow from DRAIN to SOURCE, while P Channel MOSFETs let the current flow in the opposite direction, from SOURCE to DRAIN.

I am about to find out whether my last remaining MOSFET will be fried when I will switch the cables that connect to DRAIN and SOURCE on the breadboard.

If you don't see me following this up with YEEHAAA or something equivalent, could you please explain why that didn't work, and maybe how anybody with a P channel MOSFET at hand could make this work?

5 replies

OK, I was able to buy an N-channel MOSFET: IRFZ44N

http://www.irf.com/product-info/datasheets/data/ir...

However, the circuit doesn't work, and I don't understand why...

I checked the MOSFET, it should be fully functional, according to the tests with my multimeter.

The solenoid seems fine: when i connect it directly to the battery leads, I can distinctly hear the valve.

The arduino is programmed correctly as far as I can tell. When I test the voltage between digital pin2 and GND, I get 0v and 5v with 5 second intervals.

When I connect everything... nothing happens.

Ideas, anybody? Are the specs of the MOSFET up to the task?

Do you have a pulldown resistor on the gate?

The specifications of my parts:

solenoid valve: 12v dc 8.5w // 12v ac 4.4va
diode: IN4006
battery: 12v 2.0AH/20hr
MOSFET: IRFZ44N

Hmm, it doesn't seem to work. Bugger :(

The problem was that I was working with two power sources. Apparently, the rule there is to always connect both grounds to get stable voltages. If you mix two power sources you will get a voltage that floats somewhere in between, which, well, makes you waste a lot of time wondering where it all goes wrong. :-)