Raspberry Pi Pico - Water/Liquid Valve
This tutorial instructs you how to control liquid flows such as water, beer, and oil using a Raspberry Pi Pico and a solenoid valve. We will cover:
- How to connect the solenoid valve with Raspberry Pi Pico
- How to code Raspberry Pi Pico to control the 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 usually has two connections.
- Attach the positive (+) pin (red) to the 12V DC power supply.
- Attach the negative (-) pin (black or other) to the GND of the DC power supply.
How Water/Liquid Valve works
Normally, the valve is closed. When you connect 12V DC to the two points, the valve opens and lets water or other fluids flow through.
※ NOTE THAT:
Some valves contain a gasket and require a certain pressure to open when 12V DC is applied. This pressure comes from the liquid flow. Also, some valves let liquid pass through only one way.
How to Control Water/Liquid Solenoid Valve
When the valve gets 12V power, it opens. You need a relay to control the valve using the Raspberry Pi Pico. The Raspberry Pi Pico uses the relay to control the valve. If you don't know how a relay works, its pin setup, or how to set it up, check out the Raspberry Pi Pico - Relay tutorial.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Pico Code
This code will switch the water valve ON for five seconds, then switch it OFF for another five seconds.
Detailed Instructions
Please follow these instructions step by step:
- Ensure that Thonny IDE is installed on your computer.
- Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
- If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
- Wire the Raspberry Pi Pico to the water valve according to the provided diagram.
- Connect the Raspberry Pi Pico to your computer using a USB cable.
- Launch the Thonny IDE on your computer.
- On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
- In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
- Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your Raspberry Pi Pico by:
- Click the Save button, or use Ctrl+S keys.
- In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
- Save the file as main.py
- Click the green Run button (or press F5) to run the script. The script will execute.
- Check the water flow.
If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.
Code Explanation
You can find the explanation in the comments of the Raspberry Pi Pico code provided.