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.

Arduino Mega pump

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
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 Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

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

Arduino Mega Pump Pinout
image source: diyables.io

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

The wiring diagram between Arduino Mega Pump

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.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-controls-pump */ // constants won't change const int RELAY_PIN = 3; // the Arduino pin, which connects to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin D3 as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); // turn on pump 5 seconds delay(5000); digitalWrite(RELAY_PIN, LOW); // turn off pump 5 seconds delay(5000); }

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

The wiring diagram between Arduino Mega pump MOSFET module

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.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-controls-pump */ const int MOSFET_PIN = 2; // Arduino Mega pin connected to the PWM pin of the MOSFET module void setup() { Serial.begin(9600); pinMode(MOSFET_PIN, OUTPUT); analogWrite(MOSFET_PIN, 0); // start with the load OFF } void setLoad(int percent) { int duty = map(percent, 0, 100, 0, 255); // convert 0-100% to 0-255 analogWrite(MOSFET_PIN, duty); Serial.print("Load power: "); Serial.print(percent); Serial.println("%"); } void loop() { // turn the load fully ON, then fully OFF Serial.println("Load: ON"); setLoad(100); delay(2000); Serial.println("Load: OFF"); setLoad(0); delay(2000); // increase the power step by step (PWM) for (int percent = 10; percent <= 100; percent += 10) { setLoad(percent); delay(500); } // decrease the power step by step (PWM) for (int percent = 90; percent >= 0; percent -= 10) { setLoad(percent); delay(500); } delay(1000); }

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.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
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%
Ln 11, Col 1
Arduino Mega Or... on COM15
2

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.

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