A solenoid valve is an electrically operated valve. Current through a wire coil creates a magnetic field that pulls a metal plunger, and that plunger opens or closes a path for water, air, gas or oil. To control one with an Arduino, you switch the coil’s own power supply through a logic-level MOSFET or a relay, and you add a flyback diode. You never power the coil straight from a pin.
This guide explains how solenoid valves work and how normally closed and normally open types differ. It also covers how to pick one that actually suits a hobby build, then walks through a tested 12 V Arduino circuit with code.
What Is a Solenoid Valve?
A valve and solenoid are bolted together into one unit. The solenoid is the electrical half: a coil of enamelled copper wire wound around a hollow tube. The valve is the mechanical half: a body with an inlet port, an outlet port, and an orifice between them that a seal can block.
Inside the tube sits a ferromagnetic plunger, also called the armature or core. A spring holds the plunger in its resting position. When you energise the coil, the magnetic field pulls the plunger into the coil against the spring. When you cut the power, the spring pushes it back.
The main parts are:
- Coil: the electromagnet. It is rated for a specific voltage and current type, such as 12 V DC or 24 V AC.
- Plunger (armature): the moving iron core that carries the seal or lifts off the orifice.
- Spring: returns the plunger when power is removed. It sets the valve’s resting state.
- Seal: usually NBR (nitrile), EPDM or FKM (Viton) rubber, chosen to suit the fluid.
- Valve body: brass, stainless steel or plastic, with threaded ports. Hobby valves usually use 1/4″, 1/2″ or 3/4″ BSP or NPT threads.
- Orifice: the hole the fluid passes through. Its size limits flow.
If you have read about electric motor parts, the coil and armature will look familiar. A solenoid uses the same electromagnetism as a motor, but it produces a short straight-line pull instead of rotation.
How Solenoid Valves Work
Every solenoid valve does the same basic job: it converts an electrical signal into a mechanical open or close. How the plunger actually moves the fluid depends on the operating principle. This is the spec that trips up most makers.
Direct-acting valves
In a direct-acting valve, the plunger itself seals the main orifice. Energise the coil, the plunger lifts, and fluid flows. These valves work from zero pressure upward, so they are the right choice for gravity-fed water or low-pressure air.
The trade-off is size. The coil has to fight the full line pressure pushing on the seal. Direct-acting valves therefore tend to have small orifices or need large, power-hungry coils for big ports.
Pilot-operated (servo-assisted) valves
Most cheap 1/2″ water valves sold for Arduino projects are pilot-operated. The plunger only opens a tiny pilot hole. That changes the pressure balance across a rubber diaphragm, and the line pressure itself lifts the diaphragm off the main orifice.
This lets a small coil switch a large flow. It also means the valve needs a minimum pressure difference between inlet and outlet to work, often somewhere around 0.3 to 0.5 bar (roughly 5 to 7 psi). Check the datasheet for your model. Below that, the diaphragm will not lift or seal properly.
Here is a concrete benchmark: 1 metre of water height produces only about 0.1 bar. A rain barrel on a 1 m stand will not run a typical pilot-operated valve. It will click, and very little water will come out.
Semi-direct (assisted lift) valves
Semi-direct valves combine both methods. The plunger is mechanically linked to the diaphragm, so it pulls the diaphragm open at zero pressure. Line pressure then helps at higher pressures. They cost more, but they suit systems where pressure varies from nothing up to mains level.
2-way and 3-way valves
A 2-way valve has two ports (in and out) and simply starts or stops flow. A 3-way valve has three ports and switches flow between paths. A common use is pressurising and then exhausting a pneumatic cylinder. For water projects you will almost always use 2-way valves.
Normally Closed vs Normally Open Solenoid Valves
The “normal” state is what the valve does with no power applied. This one choice determines what happens when your Arduino crashes, your supply fails or a wire falls off.
Normally closed (NC)
A normally closed valve is shut when unpowered. The spring holds the seal against the orifice. Applying power opens it.
Choose NC for anything where uncontrolled flow causes damage: irrigation, plant watering, tank filling, coffee machines, gas lines. If power fails, the valve shuts. This is the default type, and it is what you should buy for most projects.
Normally open (NO)
A normally open valve passes flow when unpowered. Applying power closes it.
NO valves suit systems that should keep flowing during a power failure, such as cooling water for equipment that would overheat without it. They also save energy when the valve spends most of its time open, because the coil draws current continuously only while it is energised to hold the valve closed. Hobby uses are rarer.
Bistable (latching) valves
Latching valves use a permanent magnet to hold either position with no power at all. A short pulse switches them one way, and a reverse-polarity pulse switches them back. Battery-powered garden timers use them because the coil draws current for only tens of milliseconds per switch.
You drive a latching valve with an H-bridge, not a single MOSFET, because you need to reverse the current.
Why Do We Use Solenoid Valves?
Solenoid valves earn their place for a few clear reasons:
- Electrical control: a microcontroller, timer or sensor can open them. No human hand is needed.
- Speed: small valves switch in tens of milliseconds, far faster than a motorised ball valve.
- Fail-safe behaviour: an NC valve closes by itself when power drops.
- Reliability: there is one moving part and no gearbox, and the coil is sealed away from the fluid.
- Cost: a plastic 12 V water valve costs a few dollars. That is cheaper than any motorised alternative.
They have limits too. They are binary (open or shut), so they cannot throttle flow the way a proportional valve can. Many pilot-operated types also need line pressure to work.
Where Do We Use Solenoid Valves?
You have probably used dozens of solenoid valves today without noticing. A few common places:
- Domestic appliances: washing machines and dishwashers use them to let water in. Fridge ice makers and water dispensers use small ones too.
- Espresso machines: they release pressure from the group head after a shot.
- Garden irrigation: zone valves from brands like Rain Bird and Hunter are 24 V AC solenoid valves.
- Heating systems: boilers use them for gas safety shut-off.
- Pneumatics: factory automation uses 3-way and 5-way valves to drive air cylinders.
- Vehicles: fuel shut-off, evaporative emissions systems and transmission control all use them.
- Aquariums: CO2 injection systems use small solenoids to stop gas flow overnight.
For makers, the classic builds are automatic plant watering, aquarium top-off, pet water fountains and drink dispensers.
How to Choose a Solenoid Valve for Arduino Projects
Before you order, read these specs on the listing or datasheet. Most “it doesn’t work” problems come from skipping one of them.
| Spec | What to look for | Why it matters |
|---|---|---|
| Coil voltage | 12 V DC is easiest for Arduino | Must match your supply exactly |
| AC or DC coil | DC for a MOSFET circuit | AC coils need a relay, triac or SSR |
| Coil power or current | Often 3 to 8 W at 12 V (check yours) | Sizes your supply and transistor |
| Operating principle | Direct-acting for gravity feed | Pilot types need minimum pressure |
| Pressure range | Minimum and maximum, in bar or psi | Mains water is usually 2 to 4 bar |
| Port size and thread | 1/2″ BSP vs NPT | BSP and NPT threads don’t seal together |
| Body material | Brass, stainless, food-safe plastic | Drinking water needs food-grade parts |
| Seal material | NBR, EPDM, FKM | Oil, hot water and chemicals attack the wrong rubber |
| Duty cycle | 100% ED for continuous use | Some coils overheat if left on |
| Normal state | NC for most projects | Defines fail-safe behaviour |
Look for the flow arrow cast into the body. Pilot-operated valves only work in one direction, and fitting one backwards is a very common mistake.
Mains-voltage valves
Many solenoid valves come with 110 V or 230 V AC coils. Do not wire these into an Arduino project unless a qualified electrician designs and installs the mains side. Buy a 12 V DC or 24 V AC version instead.
Project: Control a 12V Solenoid Valve with Arduino
This build switches a 12 V DC normally closed valve from an Arduino Uno. A pushbutton or the Serial Monitor controls it, and a software timer shuts it after 60 seconds so a bug can’t flood your bench.
Parts
| Part | Example | Notes |
|---|---|---|
| Microcontroller | Arduino Uno R3 | Any 5 V Arduino works |
| Solenoid valve | 12 V DC, NC, 1/2″ | Plastic hobby type is fine |
| MOSFET | IRLZ44N (TO-220) | Logic-level, specified for 5 V gate drive |
| Flyback diode | 1N4007 or 1N5819 | 1N5819 Schottky is better if you use PWM |
| Gate resistor | 220 Ω | Limits current into the gate |
| Pull-down resistor | 10 kΩ | Keeps the valve off during boot |
| Power supply | 12 V DC, 2 A | At least 1.5 times the coil current |
| Pushbutton | 6 mm tactile switch | Optional |
| Capacitor | 470 µF, 25 V electrolytic | Optional, steadies the 12 V rail |
| Breadboard, jumpers, DC barrel jack adapter | Any | Use thicker wire for the 12 V loop |
Why not a cheaper transistor? A standard IRF540N or IRFZ44N needs around 10 V on its gate to switch fully on. At 5 V it only half turns on, gets hot and may not open the valve. The “L” in IRLZ44N means logic level, and that letter is the whole point.
Wiring
The IRLZ44N pinout, viewed from the front (printed side facing you, legs down), is pin 1 Gate, pin 2 Drain, pin 3 Source.
| From | To | Wire purpose |
|---|---|---|
| Arduino D9 | 220 Ω resistor, then MOSFET pin 1 (Gate) | Switching signal |
| MOSFET pin 1 (Gate) | 10 kΩ resistor, then GND | Pull-down, valve stays off at reset |
| MOSFET pin 3 (Source) | Common ground rail | Return path for coil current |
| MOSFET pin 2 (Drain) | Valve terminal B | Low side of the coil |
| 12 V supply + | Valve terminal A | Coil supply |
| Diode cathode (striped end) | Valve terminal A (+12 V) | Flyback protection |
| Diode anode | Valve terminal B (Drain side) | Flyback protection |
| 12 V supply − | Common ground rail | Shared ground |
| Arduino GND | Common ground rail | Shared reference with the MOSFET |
| Arduino D2 | Pushbutton, then GND | Toggle input with internal pull-up |
| 470 µF capacitor + / − | 12 V + / ground, near the valve | Absorbs current spikes |
Most DC valves have no polarity marking on the coil, so terminal A and B are interchangeable. The diode direction is what matters. If you get it backwards, it shorts your 12 V supply the moment the MOSFET switches on.
Power check
An Uno pin is rated for 40 mA absolute maximum and 20 mA in normal use, while a 12 V valve coil can draw several hundred milliamps. The coil must run from the 12 V supply through the MOSFET, never from a pin or the 5 V header.
Build steps
- Seat the IRLZ44N on the breadboard with its printed face toward you.
- Wire the 220 Ω gate resistor from D9 and the 10 kΩ pull-down to ground.
- Connect the valve between +12 V and the MOSFET drain.
- Fit the flyback diode across the valve terminals, stripe toward +12 V.
- Join the 12 V supply ground, MOSFET source and Arduino GND.
- Add the pushbutton between D2 and GND.
- Upload the sketch with only USB connected, then plug in the 12 V supply.
- Open the Serial Monitor at 115200 baud and send “o” to test.
Maker tip
Once the circuit works, take the diode off the breadboard and solder it directly across the valve’s terminals with your soldering iron, sleeved in heatshrink. The diode then travels with the valve, so it can never be forgotten when you rewire.
Code
This sketch compiles on the Arduino IDE 2.x with the standard AVR core. It needs no extra libraries.
// Solenoid valve control: Arduino Uno R3 + IRLZ44N logic-level MOSFET
// D9 drives the MOSFET gate, D2 reads a pushbutton to GND.
const uint8_t VALVE_PIN = 9;
const uint8_t BUTTON_PIN = 2;
const unsigned long MAX_OPEN_MS = 60000UL; // safety cutoff: 60 seconds
const unsigned long DEBOUNCE_MS = 50;
bool valveOpen = false;
unsigned long openedAt = 0;
int lastReading = HIGH;
int stableState = HIGH;
unsigned long lastChange = 0;
void setValve(bool open) {
valveOpen = open;
digitalWrite(VALVE_PIN, open ? HIGH : LOW);
if (open) {
openedAt = millis();
Serial.println(F("Valve OPEN"));
} else {
Serial.println(F("Valve CLOSED"));
}
}
void setup() {
pinMode(VALVE_PIN, OUTPUT);
digitalWrite(VALVE_PIN, LOW); // start closed
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(115200);
Serial.println(F("Ready. Press the button, or send o (open) / c (close)."));
}
void loop() {
// Debounced pushbutton toggles the valve
int reading = digitalRead(BUTTON_PIN);
if (reading != lastReading) {
lastChange = millis();
lastReading = reading;
}
if ((millis() - lastChange) > DEBOUNCE_MS && reading != stableState) {
stableState = reading;
if (stableState == LOW) {
setValve(!valveOpen);
}
}
// Serial commands
if (Serial.available() > 0) {
char c = Serial.read();
if (c == 'o') setValve(true);
if (c == 'c') setValve(false);
}
// Safety timeout: never leave the valve open forever
if (valveOpen && (millis() - openedAt >= MAX_OPEN_MS)) {
Serial.println(F("Timeout reached"));
setValve(false);
}
}
How it works
When D9 goes HIGH, 5 V reaches the MOSFET gate through the 220 Ω resistor. The MOSFET turns fully on, and current flows from +12 V, through the valve coil, through the MOSFET drain to source, and back to ground. The plunger lifts and the valve opens.
When D9 goes LOW, the MOSFET turns off. The coil’s magnetic field collapses, and an inductor resists sudden changes in current. Without the diode, that collapse throws a voltage spike of tens or even hundreds of volts onto the drain. That spike can kill the MOSFET or reset your Arduino. The diode gives the current a safe loop back through the coil until it dies away.
The 10 kΩ pull-down matters during the second or so after power-up. During that time D9 is still an input and floating. Without the pull-down, the gate could pick up noise and briefly open the valve.
The shared ground is not optional. The MOSFET switches based on the voltage between gate and source. If the Arduino ground and the 12 V ground aren’t joined, that voltage is undefined and the valve won’t respond.
Using a Relay Module Instead
A relay module is the easier option in two cases. The first is a 24 V AC irrigation valve, because a MOSFET cannot switch AC. The second is when you want full electrical isolation between your controller and the valve circuit.
Wire the valve’s supply through the relay’s COM and NO (normally open) contacts. For a DC valve, still fit a flyback diode across the coil, because the spike would otherwise arc across the relay contacts and wear them out. For a 24 V AC valve, skip the diode, since it would short half of every AC cycle. A small RC snubber or a varistor across the coil does the same job on AC.
Power a 24 V AC valve from a plug-in irrigation transformer, so you never handle mains wiring yourself.
Driving Valves from ESP32, Raspberry Pi and Pico
The ESP32, Raspberry Pi and Pico all use 3.3 V logic. The IRLZ44N is specified for 5 V and 4 V gate drive, and at 3.3 V it is marginal. It may work on your bench and then run warm in a sealed box.
Pick a MOSFET specified for 2.5 V gate drive instead, such as the AO3400A (SOT-23, so you’ll need a breakout board). The other option is a ready-made MOSFET driver module listed as 3.3 V compatible. Our general purpose input output guide covers which pins on each board are safe to use at boot, which matters here. Some ESP32 pins pulse briefly during startup, and that pulse would twitch your valve.
To drive several small valves, a ULN2803 Darlington array is a handy integrated circuit. It has eight channels, each rated up to 500 mA, and the flyback diodes are built in. Connect its COM pin to your +12 V rail to enable those diodes. Check that your valve’s current sits well under the limit, because the Darlington drop of around 1 V also turns into heat.
Cutting Coil Heat with PWM Hold Current
This trick is rarely mentioned on valve spec pages. A solenoid needs far more force to pull the plunger in than to hold it there, because the magnetic gap is much smaller once the plunger is seated. You can energise the coil at full power for a moment, then drop to a lower PWM duty cycle to hold it.
// Pull in at full power, then hold at roughly 50% duty.
// Use a PWM-capable pin (D9 is PWM on the Uno).
void openWithHold() {
digitalWrite(VALVE_PIN, HIGH); // full current to pull the plunger in
delay(150); // give it time to seat
analogWrite(VALVE_PIN, 128); // about 50% duty to hold
}
void closeValve() {
analogWrite(VALVE_PIN, 0);
digitalWrite(VALVE_PIN, LOW);
}
Halving the duty cycle roughly halves the average current, so the coil runs much cooler and your supply works less hard. Test this with your specific valve under real line pressure. Many valves are only rated to hold down to around 70% of their rated voltage, so 50% may let the plunger drop out and you may need 70% or more.
The Uno’s default PWM on D9 runs at about 490 Hz, so expect an audible hum. With PWM, the flyback diode conducts on every cycle, so use the 1N5819 Schottky rather than the slower 1N4007.
Troubleshooting
The valve clicks but little or no water flows. It is almost certainly a pilot-operated valve without enough pressure. Raise the supply, switch to a direct-acting valve, or confirm the flow arrow points the right way.
There is no click at all. Measure 12 V across the valve terminals while D9 is HIGH. If you read 0 V, check that the Arduino GND and 12 V supply ground are joined. Also confirm you used a logic-level MOSFET and identified its legs correctly.
The Arduino resets or the Serial Monitor freezes when the valve switches. The flyback diode is missing, backwards or too far from the coil. Add the 470 µF capacitor near the valve. Keep the 12 V wiring short and route it away from signal wires.
The MOSFET gets hot. It isn’t fully on. Use a logic-level part, check the gate resistor isn’t too large, and make sure PWM isn’t running at a very low duty cycle.
The coil gets very hot. Coils on continuous duty often run hot enough to be uncomfortable to touch, and that’s within spec for many valves. If yours is rated for intermittent duty, shorten the on-time or use PWM hold current.
The valve drips when closed. Debris on the diaphragm or seal is the usual cause. Fit an inline mesh filter upstream and flush the valve.
The valve buzzes loudly. If the PWM trick isn’t the cause, check whether you’re running an AC coil on DC or a DC coil on AC. Both fail, and the coil can burn out.
Next Steps
Once a single valve works reliably, it becomes the output stage for plenty of practical builds:
- Add a capacitive soil moisture sensor and water a plant only when it’s dry.
- Add a hall-effect flow sensor, such as the YF-S201, to measure exactly how much water passed through.
- Show valve state, flow and timer countdown on a 16×2 liquid crystal display (LCD).
- Move to an ESP32 to schedule watering over Wi-Fi and log each run.
- Run several zones from one supply with the ULN2803 or a multi-channel relay board.
Keep the 60-second safety timeout, or something like it, in every version. A stuck valve is a flooded floor.
FAQ
What is a solenoid valve in simple terms?
It’s a tap that a coil of wire opens and closes instead of a hand. Power the coil, and its magnetism pulls a plunger that opens or shuts the flow path. Cut the power, and a spring returns it.
Can an Arduino power a solenoid valve directly?
No. A pin can safely supply about 20 mA, and even small solenoid valves draw hundreds of milliamps at 12 V. Use the pin to switch a logic-level MOSFET or a relay, and power the coil from a separate supply.
Why does a solenoid valve need a diode?
The coil is an inductor. When it switches off, its collapsing magnetic field produces a large voltage spike. A flyback diode across the coil gives that energy a harmless path and protects your MOSFET, relay contacts and Arduino.
Should I buy a normally closed or normally open valve?
Buy normally closed for almost every hobby project. It shuts off flow when power fails or your code crashes, which is what you want for watering, filling and dispensing.
Will a 12V solenoid valve work with a gravity-fed water barrel?
Only if it is direct-acting and rated for zero pressure. Most cheap pilot-operated valves need a few psi, and a barrel 1 m off the ground gives only about 0.1 bar (around 1.4 psi).
Can I run a DC solenoid valve on AC, or the other way round?
No. A DC coil on AC chatters and draws the wrong current. An AC coil on DC loses the impedance that limits its current, so it overheats and can burn out. Match the supply to the coil label exactly.
How long can a solenoid valve stay switched on?
Valves rated for 100% duty cycle can stay energised indefinitely, though the coil will run warm. Intermittent-duty valves need rest periods, so check the datasheet. PWM hold current helps in both cases.









