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!

Gardening & Homesteading Contest

Participated in the
Gardening & Homesteading Contest