Raspberry Pi - Pump

This tutorial instructs you how to use Raspberry Pi to control a pump. You can modify this to create an irrigation system, an aquarium, a water refilling system ...

The pump can be controlled in two ways. The first way uses a relay, which only starts and stops the pump. The second way uses a MOSFET module. It also starts and stops the pump, but it can change the pump speed too, so you can choose how much water flows. It makes no clicking sound. Both ways are covered on this page.

Hardware Preparation

1×Raspberry Pi 5
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×Recommended: Screw Terminal Block Shield for Raspberry Pi
1×Recommended: Raspberry Pi Prototyping Base Plate & Breadboard Kit

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

The Pump Pinout

Raspberry Pi Pump pinout
image source: diyables.io

The 12V Pump typically has two pins:

  • The negative pin (black) should be connected to the ground of the DC power supply.
  • The positive pin (red) should be linked to the 12V of the DC power supply.

How to Control Pump using Raspberry Pi

To make 12V pump work, it needs to be powered by a 12V power supply. A relay must be used to control the pump from a Raspberry Pi. The Raspberry Pi can then be used to control the pump through the relay.

If you are unfamiliar with relays (pinout, how it works, how to program, etc.), please refer to the Raspberry Pi - Relay tutorial for more information.

To learn the MOSFET module (pinout, how it works, how to program it), see the Raspberry Pi - MOSFET Module tutorial.

Relay vs MOSFET Module for a Pump

Relay MOSFET Module
Pump control start or stop only start, stop, and flow rate (pump speed) by PWM
Lifetime limited, the contacts wear out long, there are no moving parts
Sound a click at every switch silent
How fast it switches slowly (milliseconds) very quickly
Supported pumps AC and DC pumps DC pumps only
Pump supply anything within the relay rating 6V or more is needed
Isolation yes (coil and contacts) yes (PC817 optocoupler)
Pick it when the pump runs on AC, or you only need ON/OFF you have a 12V DC pump and want to adjust the flow

With the 12V DC pump of this tutorial, both ways work. Choose the MOSFET module if you want a gentle flow, for example to water plants slowly or to fill a small cup without splashing.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pump

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code for controlling Pump

The code below turns the pump ON for five seconds, followed by turning it OFF for five seconds, repeatedly.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file pump.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-pump """ import RPi.GPIO as GPIO import time # Set the GPIO mode (BCM or BOARD) GPIO.setmode(GPIO.BCM) # Define the GPIO pin controls the pump via the relay module RELAY_PIN = 12 # Set the relay pin as an output pin GPIO.setup(RELAY_PIN, GPIO.OUT) try: # Run the loop function indefinitely while True: # Turn the relay ON (HIGH) to turn on the pump GPIO.output(RELAY_PIN, GPIO.HIGH) time.sleep(5) # Wait for 5 seconds # Turn the relay OFF (LOW) to turn off the pump GPIO.output(RELAY_PIN, GPIO.LOW) time.sleep(5) # Wait for 5 seconds except KeyboardInterrupt: # If the user presses Ctrl+C, clean up the GPIO configuration GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 pump.py
  • Check the status of the pump.

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Check out this Raspberry Pi tutorial to learn how to make a pump turn on after 10 seconds when a button is pressed.

How to Control Pump Flow with MOSFET Module

Here, a MOSFET module is used in place of the relay. The Raspberry Pi sends PWM pulses to it, and the pump speed follows the duty cycle: a higher duty cycle means more water flow.

Wiring Diagram

The wiring diagram between Raspberry Pi and water pump MOSFET module

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield

Control side:

  • MOSFET module PWM → Raspberry Pi GPIO16 (pin 36)
  • MOSFET module GND → Raspberry Pi GND (pin 34)

Power side:

  • + terminal → 12V adapter positive and pump red wire (both wires in the same terminal)
  • LOAD terminal → pump black wire
  • − terminal → 12V adapter negative
  • The pump motor is a coil, so add a 1N5819 or 1N4007 flyback diode across it. Put the striped end (cathode) on the pump positive wire.

Raspberry Pi Code

We reuse the script from the MOSFET module tutorial. It runs the pump at full flow for 2 seconds, stops it for 2 seconds, then raises the flow from 10% to 100% and lowers it back to 0%.

Detailed Instructions

  • Follow the setup in the relay Quick Steps above (operating system, SSH, RPi.GPIO).
  • Put the pump in water and wire it to the MOSFET module like the diagram above.
  • Create a Python script file MosfetModule.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-pump """ import RPi.GPIO as GPIO import time MOSFET_PIN = 16 # GPIO16 (pin 36) connected to the PWM pin of the MOSFET module GPIO.setmode(GPIO.BCM) GPIO.setup(MOSFET_PIN, GPIO.OUT) pwm = GPIO.PWM(MOSFET_PIN, 500) # 500 Hz PWM, slow enough for the module's optocoupler pwm.start(0) # start with the load OFF def set_load(percent): pwm.ChangeDutyCycle(percent) print("Load power: {}%".format(percent)) try: 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) except KeyboardInterrupt: pass finally: pwm.stop() GPIO.cleanup()
  • Plug in the 12V adapter. Then run:
python3 MosfetModule.py
  • Look at the water coming out of the tube. The flow is strong, then stops, then grows little by little and gets weaker again. At a low duty cycle, the pump may not push water at all.
  • The terminal prints:
PuTTY - Raspberry Pi
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%

Remember: the MOSFET module is for DC pumps with a supply of about 6V or more. An AC pump 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!