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.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Relay
1×Liquid Solenoid Valve
1×12V Power Adapter
1×DC Power Jack
n×Jumper Wires
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

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

Wiring Diagram

wiring diagram between esp32 water valve

This image is created using Fritzing. Click to enlarge image

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 GIOP16, which connects to the IN pin of relay // the code in setup function runs only one time when ESP32 starts void setup() { // initialize digital pin A5 as an output. pinMode(RELAY_PIN, OUTPUT); } // the code in loop function is executed repeatedly infinitely void loop() { digitalWrite(RELAY_PIN, HIGH); // open valve 5 seconds delay(5000); digitalWrite(RELAY_PIN, LOW); // close valve 5 seconds delay(5000); }

Quick Instructions

  • Connect ESP32 to PC via USB cable
  • Open Arduino IDE, select the right board and 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!

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.

※ NOTICES