Arduino UNO R4 - Water/Liquid Valve
In this tutorial, we will learn how to control the flow of liquids like water, beer, and oil using an Arduino UNO R4 and a solenoid valve. In detail, we will learn:
- How to connect the solenoid valve to Arduino UNO R4
- How to program Arduino UNO R4 to control the valve.
- How to switch the same valve with a MOSFET module in place of the relay.
Both a relay and a MOSFET module can open and close the valve. A solenoid valve is only open or closed, so the MOSFET module does not give extra control here. Its gain is different: it switches with no click, it reacts faster, and it has no metal contacts that wear out over time.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables STEM V4 IoT Starter Kit (Arduino included) | |
| 1 | × | DIYables STEM V4B IoT Starter Kit (Arduino included) | |
| 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
Pinout

A solenoid valve typically has two connections.
- Connect the positive (+) pin (red) to the 12V DC power supply.
- Connect the negative (-) pin (black or other) to the GND of the DC power supply.
How Water/Liquid Valve works
Usually, the valve is shut. When 12V DC is connected to the two points, the valve opens, allowing water or other liquids to pass through.
※ NOTE THAT:
Some valves have a gasket inside, and they need a minimum pressure to open after applying 12V DC. This pressure can be generated by the flow of liquid. Additionally, some valves allow liquid to flow in only one direction.
How to Control Water/Liquid Solenoid Valve
When the valve receives 12V power, it opens. To manage the valve, a relay is required between the Arduino UNO R4 and the valve. The Arduino UNO R4 uses the relay to operate the solenoid valve. If you are unfamiliar with how a relay functions, its pin configuration, or how to program it, you should read the Arduino UNO R4 - Relay tutorial.
A MOSFET module can do the same job as the relay. You can find its pinout and how it works in the Arduino UNO R4 - MOSFET Module tutorial.
Relay vs MOSFET Module for a Solenoid Valve
| Relay | MOSFET Module | |
|---|---|---|
| Valve control | open or closed | open or closed (no PWM for a valve) |
| Sound of the switch | clicks | silent |
| Switching speed | milliseconds | much faster |
| Wear | contacts wear after many cycles | no moving contacts |
| Valve power | AC or DC | DC only |
| Supply voltage | any, within the relay rating | at least about 6V |
| Isolation | coil is apart from the contacts | PC817 optocoupler |
| Good choice for | AC valves | 12V DC valves that open and close often |
Both modules control the valve in the same way, so pick the one that fits your project. A 12V DC valve that opens many times a day, or sits in a quiet room, works well with the MOSFET module. An AC valve needs the relay.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Arduino UNO R4 Code
This code will turn the water valve ON for five seconds and then turn it OFF for five.
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Connect the Arduino Uno R4 to the water valve according to the provided diagram.
- Connect the Arduino Uno R4 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Copy the provided code and paste it into the Arduino IDE.
- Click the Upload button in the Arduino IDE to transfer the code to the Arduino UNO R4.
- Monitor the water flow.
Code Explanation
The explanation is available in the comments within the provided Arduino code.
How to Control Water/Liquid Valve with MOSFET Module
You can swap the relay for a MOSFET module. The valve still only opens and closes, but now it happens silently. On this page, the relay and the MOSFET module use different pins: the relay uses pin 3 and the MOSFET module uses pin D9. So each wiring has its own code.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Uno R4 and other components.
| From | To |
|---|---|
| Arduino UNO R4 pin D9 | MOSFET module PWM |
| Arduino UNO R4 GND | MOSFET module GND |
| 12V adapter + | MOSFET module + |
| Valve + (red) | MOSFET module + (together with the adapter +) |
| Valve − | MOSFET module LOAD |
| 12V adapter − | MOSFET module − |
The valve has a coil inside, so a flyback diode is a must. Place a 1N4007 or 1N5819 diode across the valve, with the stripe (cathode) on the valve + side.
Arduino UNO R4 Code
The code is the same idea as the relay code: HIGH opens the valve and LOW closes it. It opens the valve for 5 seconds, then closes it for 5 seconds, again and again.
A solenoid valve needs full power or no power. Never send a PWM signal to it. This is why this page does not use the fade code from the MOSFET module tutorial.
Detailed Instructions
- Wire the Arduino UNO R4, the MOSFET module and the valve as shown in the diagram above.
- Connect the Arduino UNO R4 to your computer with a USB cable.
- Open the Arduino IDE and select the board and COM port, like in the relay Quick Steps above.
- Copy the code above, paste it into the Arduino IDE and click Upload.
- Watch the water flow: the valve opens for 5 seconds and closes for 5 seconds.
※ NOTE THAT:
The MOSFET module works with DC valves only, and the valve supply must be at least about 6V. For an AC valve, keep using the relay.