ESP8266 - Water/Liquid Valve
This tutorial instructs you how to use an ESP8266 and a solenoid valve to control the flow of liquids such as water, beer, and oil. This same technique can be applied to controlling gas flow.
The valve can be opened and closed through a relay or through a MOSFET module. Both of them only open the valve fully or close it fully, because a solenoid valve must not be driven with PWM. The MOSFET module, however, switches with no click, switches faster, and has no metal contacts that wear out. This tutorial covers both options.
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 (red) should be connected to a 12V DC power supply
- The Negative (-) pin (black or other) should be connected to the GND of a DC power supply
How Water/Liquid Valve works
Typically, the valve is in a shut position. When 12V DC is supplied to the two terminals, the valve opens allowing water/liquid to pass through.
※ NOTE THAT:
- For certain types of valves, an internal gasket arrangement necessitates a minimum pressure to open the valve once 12V DC is applied. This pressure can be generated by the flow of liquid.
- Moreover, for some valves, the liquid is only able to flow in one direction.
How to Control Water/Liquid Solenoid Valve using ESP8266
If the valve is supplied with 12V, it will open. To control the valve, a relay must be used between ESP8266 and the valve. ESP8266 can then control the solenoid valve through the relay.
If you are unfamiliar with relays (pinout, how they work, how to program them, etc.), you can learn about them in the ESP8266 - Relay tutorial.
You can also use a MOSFET module in place of the relay. To learn its pins and how it switches a 12V load, see the ESP8266 - MOSFET Module tutorial.
Relay vs MOSFET Module for Water/Liquid Valve
| Item | Relay | MOSFET Module |
|---|---|---|
| Valve control | open/close | open/close (no PWM for a valve) |
| Sound | a click every time | no click |
| Switching speed | slow (milliseconds) | very fast |
| Wear | relay 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 from contacts | yes, by the PC817 optocoupler |
| Good for | AC valves, simple projects | DC valves, quiet or frequent switching |
For a 12V DC valve, both parts do the same job of opening and closing. Pick the MOSFET module if you want silence or the valve switches many times a day. Pick the relay if the valve runs on AC.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
ESP8266 Code for Controlling Liquid Valve
The below code turns the water valve ON for five seconds and then OFF for five seconds
Detailed Instructions
To get started with ESP8266 on Arduino IDE, follow these steps:
- Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
- Wire the components as shown in the diagram.
- Connect the ESP8266 board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
- Sensor output on Serial Monitor
- Connect your ESP8266 to your computer using a USB cable.
- Launch the Arduino IDE, select the correct board and port.
- Copy the code above and open it in the Arduino IDE.
- Click the Upload button in the Arduino IDE to send the code to the ESP8266.
- Check the output of the water flow sensor on the Serial Monitor.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!
How to Control Water/Liquid Valve with MOSFET Module
Here we take out the relay and put a MOSFET module in its place. The valve still only opens or closes, but now it does so without a click.
The relay and the MOSFET module use different pins on this page. The relay is on D8, and the MOSFET module is on D5 (GPIO14). Because of this, each wiring has its own code.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.
| NodeMCU / 12V side | MOSFET Module |
|---|---|
| D5 (GPIO14) | PWM |
| G (GND) | GND |
| 12V adapter + | + |
| Valve + wire | + (shared with the adapter +) |
| Valve − wire | LOAD |
| 12V adapter − | − |
The valve has a coil inside, so it needs a flyback diode (1N4007 or 1N5819) across its two wires. The stripe (cathode) must face the valve + wire.
ESP8266 Code
The code works the same way as the relay code: HIGH turns the valve ON (open), and LOW turns it OFF (closed). The valve opens for 5 seconds and closes for 5 seconds, again and again. Nothing is printed to the Serial Monitor.
A valve must get only full ON or full OFF, never PWM. That is why this page does not use the PWM fade code from the MOSFET module tutorial.
Detailed Instructions
- Wire the components as shown in the diagram above.
- Connect the ESP8266 board to your computer using a USB cable.
- Open Arduino IDE and choose the board and COM port, as in the relay Quick Steps above.
- Copy the code above and click the Upload button.
- Power the valve. It opens for 5 seconds, then closes for 5 seconds, over and over.
※ NOTE THAT:
The MOSFET module can switch only DC, and it needs a valve supply of at least about 6V. For an AC valve, use the relay.