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 ...
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 12V Pump
The Pump Pinout
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.
Wiring Diagram
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 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:
- Create a Python script file pump.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- 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.