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.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Relay
1×Liquid Solenoid Valve
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Breadboard
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) Screw Terminal Expansion Board for ESP8266

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
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.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and water valve

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.

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!

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!