Arduino Nano - Water/Liquid Valve

This tutorial instructs you how to use an Arduino Nano and a solenoid valve to control the flow of liquids such as water, beer, and oil. The same principles apply for controlling the flow of gas.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Relay
1×Liquid Solenoid Valve
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of Water/Liquid Valve

The Water/Liquid Valve Pinout

Arduino Nano Water/Liquid Valve pinout

A solenoid valve typically has two terminals:

  • The Positive (+) pin (which is usually red) should be connected to the 12V of a DC power supply
  • The Negative (-) pin (which is usually black or another color) 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 liquid/water can pass through.

※ NOTE THAT:

  • A gasket arrangement is inside, so a minimum pressure is necessary to open the valve after 12V DC is applied. This pressure can be generated by the flow of liquid.

How to Control Water/Liquid Solenoid Valve using Arduino Nano

If the valve is powered by a 12V power supply, it will open. In order to control the valve, a relay needs to be used between Arduino Nano and the valve. The solenoid valve can be managed by Arduino Nano through the relay.

If you are not familiar with relays (pinout, how it works, how to program, etc.), please refer to the Arduino Nano - Relay tutorial for more information.

Wiring Diagram

The wiring diagram between Arduino Nano and water valve

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code for Controlling Liquid Valve

The code below turns the water valve ON for five seconds and OFF for five seconds.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-water-liquid-valve */ #define RELAY_PIN 2 // The Arduino Nano pin connected to the IN pin of relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin A5 as an output. pinMode(RELAY_PIN, OUTPUT); } // The loop function repeats indefinitely void loop() { digitalWrite(RELAY_PIN, HIGH); // open valve 5 seconds delay(5000); digitalWrite(RELAY_PIN, LOW); // close valve 5 seconds delay(5000); }

Detailed Instructions

  • Plug the USB cable into the Arduino Nano and the PC.
  • Open the Arduino IDE, select the appropriate board and port.
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Measure the water flow rate.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Video Tutorial