Arduino Nano ESP32 - Water/Liquid Valve
This tutorial provides instructions on how to control a liquid flow such as water, beer, oil by using Arduino Nano ESP32 and a solenoid valve. It is the same for controlling gas flow.
The valve can be switched by a relay or by a MOSFET module. Both of them simply open and close the valve. The difference is in how they switch: the MOSFET module has no click sound, it is faster, and it has no metal contacts that wear out over time. We will show the relay first, and then how to use a MOSFET module.
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
Pinout

Solenoid Valve usually has two terminals:
- Positive (+) pin (red): needs to be connected to 12V of DC power supply
- Negative (-) pin (black or other): needs to be connected to GND of DC power supply
How Water/Liquid Valve works
Normally, the valve is closed. When 12V DC is applied to the two terminals, the valve opens and water/liquid can flow.
※ NOTE THAT:
- For some kinds of valve, there is a gasket arrangement inside, so there is a minimum pressure requires to open the valve (after 12V DC is applied). The pressure can be created by liquid flow.
- For some kinds of valve, liquid can only flow one direction.
How to Control Water/Liquid Solenoid Valve
If the valve is powered by 12V power supply, it opens. To control the valve, we need to use a relay in between Arduino Nano ESP32 and valve. Arduino Nano ESP32 can control the solenoid valve via the relay. Learn more about relay in the Arduino Nano ESP32 - Relay tutorial
Instead of a relay, you can use a MOSFET module. You can find how this module works in the Arduino Nano ESP32 - MOSFET Module tutorial.
Relay vs MOSFET Module for a Solenoid Valve
| Relay | MOSFET Module | |
|---|---|---|
| Valve control | open / close | open / close (no PWM for a valve) |
| Sound of the switch | clicks | quiet |
| Response | slow, some milliseconds | very fast |
| Wear | the contacts wear out | no moving contacts |
| Valve type | AC or DC valve | DC valve only |
| Valve supply | any, as the relay is rated | about 6V or more |
| Isolation | yes (coil and contacts) | yes (PC817 optocoupler) |
| Good for | AC valves, simple use | DC valves that open and close often |
A solenoid valve must be fully open or fully closed, so do not use PWM with it. Both ways give the same result. Pick the MOSFET module for a 12V DC valve that switches many times a day. Pick the relay for an AC valve.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Arduino Nano ESP32 Code
The below code repeatedly turns the water valve ON in five seconds and OFF in five seconds,
Detailed Instructions
To get started with Arduino Nano ESP32, follow these steps:
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Wire the components according to the provided diagram.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano ESP32 board and its corresponding COM port.* Copy the above code and paste it to Arduino IDE
- Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
- Check the water flow
Code Explanation
The above Arduino Nano ESP32 code contains line-by-line explanation. Please read the comments in the code!
How to Use MOSFET Module to Control Water/Liquid Valve
You can take the relay out and put a MOSFET module in its place. The valve still just opens and closes, but with no click.
On this page, the relay uses D2 and the MOSFET module uses D9. Because the pins are different, each wiring has its own code.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
| Arduino Nano ESP32 / Power | MOSFET Module |
|---|---|
| D9 | PWM pin |
| GND | GND pin |
| 12V adapter + | + terminal |
| Valve + (red) | + terminal (shared with the adapter +) |
| Valve − | LOAD terminal |
| 12V adapter − | − terminal |
The valve has a coil inside, so add a flyback diode (1N4007 or 1N5819) across its two terminals. The stripe side (cathode) goes to valve +.
Code
The below code is the same idea as the relay code: it sets D9 HIGH to open the valve for 5 seconds, then LOW to close it for 5 seconds, again and again.
A valve must only get full ON or full OFF, never PWM. So this page does not use the fade code from the MOSFET module tutorial.
Detailed Instructions
- Wire the components according to the diagram above.
- Connect the Arduino Nano ESP32 to your computer and open the Arduino IDE, as in the relay Quick Steps.
- Copy the above code, then compile and upload it to the Arduino Nano ESP32.
- Plug in the 12V adapter.
- Check the water flow. The valve opens for 5 seconds and closes for 5 seconds, but now without any click.
Tip: the MOSFET module can switch only DC, and the valve supply must be about 6V or more. If your valve runs on AC, use the relay.