ESP32 UNO - Water/Liquid Valve

Need to start and stop a water flow from code? An ESP32 Uno water valve setup does it with one relay and a 12V solenoid valve. Wire the liquid valve to the ESP32 Uno form board, upload a short sketch, and water, beer or oil flows on your command.

By the end, you can:

  1. Connect a 12V solenoid valve through a relay to the ESP32 Uno
  2. Open and close the valve with digitalWrite() in Arduino IDE
  3. Swap the relay for a silent MOSFET module
  4. Protect the board from the valve coil with a flyback diode
ESP32 Uno water valve

Components You Need

1×ESP32 Uno-form Board
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×DC Power Jack
1×Jumper Wires

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 the Liquid Solenoid Valve

After this part, you know the valve wires and what happens when you power them.

Valve Pinout

ESP32 Uno Water/Liquid Valve Pinout

The valve has two wires only.

Valve Wire Goes To
Positive (+), usually red 12V of the DC power supply
Negative (-), black or another color GND of the same DC power supply

How the Valve Works

At rest, the valve is shut. Apply 12V DC to the two wires and the coil pulls it open, so liquid can pass. Remove the 12V and it closes again.

※ NOTE THAT:

Some valves hold a rubber gasket and need a small minimum pressure before they open. The moving liquid often gives that pressure. Many valves also pass liquid in one direction only, so look for the arrow on the valve body.

Why You Need a Relay

The valve wants 12V and a lot of current. An ESP32 Uno pin gives 3.3V and only a few milliamps. So the board flips a relay, and the relay connects the 12V supply to the valve.

New to relays? Read the ESP32 Uno - Relay tutorial for the pins, the trigger modes and the basic code.

A MOSFET module is a second way to switch the valve. Its pins are explained in the ESP32 Uno - MOSFET Module tutorial.

Relay or MOSFET Module?

Feature Relay MOSFET Module
What it does to the valve opens or closes opens or closes (never use PWM on a valve)
Noise clicks each time silent
Speed a few milliseconds faster
Wear contacts age after many cycles nothing moves inside
Valve type AC or DC DC only
Supply needed any value within the relay rating about 6V or more
Isolation coil apart from contacts PC817 optocoupler
Best for AC valves 12V DC valves with frequent cycles

Good to know: a valve needs full power or none, so the MOSFET module adds no extra control here. Choose it for quiet and long life. Choose the relay for an AC valve.

ESP32 Uno Pinout

Every Uno label on the ESP32 Uno form board points to one GPIO number, and your sketch uses that number. Find D3 and D9 on the diagram before you place any wire.

ESP32 Uno pinout diagram

How to Connect

When you finish this part, the relay input is wired to the ESP32 Uno and the valve has its own 12V loop.

The wiring diagram between ESP32 Uno water valve

This image is created using Fritzing. Click to enlarge image

Relay Pin ESP32 Uno Pin
VCC 5V
GND GND
IN D3 (GPIO25)

The valve never touches the board. It sits in its own loop with the 12V adapter and the relay contacts (COM and NO).

Good to know: ESP32 Uno pins use 3.3V logic and are NOT 5V tolerant. The signal here only leaves the board, so it is safe. Most relay modules with a transistor or optocoupler input accept 3.3V as HIGH. If yours stays silent, pick a module made for 3.3V logic.

ESP32 Uno Code

This sketch opens the valve for 5 seconds, closes it for 5 seconds, and repeats without end.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-water-liquid-valve */ #define RELAY_PIN 25 // D3 (GPIO25) on the ESP32 Uno drives the relay IN pin // setup() runs one time after power-on or a reset void setup() { // D3 (GPIO25) sends the control signal, so make it an output pinMode(RELAY_PIN, OUTPUT); } // loop() repeats again and again void loop() { digitalWrite(RELAY_PIN, HIGH); // relay on: the valve is open for 5 seconds delay(5000); digitalWrite(RELAY_PIN, LOW); // relay off: the valve is closed for 5 seconds delay(5000); }

Upload and Test

  1. First time with this board? Set it up with ESP32 Uno - Getting Started before you continue.
  2. Build the circuit by following the wiring table above.
  3. Plug in the USB Type-C cable between the ESP32 Uno and your computer.
  4. Select the board in Arduino IDE: pick ESP32 Dev Module and the COM port of the board (CH340).
  5. Copy the code above into the Arduino IDE.
  6. Press Upload to send the sketch to the board.
  7. Power the valve by connecting the 12V adapter.
  8. Watch the flow: water runs for 5 seconds, then stops for 5 seconds.
  9. Pro tip: do the first test over a sink or a bucket, with the board far from any splash.

Video Tutorial

See the ESP32 Uno build in action in the video below.

How the Code Works

Code Part What It Does
#define RELAY_PIN 25 names the pin D3 (GPIO25) that feeds the relay IN
pinMode(RELAY_PIN, OUTPUT) makes the pin an output in setup()
digitalWrite(RELAY_PIN, HIGH) closes the relay, so the valve opens
delay(5000) waits 5 seconds before the next step
digitalWrite(RELAY_PIN, LOW) opens the relay, so the valve closes

The result is a valve that cycles open and closed every 5 seconds. The comments inside the code give more detail.

ESP32 Uno vs UNO R4 for This Project

Moving this valve project from the UNO R4 to the ESP32 Uno changes only the pin numbers and the logic voltage.

Item Arduino UNO R4 ESP32 Uno
Logic voltage 5V 3.3V
Relay IN pin D3 (pin 3) D3 (GPIO25)
MOSFET PWM pin D9 (pin 9) D9 (GPIO13)
Arduino IDE board Arduino UNO R4 WiFi ESP32 Dev Module
Extra library none none
Wireless WiFi on the WiFi model only WiFi and Bluetooth built in

The same wiring works as it is. Only the numbers in the sketch change, from 3 and 9 to 25 and 13. Check that your relay module accepts a 3.3V signal.

Quiet Option: MOSFET Module Instead of the Relay

When you finish this part, the same valve opens and closes with no click.

Each wiring has its own sketch: the relay sits on D3 (GPIO25) and the MOSFET module sits on D9 (GPIO13).

MOSFET Wiring

The wiring diagram between ESP32 Uno water valve MOSFET module

This image is created using Fritzing. Click to enlarge image

From To
ESP32 Uno D9 (GPIO13) MOSFET module PWM
ESP32 Uno GND MOSFET module GND pin
12V adapter + MOSFET module +
Valve + (red) MOSFET module + (joined with the adapter +)
Valve - MOSFET module LOAD
12V adapter - MOSFET module -

The valve is a coil, so a flyback diode is a must. Place a 1N4007 or 1N5819 diode over the two valve wires. The striped end (cathode) faces the + wire.

MOSFET Code

HIGH opens the valve and LOW closes it, just like the relay sketch. It opens for 5 seconds, then closes for 5 seconds.

Never send PWM to a solenoid valve. So the fade sketch from the MOSFET module page is not used here.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-water-liquid-valve */ #define MOSFET_PIN 13 // D9 (GPIO13) on the ESP32 Uno goes to the PWM pin of the MOSFET module // setup() runs once when the board starts void setup() { pinMode(MOSFET_PIN, OUTPUT); // D9 (GPIO13) is a plain ON/OFF output here } // loop() runs over and over void loop() { digitalWrite(MOSFET_PIN, HIGH); // MOSFET on: valve open for 5 seconds delay(5000); digitalWrite(MOSFET_PIN, LOW); // MOSFET off: valve shut for 5 seconds delay(5000); }

MOSFET Steps

  1. Build the MOSFET circuit from the diagram above.
  2. Connect the ESP32 Uno to your computer with the USB Type-C cable.
  3. Pick ESP32 Dev Module and the COM port, as in the relay steps.
  4. Copy the MOSFET code, then click Upload.
  5. Power the 12V adapter and listen: the valve now works without a click.

※ NOTE THAT:

This module suits DC valves only, and it needs a supply of about 6V or more. With an AC valve, stay with the relay.

Go Wireless: Open the Valve from Your Phone

After this part, you know how to switch the valve without touching the board.

The ESP32 Uno has WiFi and Bluetooth built in. Put the valve on a web page or a phone app, and you can start a watering cycle from the sofa. A button on the screen sets the relay pin HIGH or LOW, so you do not need to change the wiring.

Troubleshooting

Problem Possible Cause Solution
No COM port appears CH340 driver missing or a charge-only cable Install the CH340 driver and use a USB cable that carries data
Failed to connect or Waiting for download the board is not in upload mode Hold the BOOT button while the upload starts
Relay does not click wrong pin or no power on the relay Check IN on D3 (GPIO25), VCC on 5V and GND on GND
Relay clicks but the valve stays shut no 12V or valve on the wrong terminals Check the adapter and the DC jack, and wire the valve to COM and NO
Valve works in reverse NC terminal used or a LOW level trigger relay Move the wire to NO, or swap HIGH and LOW in the code
Valve hums but no water flows low pressure or flow against the arrow Raise the tank or add pressure, and follow the arrow on the valve
ESP32 Uno restarts at each switch spikes from the valve coil Fit a diode across the valve and route the 12V cables away from the board
MOSFET valve never opens supply below 6V or the LOAD wire on the wrong side Use a 12V supply and check that the valve - goes to LOAD
Water drips from a pipe joint fitting not tight Seal the thread with tape and keep the board and relay away from the water

FAQ

Can the ESP32 Uno drive the solenoid valve directly?

No. The valve needs 12V and far more current than a pin can give. Always use a relay or a MOSFET module with a separate 12V adapter. A valve wired straight to a pin can damage the ESP32 Uno.

Does a 5V relay module work with a 3.3V pin?

Most do, because their input uses a transistor or an optocoupler. Power the module from 5V and connect IN to D3 (GPIO25). If the click is weak or missing, use a relay module made for 3.3V logic.

Why does the relay click but no water comes out?

A valve with a gasket may not lift without some line pressure. Confirm the water moves along the printed arrow, and that your 12V adapter can supply the coil current.

Is a flyback diode really needed?

Yes, it is a good habit. When the coil is switched off, it throws a short voltage spike. A 1N4007 or 1N5819 diode across the valve wires, cathode to the + side, protects the switch and cuts noise. The MOSFET module needs it most.

Can I control the valve with PWM to slow the flow?

No. A solenoid valve has two states, open and closed. PWM makes it buzz and heat up. For a variable flow, use a motorized valve or a pump.

Which valve type goes with which switch?

Use the relay for an AC valve or any valve within the relay rating. Pick the MOSFET module for a 12V DC valve that cycles a lot or must stay quiet.

How do I run the valve on a timer in my own sketch?

Replace the two delay(5000) calls with the times you want, in milliseconds. For long times, use millis() so the board can do other work while the valve waits.

Are the ESP32 Uno pins safe for a 5V signal?

No. They are not 5V tolerant, so a 5V signal into a pin can damage the board. In this project the signal only goes out, so you do not need a level shifter.

What if my project needs more pins than the Uno headers give?

The ESP32 Uno has a few extra holes near the power header: GPIO32 and GPIO33 (I/O, PWM, analog on ADC1, so they also work while WiFi is on) and GPIO15 (I/O, PWM, analog on ADC2). Solder pin headers on them, then type the GPIO number in your sketch. For more valves, GPIO32 and GPIO33 are good first choices. Two warnings. First, GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC position) are boot strapping pins, so a wrong connection can stop the board from booting or uploading code. Use GPIO32 and GPIO33 first. Second, never use the six holes under the digital header (SD0 to SD3, CMD, CLK = GPIO6 to GPIO11). They connect to the flash chip, and using them crashes the board.

Can I use the UNO R4 sketch on the ESP32 Uno?

Yes, the program flow stays as it was. Only two numbers differ: 3 turns into 25 and 9 turns into 13.

Can I control the valve over WiFi with the ESP32 Uno?

Yes. The ESP32 Uno has WiFi and Bluetooth, so a web page or a phone app can set the relay pin HIGH or LOW. See the Go Wireless section for ready tutorials.

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