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:
- How to connect the solenoid valve with ESP32
- How to write MicroPython code for ESP32 to control the water valve.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
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
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.
Wiring Diagram
- How to connect ESP32 and water/liquid valve using breadboard (powered via USB cable)
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and water/liquid valve using breadboard (powered via Vin pin)
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and water/liquid valve using screw terminal block breakout board (powered via USB cable)
- How to connect ESP32 and water/liquid valve using screw terminal block breakout board (powered via Vin pin)
ESP32 MicroPython Code
This code will switch the water valve ON for five seconds, then switch it OFF for another five seconds.
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.