Arduino Mega - Water/Liquid Valve

In this guide, we will learn how to control the flow of liquids such as water, beer, and oil using an Arduino Mega and a solenoid valve. We will cover:

The valve can be controlled by a relay or by a MOSFET module. Both of them only open and close the valve, because a valve has just two states. The MOSFET module, however, works without a click, switches much faster, and has no contacts that wear out. We explain both options.

Arduino Mega Water/Liquid Valve

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
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×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 Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

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

Pinout

Arduino Mega Water/Liquid Valve Pinout

A solenoid valve usually has two ports.

  • Connect the red wire to the +12V DC power supply.
  • Connect the black wire to the ground (GND) terminal of the DC power supply.

How Water/Liquid Valve works

Normally the valve is closed. When you connect 12V DC to the two terminals, the valve opens and water or other liquids can flow through.

※ NOTE THAT:

Some valves have a seal inside and need a minimum pressure to open after you apply 12 V DC. This pressure can come from the liquid flow. Also, some valves let liquid flow in only one direction.

How to Control Water/Liquid Solenoid Valve

The valve opens when it gets 12V. To control the valve, you need a relay between the Arduino Mega and the valve. The Arduino Mega uses the relay to turn on the solenoid valve. If you don’t know how a relay works, what its pins do, or how to program it, you should read the Arduino Mega - Relay tutorial.

You can also open the valve with a MOSFET module. How the module works and how to wire it are covered in the Arduino Mega - MOSFET Module tutorial.

Solenoid Valve: Relay vs MOSFET Module

Relay MOSFET Module
What it does to the valve open or close open or close (no PWM for a valve)
Noise the relay clicks no noise from the module
Response slow, a few milliseconds very quick
Wear the contacts wear out slowly nothing moving inside
Valve type AC or DC valve DC valve only
Valve supply any voltage in the relay rating about 6V minimum
Isolation yes (coil and contacts) yes (PC817 optocoupler)
Better for AC valves, simple jobs 12V DC valves that open and close often

Both parts give the same result for a 12V valve: open or closed. Pick the MOSFET module if you want a quiet system that lasts long, for example a valve that opens many times each day. Pick the relay for an AC valve.

Wiring Diagram

The wiring diagram between Arduino Mega water valve

This image is created using Fritzing. Click to enlarge image

Arduino Mega Code

This code will turn the water valve on for five seconds, then turn it off for five seconds.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-water-liquid-valve */ #define RELAY_PIN 3 // The Arduino Mega 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 steps one by one:

  • Connect the Arduino Mega to the water valve as shown in the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the correct board (Arduino Mega) and the COM port.
  • Copy the code and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
  • Watch the water flow.

Code Explanation

The explanation is in the comments of the Arduino code I provided.

How to Control Water Valve with MOSFET Module

Here the relay is replaced by a MOSFET module. The valve still only opens and closes, but now it happens silently.

Note: this page uses a different pin for each part. The relay is on pin 3, and the MOSFET module is on pin D2. That is why the MOSFET wiring has its own code.

Wiring Diagram

The wiring diagram between Arduino Mega solenoid valve MOSFET module

This image is created using Fritzing. Click to enlarge image

Wire Connect to
Arduino Mega pin D2 Module PWM pin
Arduino Mega GND Module GND pin
12V adapter + Module + terminal
Valve red wire (+) Module + terminal (together with the adapter +)
Valve black wire (−) Module LOAD terminal
12V adapter − Module − terminal

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

Arduino Mega Code

This code works like the relay code: it sets pin D2 HIGH to open the valve for 5 seconds, then LOW to close it for 5 seconds, and repeats.

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

Why no fade code here? The MOSFET module tutorial uses PWM to fade, but a solenoid valve must only get full ON or full OFF. Never send PWM to it.

Detailed Instructions

  • Wire the valve and the MOSFET module as shown above. Keep the 12V adapter unplugged for now.
  • Connect the Arduino Mega to your computer and open the Arduino IDE, just like in the relay Quick Steps.
  • Copy the code above, paste it into the Arduino IDE and click Upload.
  • Plug in the 12V adapter.
  • The valve opens for 5 seconds and closes for 5 seconds, and you hear no relay click.

Tip: the MOSFET module is for DC valves only, and the valve supply must be at least about 6V. An AC valve needs 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!