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

1×Raspberry Pi 5
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×Recommended: Screw Terminal Block Shield for Raspberry Pi
1×Recommended: Raspberry Pi Prototyping Base Plate & Breadboard Kit

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

The Water/Liquid Valve Pinout

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

The wiring diagram between Raspberry Pi and water valve

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:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file valve.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-water-liquid-valve """ import RPi.GPIO as GPIO import time # Set the GPIO mode (BCM or BOARD) GPIO.setmode(GPIO.BCM) # Define the GPIO pin controls the water valve via the relay module RELAY_PIN = 12 # Set the relay pin as an output pin GPIO.setup(RELAY_PIN, GPIO.OUT) try: # Run the loop function indefinitely while True: # Turn the relay ON (HIGH) to turn on the water valve GPIO.output(RELAY_PIN, GPIO.HIGH) time.sleep(5) # Wait for 5 seconds # Turn the relay OFF (LOW) to turn off the water valve GPIO.output(RELAY_PIN, GPIO.LOW) time.sleep(5) # Wait for 5 seconds except KeyboardInterrupt: # If the user presses Ctrl+C, clean up the GPIO configuration GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 valve.py
  • 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

The wiring diagram between Raspberry Pi and solenoid valve MOSFET module

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:

Raspberry Pi Screw Terminal Block Shield
  • 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:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-water-liquid-valve """ import RPi.GPIO as GPIO import time MOSFET_PIN = 16 # GPIO16 (pin 36) connected to the PWM pin of the MOSFET module GPIO.setmode(GPIO.BCM) GPIO.setup(MOSFET_PIN, GPIO.OUT) # set the pin as an output try: while True: GPIO.output(MOSFET_PIN, GPIO.HIGH) # open the valve for 5 seconds time.sleep(5) GPIO.output(MOSFET_PIN, GPIO.LOW) # close the valve for 5 seconds time.sleep(5) except KeyboardInterrupt: pass finally: GPIO.cleanup()
  • Save the file and run it:
python3 valve_mosfet.py
  • 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.

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!