Arduino Nano - Water/Liquid Valve
This tutorial instructs you how to use an Arduino Nano and a solenoid valve to control the flow of liquids such as water, beer, and oil. The same principles apply for controlling the flow of gas.
To open and close the valve, the Nano can use either a relay or a MOSFET module. Both of them only open or close the valve, because a solenoid valve works with ON/OFF only. The MOSFET module has other gains: it switches without a click, it reacts faster, and it has no contacts that wear out. This tutorial covers both ways.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Water/Liquid Valve
The Water/Liquid Valve Pinout

A solenoid valve typically has two terminals:
- The Positive (+) pin (which is usually red) should be connected to the 12V of a DC power supply
- The Negative (-) pin (which is usually black or another color) should be connected to the GND of a DC power supply
How Water/Liquid Valve works
Typically, the valve is shut. When 12V DC is supplied to the two terminals, the valve opens and liquid/water can pass through.
※ NOTE THAT:
- A gasket arrangement is inside, so a minimum pressure is necessary to open the valve after 12V DC is applied. This pressure can be generated by the flow of liquid.
How to Control Water/Liquid Solenoid Valve using Arduino Nano
If the valve is powered by a 12V power supply, it will open. In order to control the valve, a relay needs to be used between Arduino Nano and the valve. The solenoid valve can be managed by Arduino Nano through the relay.
If you are not familiar with relays (pinout, how it works, how to program, etc.), please refer to the Arduino Nano - Relay tutorial for more information.
If the MOSFET module is new to you, check the Arduino Nano - MOSFET Module tutorial to learn its pins and how it works.
Relay vs MOSFET Module for Solenoid Valve
| Relay | MOSFET Module | |
|---|---|---|
| Valve control | open or closed (ON/OFF) | open or closed (ON/OFF) |
| Noise | the relay clicks on each switch | quiet, only the valve itself makes a sound |
| Switching time | slow, some milliseconds | very fast |
| Wear | contacts wear out after many cycles | no moving contacts |
| Valve power | AC or DC | DC only |
| Valve supply | any (within rating) | about 6V or more |
| Isolation | yes, coil and contacts | yes, PC817 optocoupler |
| Suits | AC valves, simple projects | 12V DC valves that open and close often |
Do not send a PWM signal to a solenoid valve. It needs full power to open. For a 12V DC valve that switches many times a day, the MOSFET module is a good choice. For an AC valve, use the relay.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Pushing jumper wires into the headers is quick, but the wires can slip out. You can plug the Arduino Nano into a Screw Terminal Expansion Board for Arduino Nano and connect the wires to its screw terminals. This is how it looks with the screw terminal board:

This image is created using Fritzing. Click to enlarge image
Arduino Nano Code for Controlling Liquid Valve
The code below turns the water valve ON for five seconds and OFF for five seconds.
Detailed Instructions
- Plug the USB cable into the Arduino Nano and the PC.
- Open the Arduino IDE, select the appropriate board and port.
- Copy the code and open it in the Arduino IDE.
- Click the Upload button in the Arduino IDE to compile and upload the code to the Arduino Nano.
- Measure the water flow rate.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!
How to Control Solenoid Valve with MOSFET Module
You can also open and close the valve with a MOSFET module in place of the relay. The valve still works as ON/OFF only, but the switching is silent and has no contacts to wear out.
Please note that the relay and the MOSFET module do not share a pin on this page. The relay is on D2, while the MOSFET module is on D9. Each wiring therefore comes with its own code.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
| From | To |
|---|---|
| Arduino Nano pin D9 | MOSFET module PWM pin |
| Arduino Nano GND | MOSFET module GND pin |
| 12V adapter + and valve + | MOSFET module + (both on this one terminal) |
| Valve − | MOSFET module LOAD |
| 12V adapter − | MOSFET module − |
Jumper wires can come loose when you move the project. To do that, mount the Arduino Nano on a Screw Terminal Expansion Board for Arduino Nano. The pins do not change. Just connect each wire to the matching screw terminal:

This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Nano and other components.
The valve has a coil, so add a flyback diode (1N4007 or 1N5819) across the valve terminals, with the stripe (cathode) on valve +.
Arduino Nano Code
The idea is the same as in the relay code: HIGH on D9 opens the valve for five seconds, and LOW closes it for five seconds.
A solenoid valve must get only full ON or full OFF, never PWM. For this reason, the fade code from the MOSFET module tutorial is not used here.
Detailed Instructions
- Wire the MOSFET module and the valve as shown in the diagram.
- Plug the USB cable into the Arduino Nano and the PC.
- Open the Arduino IDE and select the board and port, as described in the relay Quick Steps.
- Copy the code above and upload it to the Arduino Nano.
- Plug in the 12V adapter. The valve opens for five seconds and closes for five seconds, and you will not hear a relay click.
Tip: the MOSFET module works only with DC valves, and the valve supply must be at least about 6V. For an AC valve, keep the relay.