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.

The valve can be switched by a relay or by a MOSFET module. Both of them simply open and close the valve. The difference is in how they switch: the MOSFET module has no click sound, it is faster, and it has no metal contacts that wear out over time. We will show the relay first, and then how to use a MOSFET module.

Hardware Preparation

1×Arduino Nano ESP32
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×Optionally, DC Power Jack
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano ESP32

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

Pinout

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.

※ 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 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

Instead of a relay, you can use a MOSFET module. You can find how this module works in the Arduino Nano ESP32 - MOSFET Module tutorial.

Relay vs MOSFET Module for a Solenoid Valve

Relay MOSFET Module
Valve control open / close open / close (no PWM for a valve)
Sound of the switch clicks quiet
Response slow, some milliseconds very fast
Wear the 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 and contacts) yes (PC817 optocoupler)
Good for AC valves, simple use DC valves that open and close often

A solenoid valve must be fully open or fully closed, so do not use PWM with it. Both ways give the same result. Pick the MOSFET module for a 12V DC valve that switches many times a day. Pick the relay for an AC valve.

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 newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-water-liquid-valve */ #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!

How to Use MOSFET Module to Control Water/Liquid Valve

You can take the relay out and put a MOSFET module in its place. The valve still just opens and closes, but with no click.

On this page, the relay uses D2 and the MOSFET module uses D9. Because the pins are different, each wiring has its own code.

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and water valve MOSFET module

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 / Power MOSFET Module
D9 PWM pin
GND GND pin
12V adapter + + terminal
Valve + (red) + terminal (shared with the adapter +)
Valve − LOAD terminal
12V adapter − − terminal

The valve has a coil inside, so add a flyback diode (1N4007 or 1N5819) across its two terminals. The stripe side (cathode) goes to valve +.

Code

The below code is the same idea as the relay code: it sets D9 HIGH to open the valve for 5 seconds, then LOW to close it for 5 seconds, again and again.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-water-liquid-valve */ #define MOSFET_PIN D9 // The Arduino Nano ESP32 pin D9 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 only get full ON or full OFF, never PWM. So this page does not use the fade code from the MOSFET module tutorial.

Detailed Instructions

  • Wire the components according to the diagram above.
  • Connect the Arduino Nano ESP32 to your computer and open the Arduino IDE, as in the relay Quick Steps.
  • Copy the above code, then compile and upload it to the Arduino Nano ESP32.
  • Plug in the 12V adapter.
  • Check the water flow. The valve opens for 5 seconds and closes for 5 seconds, but now without any click.

Tip: the MOSFET module can switch only DC, and the valve supply must be about 6V or more. If your valve runs on AC, 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!