Arduino - Water/Liquid Valve

In this tutorial, we are going to learn how to control a liquid flow such as water, beer, oil by using Arduino and a solenoid valve. It is the same for controlling gas flow.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Relay
1×Liquid Solenoid Valve
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

About Water/Liquid Valve

Pinout

Arduino Water/Liquid Valve Pinout

Solenoid Valve usually has two terminals:

  • Positive (+) pin (red): needs to be connected to 12V of DC power supply
  • Negative (-) pin (black or other): needs to be connected to GND of DC power supply

How Water/Liquid Valve works

Normally, the valve is closed. When 12V DC is applied to the two terminals, the valve opens and water/liquid can flow.

※ NOTE THAT:

  • For some kinds of valve, there is a gasket arrangement inside, so there is a minimum pressure requires to open the valve (after 12V DC is applied). The pressure can be created by liquid flow.
  • For some kinds of valve, liquid can only flow one direction.

How to Control Water/Liquid Solenoid Valve

If the valve is powered by 12V power supply, it opens. To control the valve, we need to use a relay in between Arduino and valve. Arduino can control the solenoid valve via the relay. If you do not know about relay (pinout, how it works, how to program ...), learn about relay in the Arduino - Relay tutorial

Wiring Diagram

Arduino water valve wiring diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code

The below code repeatedly turns the water valve ON in five seconds and OFF in five seconds,

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-water-liquid-valve */ // constants won't change const int RELAY_PIN = 4; // the Arduino pin D4, which connects to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin D4 as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); // open valve 5 seconds delay(5000); digitalWrite(RELAY_PIN, LOW); // close valve 5 seconds delay(5000); }

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Check the water flow

Code Explanation

Read the line-by-line explanation in comment lines of code!

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES