Arduino UNO R4 - Control Pump

In this guide, we will learn how to connect an 12V pump to Arduino UNO R4 and how to program an Arduino UNO R4 to control the pump. This tutorial will help you build systems like irrigation for plants, aquariums, or water refilling stations.

You can put a relay or a MOSFET module between the Arduino UNO R4 and the pump. A relay only starts and stops the pump. A MOSFET module does the same, and it can also set the pump speed, so you get a strong flow or a gentle one. Both ways are covered below.

Arduino UNO R4 pump

Overview of 12V Pump

Pinout

Arduino UNO R4 Pump Pinout
image source: diyables.io

A 12V pump typically has two pins.

  • Connect the negative (-) pin (black) to the GND of the DC power supply.
  • Connect the positive (+) pin (red) to the 12V of the DC power supply.

How to Control Pump

If you use a 12V power supply for a 12V pump, it will works. To control the pump using Arduino UNO R4, a relay should be placed between the Arduino UNO R4 and the pump. The Arduino UNO R4 can control the pump through the relay. If you're unfamiliar with relays (their pinout, operation, programming, etc.), you should study them in the Arduino UNO R4 - Relay tutorial.

New to the MOSFET module? The Arduino UNO R4 - MOSFET Module tutorial explains what it is and how it works.

Relay vs MOSFET Module for a 12V Pump

Relay MOSFET Module
Pump control start and stop only start, stop, and change the flow (PWM)
Switching sound clicks none
Switching time slow (milliseconds) very fast
Wear the contacts get old with use no moving parts to wear
Works with AC and DC pumps DC pumps only
Pump supply voltage what the relay is rated for 6V or higher
Isolation yes yes, with a PC817 optocoupler
Choose it for AC pumps or plain ON/OFF DC pumps where you want to set the flow

If you only need to fill a tank, the relay is enough. If you want to water plants slowly or fill a glass without splashing, the MOSFET module gives you better control. AC pumps need the relay.

Wiring Diagram

The wiring diagram between Arduino UNO R4 Pump

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code

The code below repeatedly switches the pump ON for five seconds and then OFF for five regimes.

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-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 instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the pump to Arduino UNO R4 via a relay according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy and paste the provided code into the Arduino IDE.
  • Press the Upload button in the Arduino IDE to send the code to the Arduino UNO R4.
  • Check the state of the pump.

Code Explanation

The explanation is in the comments of the Arduino code above.

How to Control Pump Flow with MOSFET Module

This time the pump is driven by a MOSFET module. Besides ON and OFF, the Arduino UNO R4 can now make the pump push more water or less water.

Wiring Diagram

The wiring diagram between Arduino UNO R4 pump MOSFET module

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Uno R4 and other components.

Arduino UNO R4 / Power MOSFET Module
Pin 9 PWM
GND GND
12V adapter + and pump + (red) + (both wires share it)
Pump − (black) LOAD
12V adapter − −

The pump has a motor inside. Add a 1N4007 or 1N5819 diode across the pump to protect the module: stripe (cathode) on the pump + wire, the other end on the pump − wire.

Arduino UNO R4 Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-controls-pump */ const int MOSFET_PIN = 9; // Arduino UNO R4 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); }

The pump runs at full flow for 2 seconds and stops for 2 seconds. Then the flow grows from 10% to 100%, one step every half second, and goes back down to 0%.

Detailed Instructions

  • Prepare the Arduino IDE in the same way as the relay Quick Steps above.
  • Connect the pump, the MOSFET module and the Arduino UNO R4 as in the diagram. Put the pump in water before you power it.
  • Upload the code to the Arduino UNO R4, then plug in the 12V adapter.
  • Open the Serial Monitor (9600 baud).
  • Look at the water coming out of the tube. It gets stronger, then weaker. At a very low power, the pump may not move water at all.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' 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 Uno R4 WiFi on COM15
2

Remember: the MOSFET module only switches DC, and the pump supply must be at least about 6V. Keep the relay for AC pumps.

Video Tutorial

Challenge Yourself

  • Create an automated watering system with Arduino UNO R4, a soil moisture sensor, and a pump.
  • Build an automatic refill system for water using Arduino UNO R4, 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!