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

1×ESP8266 NodeMCU ESP-12F
1×Alternatively, ESP8266 D1 Mini NodeMCU ESP-12F
1×Alternatively, ESP8266 NodeMCU ESP-12E (Uno-form)
1×Micro USB Cable
1×Alternatively, ESP8266 NodeMCU ESP-12F (SMD-form)
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Relay
1×Alternatively, MOSFET Module (AOD4184, LR7843 or FR120N)
1×Liquid Solenoid Valve
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for ESP8266

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Water/Liquid Valve

The Water/Liquid Valve Pinout

ESP8266 NodeMCU 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

The wiring diagram between ESP8266 NodeMCU and water valve

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

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-water-liquid-valve */ #define RELAY_PIN D8 // The ESP8266 pin connected to the IN pin of relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin A5 as an output. pinMode(RELAY_PIN, OUTPUT); } // The loop function repeats indefinitely void loop() { digitalWrite(RELAY_PIN, HIGH); // open valve 5 seconds delay(5000); digitalWrite(RELAY_PIN, LOW); // close valve 5 seconds delay(5000); }

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

The wiring diagram between ESP8266 NodeMCU and NodeMCU water liquid valve MOSFET module

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.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-water-liquid-valve */ #define MOSFET_PIN D5 // The ESP8266 pin D5 connected to the PWM pin of the MOSFET module // The setup function runs once on reset or power-up void setup() { pinMode(MOSFET_PIN, OUTPUT); // set the pin as an output } // The loop function repeats indefinitely void loop() { digitalWrite(MOSFET_PIN, HIGH); // open the valve for 5 seconds delay(5000); digitalWrite(MOSFET_PIN, LOW); // close the valve for 5 seconds delay(5000); }

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.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!