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.

Arduino Mega pump

Hardware Preparation

1×Arduino Mega
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×12V Pump
1×Vinyl Tube
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
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.

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.

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!