ESP32 MicroPython Water/Liquid Valve

This tutorial instructs you how to control liquid flows such as water, beer, and oil using a ESP32 with MicroPython and a solenoid valve. We will cover:

The valve can be switched by a relay or by a MOSFET module. Both of them can only open the valve (ON) or close it (OFF), because a solenoid valve has no "half open" setting. So why use a MOSFET module? It switches without a click, it is much faster, and it has no metal contacts that wear out. You will learn both ways here.

ESP32 MicroPython Water/Liquid Valve

Overview of Water/Liquid Valve

Pinout

ESP32 MicroPython Water/Liquid Valve Pinout

A solenoid valve typically has two connections:

  • Connect the positive (+) pin (red) to the 12V DC power supply.
  • Connect the negative (-) pin (black or another color) to the GND of the DC power supply.

How Water/Liquid Valve works

Normally, the valve remains closed. When you apply 12V DC to the two terminals, the valve opens, allowing water or other fluids to flow through.

※ NOTE THAT:

Some valves have a gasket and need a certain amount of pressure to open when 12V DC is applied. This pressure is provided by the liquid flow. Additionally, some valves only allow liquid to flow in one direction.

How to Control Water/Liquid Solenoid Valve

When the valve receives 12V power, it opens. To control the valve with an ESP32, you need to use a relay. The ESP32 sends signals to the relay, which then controls the valve. If you're unfamiliar with how a relay works, its pin configuration, or how to set it up, check out the ESP32 MicroPython Relay tutorial.

A MOSFET module can do the same job. To understand its pins and terminals, see the ESP32 MicroPython - MOSFET Module tutorial.

Relay vs MOSFET Module for Water/Liquid Valve

Relay MOSFET Module
Valve control open or closed open or closed (no PWM for a valve)
Click when switching yes no, it is silent
Reaction time slow (milliseconds) very fast
Wear contacts wear out over time no moving parts inside the module
Valve type AC or DC valves DC valves only
Valve supply any voltage in the relay rating at least about 6V
Isolation yes (coil and contacts) yes (PC817 optocoupler)
Pick it for AC valves, simple use 12V DC valves that open and close often, quiet projects

Do not use PWM on a solenoid valve. It cannot stay half open, and PWM can make it buzz and get hot. For a 12V DC valve, the MOSFET module is a good silent switch. For an AC valve, use the relay.

Wiring Diagram

  • How to connect ESP32 and water/liquid valve using breadboard (powered via USB cable)
The wiring diagram between ESP32 MicroPython water valve

This image is created using Fritzing. Click to enlarge image

  • How to connect ESP32 and water/liquid valve using breadboard (powered via Vin pin)
The wiring diagram between ESP32 MicroPython water valve

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and water/liquid valve
How to wire ESP32 and water/liquid valve

ESP32 MicroPython Code

This code will switch the water valve ON for five seconds, then switch it OFF for another five seconds.

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-water-liquid-valve """ from machine import Pin import time RELAY_PIN = 16 # The ESP32 pin GPIO16 connected to the water valve via relay pin # Initialize the relay pin as an output relay = Pin(RELAY_PIN, Pin.OUT) # Infinite loop while True: relay.value(1) # Turn on water valve for 5 seconds time.sleep(5) relay.value(0) # Turn off water valvefor 5 seconds time.sleep(5)

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ESP32 board to the water valve according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Check the water flow.

Code Explanation

You can find the explanation in the comments of the ESP32 MicroPython code provided.

How to Control Water/Liquid Valve with MOSFET Module

If you want a valve that opens and closes with no click, put a MOSFET module where the relay was. The valve still only opens or closes, exactly like before.

Note that the relay and the MOSFET module use different pins on this page. The relay uses GPIO16, while the MOSFET module uses GPIO18. So each wiring has its own code.

Wiring Diagram

The wiring diagram between ESP32 MicroPython water valve MOSFET module

This image is created using Fritzing. Click to enlarge image

Connect the parts like this:

  • ESP32 GPIO18 → module PWM pin
  • ESP32 GND → module GND pin
  • 12V adapter + (via the DC power jack) → + terminal
  • Valve red wire (+) → + terminal too
  • Valve black wire (−) → LOAD terminal
  • 12V adapter − (via the DC power jack) → − terminal

The valve has a coil inside, so a flyback diode is needed. Place a 1N4007 or 1N5819 diode across the valve wires, with the stripe (cathode) on the valve + wire and the other end on the valve − wire.

ESP32 MicroPython Code

This code follows the same idea as the relay code: the pin goes HIGH to open the valve for five seconds, then LOW to close it for five seconds, and this repeats. It prints nothing to the Shell.

A valve must only get full ON or full OFF, never PWM. This is why we do not use the fade code from the MOSFET module tutorial here.

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-water-liquid-valve """ from machine import Pin import time MOSFET_PIN = 18 # The ESP32 pin GPIO18 connected to the PWM pin of the MOSFET module valve = Pin(MOSFET_PIN, Pin.OUT) # set the pin as an output while True: valve.value(1) # open the valve for 5 seconds time.sleep(5) valve.value(0) # close the valve for 5 seconds time.sleep(5)

Detailed Instructions

  • Wire the valve and the MOSFET module as shown in the diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE and set the interpreter and port, as in the relay Quick Steps above.
  • Paste this code into Thonny, save it as main.py on the ESP32, and click Run.
  • The valve opens for five seconds and closes for five seconds, but now with no relay click.

Tip: the MOSFET module switches DC only and needs a valve supply of at least about 6V. If your valve runs on AC, keep using 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!