Raspberry Pi - Water/Liquid Valve
This tutorial instructs you how to use a Raspberry Pi and a solenoid valve to control the flow of liquids such as water, beer, and oil. The same principles apply to controlling gas flow.
Between the Raspberry Pi and the valve, we can place either a relay or a MOSFET module. Both of them open and close the valve, and that is all a solenoid valve needs. A relay does it with a moving metal contact. The MOSFET module does it with no moving part, so it switches without a click, reacts faster, and has no contacts that wear out. You will learn how to use each one here.
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
The Water/Liquid Valve Pinout

A Solenoid Valve generally has two terminals:
- The Positive (+) pin (red) should be connected to 12V of a DC power supply
- The Negative (-) pin (black or other) should be connected to the GND of a DC power supply
How Water/Liquid Valve works
Typically, the valve is shut. When 12V DC is supplied to the two terminals, the valve opens and water/liquid can pass through.
※ NOTE THAT:
- A gasket arrangement is present inside, thus requiring a minimum pressure to open the valve after 12V DC is applied. This pressure can be generated by liquid flow.
How to Control Water/Liquid Solenoid Valve using Raspberry Pi
If the valve is supplied with 12V, it will open. In order to control the valve, a relay needs to be used between Raspberry Pi and the valve. Raspberry Pi can manage the solenoid valve through the relay.
If you are not familiar with the relay (pinout, how it works, how to program, etc.), then check out the Raspberry Pi - Relay tutorial for more information.
The MOSFET module has its own guide as well: Raspberry Pi - MOSFET Module. Read it if this module is new to you.
Relay vs MOSFET Module for a Solenoid Valve
| Relay | MOSFET Module | |
|---|---|---|
| Valve control | open / close (ON/OFF) | open / close (ON/OFF), no PWM for a valve |
| Click sound | yes, each time | no |
| Response | slower, a few milliseconds | almost instant |
| Parts that wear | the relay contacts | nothing, it has no moving parts |
| Valve power | AC or DC | DC only |
| Supply voltage | any value the relay allows | not lower than about 6V |
| Isolation | yes, between coil and contacts | yes, with a PC817 optocoupler |
| Suitable for | AC valves, simple projects | 12V DC valves that open and close many times, quiet places |
Both choices give the same result for a 12V DC valve: open or closed. The MOSFET module is nice when the valve switches often or when the click of a relay is annoying. Do not try to "half open" a solenoid valve with PWM. It is made to be fully open or fully closed.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Raspberry Pi Code for Controlling Liquid Valve
The code below turns the water valve ON and OFF every five seconds.
Detailed Instructions
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Create a Python script file valve.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Check out the water flow.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!
How to Control Water/Liquid Valve with MOSFET Module
You can swap the relay for a MOSFET module. The valve still opens and closes every five seconds, but now you will not hear a click.
The relay and the MOSFET module use different GPIO pins on this page. The relay uses GPIO12 (pin 32), and the MOSFET module uses GPIO16 (pin 36). So each wiring has its own script.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

- Connect the PWM pin of the MOSFET module to GPIO16 (pin 36) of the Raspberry Pi.
- Connect the GND pin of the module to a GND pin of the Raspberry Pi (pin 34).
- Put the 12V adapter positive and the valve red wire together into the + terminal.
- Connect the valve black wire to the LOAD terminal.
- Connect the 12V adapter negative to the − terminal.
- The valve has a coil inside, so place a flyback diode (1N4007 or 1N5819) across the valve wires. The striped side (cathode) goes to the valve positive wire.
Raspberry Pi Code
The script works like the relay script: HIGH turns the valve ON (open) and LOW turns it OFF (closed). The valve stays open for five seconds, then closed for five seconds, and this repeats. The script prints nothing.
A 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
- Make sure the wiring matches the diagram above, then plug in the 12V adapter.
- Connect your PC to the Raspberry Pi via SSH, as in the relay Quick Steps above.
- Create a Python script file valve_mosfet.py and add the following code:
- Save the file and run it:
- The valve opens for five seconds and closes for five seconds, again and again, with no click. Press Ctrl + C to stop.
Note that the MOSFET module only switches DC, and its load supply must be about 6V or more. If your valve runs on AC power, stay with the relay.