Raspberry Pi Pico - Control Pump

In this guide, you will learn how to connect a 12V pump to a Raspberry Pi Pico and how to program the Raspberry Pi Pico to control the pump. This tutorial will help you create systems for watering plants, aquariums, or water refill stations.

You can drive the pump through a relay or through a MOSFET module. A relay only turns the pump ON or OFF. A MOSFET module does that as well, but it can also change the pump speed, and so the water flow, with PWM. It does not click either. In this tutorial, we will learn how to use both.

Raspberry Pi Pico pump

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
1×Relay
1×Alternatively, MOSFET Module (AOD4184, LR7843 or FR120N)
1×12V Pump
1×Vinyl Tube
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

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 12V Pump

Pinout

Raspberry Pi Pico Pump Pinout
image source: diyables.io

A 12V pump usually has two pins.

  • Attach the negative (-) pin (black) to the GND on the DC power supply.
  • Attach the positive (+) pin (red) to the 12V on the DC power supply.

How to Control Pump

A 12V power supply is suitable for a 12V pump. To control the pump with a Raspberry Pi Pico, use a relay between them. The Raspberry Pi Pico controls the pump via the relay. If you don't know how relays work (including their pinout, operation, and how to program them), you should learn about them in the Raspberry Pi Pico - Relay tutorial.

The MOSFET module is explained step by step in the Raspberry Pi Pico - MOSFET Module tutorial, if you have not used it before.

Relay vs MOSFET Module for a Pump

Relay MOSFET Module
Control pump ON or OFF pump ON or OFF, plus flow (speed) with PWM
Sound a click at every switch silent
Switching slow, in milliseconds very fast
Lifetime metal contacts wear out in time no moving parts
Pump type AC or DC pumps DC pumps only
Supply voltage any, as rated at least about 6V
Isolation yes, coil and contacts yes, PC817 optocoupler
Best for AC pumps, plain ON/OFF 12V DC pumps, gentle or adjustable flow

With a 12V DC pump, the MOSFET module lets you give plants a soft, slow flow instead of a full blast. If your pump runs on AC, the relay is the one to use.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Pump

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code

The code below turns the pump ON for five seconds and OFF for five cycles.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-controls-pump """ from machine import Pin import time RELAY_PIN = 1 # The Raspberry Pi Pico pin GP1 connected to the pump 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 pump for 5 seconds time.sleep(5) relay.value(0) # Turn off pump for 5 seconds time.sleep(5)

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the 12V pump according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Verify that the pump is working correctly.

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Code Explanation

The comments in the Raspberry Pi Pico code above explain everything.

Raspberry Pi Pico Code - Control Pump with MOSFET Module

Now let's swap the relay for a MOSFET module. The Pico changes the pump speed by sending PWM on GP18.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico water pump MOSFET module

This image is created using Fritzing. Click to enlarge image

  • GP18 of the Pico goes to PWM on the module, and a Pico GND pin goes to GND on the module.
  • The 12V adapter: positive wire to +, negative wire to −.
  • The pump: red wire (+) also to +, black wire (−) to LOAD.
  • The pump has a coil, so put a 1N4007 or 1N5819 diode across the pump wires, with the stripe (cathode) on the pump + side.

The Code

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-controls-pump """ from machine import Pin, PWM import time MOSFET_PIN = 18 # Pico GP18 connected to the PWM pin of the MOSFET module pwm = PWM(Pin(MOSFET_PIN), freq=500) # 500 Hz PWM, slow enough for the module's optocoupler pwm.duty_u16(0) # start with the load OFF def set_load(percent): pwm.duty_u16(percent * 65535 // 100) # convert 0-100% to 0-65535 print("Load power: {}%".format(percent)) while True: # turn the load fully ON, then fully OFF print("Load: ON") set_load(100) time.sleep(2) print("Load: OFF") set_load(0) time.sleep(2) # increase the power step by step (PWM) for percent in range(10, 101, 10): set_load(percent) time.sleep(0.5) # decrease the power step by step (PWM) for percent in range(90, -1, -10): set_load(percent) time.sleep(0.5) time.sleep(1)

The pump runs at full power for 2 seconds, then stops for 2 seconds. Then the power climbs from 10% to 100% and falls back to 0%. You will see the water flow grow stronger, then weaker.

Detailed Instructions

  • Set up Thonny and MicroPython as described in the relay Quick Steps.
  • Put the pump in water, and wire it with the MOSFET module as in the diagram. Do not plug in the 12V adapter yet.
  • Copy the code to Thonny and save it on the Raspberry Pi Pico as main.py.
  • Plug in the 12V adapter and press the green Run button.
  • Watch the water flow and read the Shell output.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Load: ON Load power: 100% Load: OFF Load power: 0% Load power: 10% Load power: 20% Load power: 30% Load power: 40% Load power: 50% Load power: 60% Load power: 70% Load power: 80% Load power: 90% Load power: 100% Load power: 90% Load power: 80% Load power: 70% Load power: 60% Load power: 50% Load power: 40% Load power: 30% Load power: 20% Load power: 10% Load power: 0%
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

Tip: the MOSFET module is for DC loads only and needs a supply of about 6V or more. An AC pump still needs the relay. Also, do not run the pump dry.

Video Tutorial

Challenge Yourself

  • Make a self-watering system using Raspberry Pi Pico, a soil moisture sensor, and a pump.
  • Set up a self-refilling water system with Raspberry Pi Pico, a water level sensor, and a pump.

※ 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!