Arduino Nano ESP32 - Water/Liquid Valve

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

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Liquid Solenoid Valve
1×12V Power Adapter
1×(Optional) DC Power Jack
1×Jumper Wires
1×(Recommended) Screw Terminal Adapter for Arduino Nano

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. We appreciate your support.

Overview of Water/Liquid Valve


Arduino Nano 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.


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

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and water valve

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code

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

/* * This Arduino Nano ESP32 code was developed by * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * */ #define RELAY_PIN D2 // The Arduino Nano ESP32 pin connected to the water valve via the 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 Arduino Nano ESP32, follow these steps:

  • If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano ESP32 board and its corresponding COM port.* Copy the above code and paste it to Arduino IDE
  • Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
  • Check the water flow

Code Explanation

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

Video Tutorial


  • 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!