Arduino Mega - Control Pump
In this guide we will show how to connect a 12-volt pump to an Arduino Mega and how to write code to control the pump. This tutorial can help you build simple systems like plant watering, fish tanks, or water refill stations.
The pump can be driven by a relay or by a MOSFET module. With the relay, the pump is either running or stopped. With the MOSFET module, the Arduino Mega can also change how fast the pump runs, so you can set how much water flows. The MOSFET module is silent, too. You will learn both methods here.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of 12V Pump
Pinout

A 12-volt pump usually has two pins.
- Connect the black wire (negative, -) to the power supply's ground.
- Connect the red wire (positive, +) to the power supply's 12V.
How to Control Pump
Using a 12V power supply for a 12V pump will make it run. To control the pump with an Arduino Mega, place a relay between the Arduino Mega and the pump. The Arduino Mega can control the pump through the relay. If you don’t know about relays (pins, how they work, programming, etc.), learn from the Arduino Mega - Relay tutorial.
Another way is to put a MOSFET module between the Arduino Mega and the pump. To understand this module first, read Arduino Mega - MOSFET Module.
Pump Control: Relay vs MOSFET Module
| Relay | MOSFET Module | |
|---|---|---|
| Control | run or stop | run or stop, plus pump speed and water flow (PWM) |
| Sound | makes a click | quiet |
| Speed of switching | slow, in milliseconds | very fast |
| Lifetime | contacts become worn over time | no moving contacts |
| Pump power | AC or DC pump | DC pump only |
| Supply voltage for the pump | any value in the relay rating | at least about 6V |
| Electrical isolation | yes, between coil and contacts | yes, through a PC817 optocoupler |
| Suits | AC pumps and plain on/off jobs | DC pumps with flow control or frequent switching |
If your project needs a gentle flow, like slow watering for small plants, choose the MOSFET module for the 12V pump. If the pump runs on AC mains, choose the relay.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Arduino Mega Code
The code below keeps turning the pump on for five seconds, then off for five seconds.
Detailed Instructions
Follow these steps one at a time.
- Connect the pump to the Arduino Mega using a relay, as shown in the diagram.
- Connect the Arduino Mega to your computer with a USB cable.
- Open the Arduino IDE on your computer.
- Select the correct board (Arduino Mega) and the right COM port.
- Copy and paste the provided code into the Arduino IDE.
- Click the Upload button in the Arduino IDE to upload the code to the Arduino Mega.
- Check the pump status.
Code Explanation
The explanation is in the Arduino code comments above.
How to Control Pump with MOSFET Module
Now let us drive the same 12V pump through a MOSFET module, so the Arduino Mega can change the water flow.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
| Arduino Mega / Power side | MOSFET Module |
|---|---|
| Pin 2 | PWM pin |
| GND | GND pin |
| 12V adapter + and pump red wire (+) | + terminal (both wires together) |
| Pump black wire (−) | LOAD terminal |
| 12V adapter − | − terminal |
The pump motor makes a voltage spike when it turns off. Protect the module with a flyback diode (1N4007 or 1N5819) across the pump: the stripe (cathode) goes to the pump + wire, the other end to the pump − wire.
Arduino Mega Code
The pump runs at full power for 2 seconds, then stops for 2 seconds. Next, the power goes up from 10% to 100% and back down, so the water flow grows stronger and then weaker.
Detailed Instructions
- Place the pump inlet in water, and connect the parts as in the wiring diagram above. Leave the 12V adapter unplugged.
- Follow the relay Quick Steps above to connect the board and pick the board and COM port.
- Paste the code into the Arduino IDE and click Upload.
- Plug in the 12V adapter.
- Open the Serial Monitor with baud rate 9600, and watch the water flow change.
Note: a MOSFET module is for DC pumps only, with a supply of about 6V or higher. Keep the relay for AC pumps.
Video Tutorial
Challenge Yourself
- Make an automatic watering system with Arduino Mega, a soil moisture sensor, and a pump.
- Make an automatic water refill system with Arduino Mega, a water level sensor, and a pump.