Arduino MicroPython Water/Liquid Valve

This tutorial teaches you how to control liquid flows, such as water, beer, or oil, using an Arduino with MicroPython and a solenoid valve. We will cover:

Arduino MicroPython Water/Liquid Valve

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Relay
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/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
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 MicroPython 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 another color) to the GND of the DC power supply.

How Water/Liquid Valve works

Normally, the valve remains closed. When you apply 12V DC to the two terminals, the valve opens, allowing water or other fluids to flow through.

※ NOTE THAT:

Some valves have a gasket and need a certain amount of pressure to open when 12V DC is applied. This pressure is provided by the liquid flow. Additionally, some valves only allow liquid to flow in one direction.

How to Control Water/Liquid Solenoid Valve

When the valve receives 12V power, it opens. To control the valve with an Arduino, you need to use a relay. The Arduino sends signals to the relay, which then controls the valve. If you're unfamiliar with how a relay works, its pin configuration, or how to set it up, check out the Arduino MicroPython Relay tutorial.

Wiring Diagram

The wiring diagram between Arduino MicroPython water valve

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code

This code will switch the water valve ON for five seconds, then switch it OFF for another five seconds.

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-water-liquid-valve """ from machine import Pin import time RELAY_PIN = 'D5' # The Arduino Giga WiFi pin D5 connected to the water valve via relay pin # Initialize the relay pin as an output relay = Pin(RELAY_PIN, Pin.OUT) # Infinite loop while True: relay.value(1) # Turn on water valve for 5 seconds time.sleep(5) relay.value(0) # Turn off water valvefor 5 seconds time.sleep(5)

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the Arduino board to the water valve according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Check the water flow.

Code Explanation

You can find the explanation in the comments of the Arduino MicroPython code provided.

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!