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:
- How to connect the solenoid valve to Arduino UNO R4
- How to program Arduino UNO R4 to control the valve.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of 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.
Wiring Diagram
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.
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.