ESP32 - Water/Liquid Valve

This tutorial instructs you how to control a liquid flow such as water, beer, oil by using ESP32 and a solenoid valve. It is the same for controlling gas flow.

To switch the valve, you can use a relay or a MOSFET module. Both of them only open and close the valve, because a solenoid valve is either open or closed. The MOSFET module has other good points: it makes no click, it switches faster, and it has no contacts that wear out. This tutorial covers both options.

This tutorial shows how to program the ESP32 using the Arduino language (C/C++) via the Arduino IDE. If you’d like to learn how to program the ESP32 with MicroPython, visit this ESP32 MicroPython - Water/Liquid Valve tutorial.

Introduction to Water/Liquid Valve

Pinout

ESP32 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 ESP32 and valve. ESP32 can control the solenoid valve via the relay. Learn more about relay in the ESP32 - Relay tutorial

The valve can also be driven by a MOSFET module. You can find its pinout and working principle in the ESP32 - MOSFET Module tutorial.

Relay vs MOSFET Module for Solenoid Valve

Relay MOSFET Module
Valve control open/close open/close (no PWM for a valve)
Click sound yes no
Reaction time slower, a few milliseconds very fast
Wear contacts wear out after many cycles no moving contacts
Valve type AC or DC valve DC valve only
Valve voltage any (as rated) about 6V or more, 12V is fine
Isolation yes (coil and contacts) yes (PC817 optocoupler)
Suits AC valves, rare switching 12V DC valves that open and close often, quiet places

For a 12V DC valve that switches many times a day, the MOSFET module lasts longer and stays quiet. For an AC valve, use the relay. Do not send a PWM signal to the valve; give it only full ON or full OFF.

Wiring Diagram

  • How to connect ESP32 and water/liquid valve using breadboard (powered via USB cable)
ESP32 water valve wiring diagram

This image is created using Fritzing. Click to enlarge image

  • How to connect ESP32 and water/liquid valve using breadboard (powered via Vin pin)
ESP32 water valve wiring diagram

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and water/liquid valve
How to wire ESP32 and water/liquid valve

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: The best way to Power ESP32 and sensors/displays.

ESP32 Code

The below code repeatedly turns the water valve ON in five seconds and OFF in five seconds,

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-water-liquid-valve */ #define RELAY_PIN 16 // ESP32 pin GPIO16, which connects to the water valve via the relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin A5 as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); // open valve 5 seconds delay(5000); digitalWrite(RELAY_PIN, LOW); // close valve 5 seconds delay(5000); }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Copy the above code and paste it to Arduino IDE
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • Check the water flow

Code Explanation

The above ESP32 code contains line-by-line explanation. Please read the comments in the code!

How to Control Water Valve with MOSFET Module

This part shows how to open and close the same valve with a MOSFET module instead of the relay. The relay and the MOSFET module use different pins on this page: the relay uses GPIO16, and the MOSFET module uses GPIO18. So each wiring has its own code.

Wiring Diagram with MOSFET Module

ESP32 solenoid valve MOSFET module wiring diagram

This image is created using Fritzing. Click to enlarge image

From To
ESP32 GPIO18 Module PWM pin
ESP32 GND Module GND pin
12V adapter + Module + terminal
Valve + (red) Module + terminal (shared with the adapter +)
Valve − Module LOAD terminal
12V adapter − Module − terminal

The valve has a coil, so add a flyback diode (1N4007 or 1N5819) across the valve terminals, with the stripe (cathode) on the valve + side.

ESP32 Code for MOSFET Module

The code is the same idea 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.

A solenoid valve must only get full ON or full OFF, never PWM. This is why this page does not use the fade code from the MOSFET module tutorial.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-water-liquid-valve */ #define MOSFET_PIN 18 // The ESP32 pin GPIO18 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); }

Quick Instructions

  • Do the wiring as in the MOSFET diagram above.
  • Connect the ESP32 board to your PC via a USB cable.
  • Open Arduino IDE and select the ESP32 board and COM port, as in the relay Quick Steps above.
  • Copy the above code, paste it to Arduino IDE and click Upload.
  • Check the water flow: the valve opens for 5 seconds and closes for 5 seconds.

Tip: the MOSFET module is for DC valves with a supply of about 6V or more. For an AC valve, keep the relay.

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES