Arduino UNO R4 - Water/Liquid Valve

In this tutorial, we will learn how to control the flow of liquids like water, beer, and oil using an Arduino UNO R4 and a solenoid valve. In detail, we will learn:

Both a relay and a MOSFET module can open and close the valve. A solenoid valve is only open or closed, so the MOSFET module does not give extra control here. Its gain is different: it switches with no click, it reacts faster, and it has no metal contacts that wear out over time.

Arduino UNO R4 Water/Liquid Valve

Overview of Water/Liquid Valve

Pinout

Arduino UNO R4 Water/Liquid Valve Pinout

A solenoid valve typically has two connections.

  • Connect the positive (+) pin (red) to the 12V DC power supply.
  • Connect the negative (-) pin (black or other) to the GND of the DC power supply.

How Water/Liquid Valve works

Usually, the valve is shut. When 12V DC is connected to the two points, the valve opens, allowing water or other liquids to pass through.

※ NOTE THAT:

Some valves have a gasket inside, and they need a minimum pressure to open after applying 12V DC. This pressure can be generated by the flow of liquid. Additionally, some valves allow liquid to flow in only one direction.

How to Control Water/Liquid Solenoid Valve

When the valve receives 12V power, it opens. To manage the valve, a relay is required between the Arduino UNO R4 and the valve. The Arduino UNO R4 uses the relay to operate the solenoid valve. If you are unfamiliar with how a relay functions, its pin configuration, or how to program it, you should read the Arduino UNO R4 - Relay tutorial.

A MOSFET module can do the same job as the relay. You can find its pinout and how it works in the Arduino UNO R4 - MOSFET Module tutorial.

Relay vs MOSFET Module for a Solenoid Valve

Relay MOSFET Module
Valve control open or closed open or closed (no PWM for a valve)
Sound of the switch clicks silent
Switching speed milliseconds much faster
Wear contacts wear after many cycles no moving contacts
Valve power AC or DC DC only
Supply voltage any, within the relay rating at least about 6V
Isolation coil is apart from the contacts PC817 optocoupler
Good choice for AC valves 12V DC valves that open and close often

Both modules control the valve in the same way, so pick the one that fits your project. A 12V DC valve that opens many times a day, or sits in a quiet room, works well with the MOSFET module. An AC valve needs the relay.

Wiring Diagram

The wiring diagram between Arduino UNO R4 water valve

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code

This code will turn the water valve ON for five seconds and then turn it OFF for five.

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-water-liquid-valve */ #define RELAY_PIN 3 // The Arduino UNO R4 pin connected to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 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); }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the Arduino Uno R4 to the water valve according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the provided code and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to transfer the code to the Arduino UNO R4.
  • Monitor the water flow.

Code Explanation

The explanation is available in the comments within the provided Arduino code.

How to Control Water/Liquid Valve with MOSFET Module

You can swap the relay for a MOSFET module. The valve still only opens and closes, but now it happens silently. On this page, the relay and the MOSFET module use different pins: the relay uses pin 3 and the MOSFET module uses pin D9. So each wiring has its own code.

Wiring Diagram

The wiring diagram between Arduino UNO R4 solenoid valve MOSFET module

This image is created using Fritzing. Click to enlarge image

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

From To
Arduino UNO R4 pin D9 MOSFET module PWM
Arduino UNO R4 GND MOSFET module GND
12V adapter + MOSFET module +
Valve + (red) MOSFET module + (together with the adapter +)
Valve − MOSFET module LOAD
12V adapter − MOSFET module −

The valve has a coil inside, so a flyback diode is a must. Place a 1N4007 or 1N5819 diode across the valve, with the stripe (cathode) on the valve + side.

Arduino UNO R4 Code

The code is the same idea as the relay code: HIGH opens the valve and LOW closes it. It opens the valve for 5 seconds, then closes it for 5 seconds, again and again.

A solenoid valve needs full power or no power. Never send a PWM signal to it. This is why this page does not use the fade code from the MOSFET module tutorial.

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-water-liquid-valve */ #define MOSFET_PIN 9 // The Arduino UNO R4 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); }

Detailed Instructions

  • Wire the Arduino UNO R4, the MOSFET module and the valve as shown in the diagram above.
  • Connect the Arduino UNO R4 to your computer with a USB cable.
  • Open the Arduino IDE and select the board and COM port, like in the relay Quick Steps above.
  • Copy the code above, paste it into the Arduino IDE and click Upload.
  • Watch the water flow: the valve opens for 5 seconds and closes for 5 seconds.

※ NOTE THAT:

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