ESP32 S3 UNO - Water/Liquid Valve
This ESP32 S3 Uno water valve tutorial shows you how to open and close a 12V solenoid valve with Arduino code. You will connect the liquid solenoid valve to the ESP32 S3 Uno form board through a relay. Then you can control the water flow of water, beer, oil or other liquids from your sketch.
In this tutorial, you will:
- Learn how a liquid solenoid valve opens and closes
- Wire a water solenoid valve and a relay to the ESP32 S3 Uno
- Power the valve from a separate 12V adapter
- Write Arduino code that opens and closes the valve on a timer
- Replace the relay with a MOSFET module and run its own ON/OFF code
Both a relay and a MOSFET module can drive the valve. For a valve, they do the same job: open and close, nothing more. The MOSFET module wins in other ways. It switches with no click, it reacts faster, and it has no contacts that wear out.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Water/Liquid Valve
A solenoid valve is an electric tap. It lets you start and stop a liquid flow without touching anything. Knowing its two wires and how it reacts to power makes the wiring much easier.
Pinout

The valve has only two wires. The positive (+) wire is usually red, and it goes to the 12V side of the DC power supply. The negative (-) wire is black or another color, and it goes to the GND of the same DC power supply.
How Water/Liquid Valve works
By default, the valve stays closed. When you apply 12V DC across its two wires, the coil pulls the valve open and the liquid can pass. When you remove the 12V, the valve closes again.
※ NOTE THAT:
Some valves have a rubber gasket inside. They need a small minimum pressure, besides the 12V, before they open. The flowing liquid usually gives this pressure. Also, many valves let liquid pass in one direction only, so check the arrow on the valve body.
How to Control Water/Liquid Solenoid Valve
The valve needs 12V, but an ESP32 S3 Uno pin gives only 3.3V and a few milliamps. So you put a relay between the board and the valve. The ESP32 S3 Uno switches the relay, and the relay switches the 12V to the valve.
If relays are new to you, read the ESP32 S3 Uno - Relay tutorial first. It explains the relay pins, its trigger modes and the basic code.
A MOSFET module is the other option. Its PWM, GND, +, LOAD and − pins are covered in the ESP32 S3 Uno - MOSFET Module tutorial.
Relay vs MOSFET Module for Water/Liquid Valve
| Relay | MOSFET Module | |
|---|---|---|
| Valve control | open / close (ON/OFF) | open / close (ON/OFF) only, no PWM for a valve |
| Sound | click on every switch | no sound from the switch |
| Reaction time | slow, a few milliseconds | very fast |
| Wear | contacts wear with use | nothing moves inside |
| Valve type | AC or DC valves | DC valves only |
| Valve supply | any within the relay rating | about 6V or more for the gate |
| Isolation | yes (coil and contacts) | yes (PC817 optocoupler) |
| Choose it for | AC valves, simple projects | DC valves that switch often or must stay quiet |
A solenoid valve must get full power or none, so the MOSFET module gives you no extra control here. Pick it for a 12V DC valve when you want quiet, long-lasting switching, for example in a watering system that runs many times a day. For an AC valve, use the relay.
ESP32 S3 Uno Pinout
The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the D3 header and its GPIO number when you wire the relay.

Wiring Diagram
The relay input side connects to the ESP32 S3 Uno. The valve connects to the relay output together with its own 12V power adapter.

This image is created using Fritzing. Click to enlarge image
| Component Pin | ESP32 S3 Uno Pin |
|---|---|
| Relay VCC | 5V |
| Relay GND | GND |
| Relay IN | D3 (GPIO17) |
※ NOTE THAT:
The valve wires never touch the ESP32 S3 Uno. The 12V adapter, the relay output (COM and NO) and the valve form their own loop. Most relay modules accept a 3.3V signal on the IN pin; if yours does not switch, use a relay module made for 3.3V logic.
ESP32 S3 Uno Code
This sketch opens the water valve for five seconds, then closes it for five seconds, and repeats forever. It drives the relay on GPIO17 (D3) with digitalWrite().
Detailed Instructions
Follow these steps one by one:
- New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
- Wire it up as shown in the diagram.
- Connect the board to your computer with a USB Type-C cable.
- Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
- Copy the code above and paste it into the Arduino IDE.
- Upload the code by clicking the Upload button.
- Plug in the 12V adapter for the valve.
- Watch the water flow: it should run for five seconds, then stop for five seconds.
- Tip: Test with a bucket or a sink first, so a leak does not reach your board.
Code Explanation
The sketch is short, and the comments inside the code explain each line. In setup(), the relay pin becomes an output. In loop(), the pin goes HIGH to open the valve and LOW to close it, with a 5-second delay after each step.
How to Control Water/Liquid Valve with MOSFET Module
You can drop the relay and let a MOSFET module open and close the valve. The valve works the same way, but the switching is silent and has no parts that wear out.
On this page, the relay and the MOSFET module use different pins. The relay uses D3 (GPIO17), and the MOSFET module uses D9 (GPIO46). So each wiring has its own code.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
| Component Pin | Connection |
|---|---|
| MOSFET module PWM | ESP32 S3 Uno D9 (GPIO46) |
| MOSFET module GND | ESP32 S3 Uno GND |
| MOSFET module + | 12V adapter + (through DC power jack) and valve + wire |
| MOSFET module LOAD | valve − wire |
| MOSFET module − | 12V adapter − (through DC power jack) |
The valve is a coil. Put a 1N4007 or 1N5819 flyback diode across its two wires, with the stripe (cathode) on the valve + wire. The module has no diode of its own, so this one protects it.
※ NOTE THAT:
D9 (GPIO46) is a boot strapping pin. If the upload fails, unplug the PWM wire from D9, upload the code, then plug the wire back.
ESP32 S3 Uno Code
The code is the same idea as the relay code: HIGH turns the valve on (open), and LOW turns it off (closed). It opens the valve for five seconds, closes it for five seconds, and repeats forever.
A solenoid valve must only get full ON or full OFF, never PWM. That is why this page does not use the fade code from the MOSFET module tutorial.
Detailed Instructions
- Wire it up as shown in the MOSFET diagram above.
- Connect the board to your computer with a USB Type-C cable.
- Open Arduino IDE and choose the board and COM port, as in the relay Quick Steps above.
- Copy and upload the code above.
- Plug in the 12V adapter and watch the valve: it opens for five seconds, then closes for five seconds.
Tip: the MOSFET module needs a valve supply of at least about 6V and works with DC only. A 12V DC valve is fine. If your valve runs on AC, keep the relay.
Video Tutorial
Watch the video below to see this ESP32 S3 Uno project step by step.
FAQ
Can the ESP32 S3 Uno power the solenoid valve directly?
No. The valve needs 12V and a lot more current than a GPIO pin can give. Always use a relay (or a MOSFET module) and a separate 12V adapter. Connecting the valve to a pin can damage the ESP32 S3 Uno.
Will a 5V relay module work with the 3.3V ESP32 S3 Uno pins?
Most 5V relay modules have a transistor or optocoupler input, and they switch fine with a 3.3V HIGH signal. Power the module from the 5V pin and connect IN to D3 (GPIO17). If the relay clicks weakly or not at all, pick a relay module rated for 3.3V logic.
Why does my valve not open even when the relay clicks?
Some valves need a minimum water pressure to open, because of the gasket inside. Without pressure, the valve may stay shut. Also check that the flow follows the arrow on the valve body and that the 12V adapter gives enough current.
Do I need a flyback diode for the valve?
The valve is a coil, so it makes a voltage spike when it turns off. Many relay modules handle this fine, but a diode across the valve wires (cathode to +12V) protects the relay contacts and reduces noise. It is a good idea for long-running projects.
What if my project needs more valves or sensors than the Uno headers give?
The ESP32 S3 Uno has two extra rows of holes with more GPIOs: GPIO15 and GPIO16 (I/O, PWM, analog), GPIO45 and GPIO35 to GPIO42 (I/O, PWM), and GPIO47/GPIO48 (output only, PWM). Solder pin headers to use them and write the GPIO number in code. For extra relays, GPIO35 to GPIO42 are a good choice; for a water level or soil sensor, use GPIO15 or GPIO16. Be careful with two things. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.
Can I use the same code as on the UNO R4?
Yes, the logic is the same. Only the pin number changes: D3 on the ESP32 S3 Uno is GPIO17, so the code uses 17 instead of 3.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| No COM port in Arduino IDE | USB driver missing or board not in upload mode | Install the CP210x or CH340 driver, try another USB cable, or hold BOOT while pressing RESET |
| Relay does not click | Wrong pin, or relay VCC/GND not connected | Check that IN goes to D3 (GPIO17) and that VCC is on 5V and GND on GND |
| Relay clicks but valve stays closed | No 12V power or valve wired to the wrong relay pins | Check the 12V adapter and DC jack, and wire the valve through COM and NO |
| Valve opens and closes in reverse | Low level trigger relay or NC pin used | Use the NO pin, or swap HIGH and LOW in the code |
| Valve hums but water does not flow | Too little water pressure or flow against the arrow | Add pressure (raise the tank) and turn the valve to follow the arrow |
| Board resets when the valve switches | Electrical noise from the valve coil | Add a flyback diode across the valve and keep the 12V wires away from the board |
| Water leaks at the joints | Loose fittings | Use proper fittings and thread tape, and test away from electronics |