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:

Both a relay and a MOSFET module can open and close the valve. Neither one can half-open it: a solenoid valve is only open or closed. The MOSFET module has other good points. It switches without a click, it is faster, and it has no contacts that wear out. We will show you both ways.

Raspberry Pi Pico Water/Liquid Valve

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
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
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

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 Water/Liquid Valve

Pinout

Raspberry Pi Pico 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.

A MOSFET module can do the same job. You can read how it works in the Raspberry Pi Pico - MOSFET Module tutorial.

Relay vs MOSFET Module for a Solenoid Valve

Relay MOSFET Module
Valve control open or closed open or closed (no PWM for a valve)
Sound of the switch click none
Switching speed slow (milliseconds) very fast
Wear contacts wear out after many switches no moving parts
Valve type AC or DC valves DC valves only
Valve supply any, within the relay rating at least about 6V
Isolation yes (coil and contacts) yes (PC817 optocoupler)
Best for AC valves, or when you already have a relay 12V DC valves that open and close often

Do not try to control the flow amount with PWM. A solenoid valve is made for full ON or full OFF only. Choose the MOSFET module for a quiet 12V DC valve, and the relay for an AC valve.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico water valve

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.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-water-liquid-valve """ from machine import Pin import time RELAY_PIN = 1 # The Raspberry Pi Pico pin GP1 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

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.

How to Control Water/Liquid Valve with MOSFET Module

You can replace the relay with a MOSFET module. The valve still only opens and closes.

The relay and the MOSFET module are on different pins on this page. The relay uses GP1, and the MOSFET module uses GP18 (pin 24). So each wiring has its own code.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico water valve MOSFET module

This image is created using Fritzing. Click to enlarge image

Connect To
Pico GP18 (pin 24) PWM pin of the module
Pico GND GND pin of the module
12V adapter + + terminal
Valve + (red) + terminal, together with the adapter +
Valve − (black or other) LOAD terminal
12V adapter − − terminal
1N4007 or 1N5819 diode across the two valve wires, stripe (cathode) on the valve +

The valve has a coil inside, so the flyback diode is needed. It protects the module when the valve turns off.

Raspberry Pi Pico Code

The idea is the same as the relay code: when GP18 is HIGH, the valve opens for five seconds, and when GP18 is LOW, the valve closes for five seconds. This repeats forever. The code prints nothing to the Shell.

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

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-water-liquid-valve """ from machine import Pin import time MOSFET_PIN = 18 # The Raspberry Pi Pico pin GP18 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 in the diagram above.
  • Connect the Pico to your computer and open Thonny IDE, as in the relay Quick Steps above.
  • Paste this code into Thonny, save it on the Pico as main.py, and click Run.
  • Plug in the 12V adapter.
  • The valve opens for five seconds and closes for five seconds, but now you hear no relay click.

Tip: the MOSFET module needs a DC supply of about 6V or more. If your valve runs on AC, use 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!