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.

To open and close the valve, the Nano can use either a relay or a MOSFET module. Both of them only open or close the valve, because a solenoid valve works with ON/OFF only. The MOSFET module has other gains: it switches without a click, it reacts faster, and it has no contacts that wear out. This tutorial covers both ways.

Hardware Preparation

1×Official Arduino Nano
1×Alternatively, DIYables Nano V3.0 Development Board
1×USB A to Mini-B 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 Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

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

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.

If the MOSFET module is new to you, check the Arduino Nano - MOSFET Module tutorial to learn its pins and how it works.

Relay vs MOSFET Module for Solenoid Valve

Relay MOSFET Module
Valve control open or closed (ON/OFF) open or closed (ON/OFF)
Noise the relay clicks on each switch quiet, only the valve itself makes a sound
Switching time slow, some milliseconds very fast
Wear contacts wear out after many cycles no moving contacts
Valve power AC or DC DC only
Valve supply any (within rating) about 6V or more
Isolation yes, coil and contacts yes, PC817 optocoupler
Suits AC valves, simple projects 12V DC valves that open and close often

Do not send a PWM signal to a solenoid valve. It needs full power to open. For a 12V DC valve that switches many times a day, the MOSFET module is a good choice. For an AC valve, use the relay.

Wiring Diagram

The wiring diagram between Arduino Nano and water valve

This image is created using Fritzing. Click to enlarge image

Pushing jumper wires into the headers is quick, but the wires can slip out. You can plug the Arduino Nano into a Screw Terminal Expansion Board for Arduino Nano and connect the wires to its screw terminals. This is how it looks with the screw terminal board:

The wiring diagram between Arduino Nano and water valve Screw Terminal

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!

How to Control Solenoid Valve with MOSFET Module

You can also open and close the valve with a MOSFET module in place of the relay. The valve still works as ON/OFF only, but the switching is silent and has no contacts to wear out.

Please note that the relay and the MOSFET module do not share a pin on this page. The relay is on D2, while the MOSFET module is on D9. Each wiring therefore comes with its own code.

Wiring Diagram

The wiring diagram between Arduino Nano and solenoid valve MOSFET module

This image is created using Fritzing. Click to enlarge image

From To
Arduino Nano pin D9 MOSFET module PWM pin
Arduino Nano GND MOSFET module GND pin
12V adapter + and valve + MOSFET module + (both on this one terminal)
Valve − MOSFET module LOAD
12V adapter − MOSFET module −

Jumper wires can come loose when you move the project. To do that, mount the Arduino Nano on a Screw Terminal Expansion Board for Arduino Nano. The pins do not change. Just connect each wire to the matching screw terminal:

The wiring diagram between Arduino Nano and solenoid valve MOSFET module Screw Terminal

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Nano and other components.

The valve has a coil, so add a flyback diode (1N4007 or 1N5819) across the valve terminals, with the stripe (cathode) on valve +.

Arduino Nano Code

The idea is the same as in the relay code: HIGH on D9 opens the valve for five seconds, and LOW closes it 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 MOSFET_PIN 9 // The Arduino Nano pin D9 connected to the PWM pin of the MOSFET module // The setup function runs once on reset or power-up void setup() { pinMode(MOSFET_PIN, OUTPUT); // set the pin as an output } // The loop function repeats indefinitely void loop() { digitalWrite(MOSFET_PIN, HIGH); // open the valve for 5 seconds delay(5000); digitalWrite(MOSFET_PIN, LOW); // close the valve for 5 seconds delay(5000); }

A solenoid valve must get only full ON or full OFF, never PWM. For this reason, the fade code from the MOSFET module tutorial is not used here.

Detailed Instructions

  • Wire the MOSFET module and the valve as shown in the diagram.
  • Plug the USB cable into the Arduino Nano and the PC.
  • Open the Arduino IDE and select the board and port, as described in the relay Quick Steps.
  • Copy the code above and upload it to the Arduino Nano.
  • Plug in the 12V adapter. The valve opens for five seconds and closes for five seconds, and you will not hear a relay click.

Tip: the MOSFET module works only with DC valves, and the valve supply must be at least about 6V. For an AC valve, keep 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!