Arduino Nano - Pump

This tutorial instructs you how to use Arduino Nano to control a pump. From this tutorial, you can create an irrigation system, an aquarium, a water refilling system .. and more.

Two parts can sit between the Nano and the pump: a relay or a MOSFET module. With a relay, the pump is simply ON or OFF. With a MOSFET module, the Nano can also run the pump slower or faster, which changes how much water flows. Both ways are explained below.

Hardware Preparation

1×Official Arduino Nano
1×Alternatively, DIYables Nano V3.0 Development Board
1×USB A to Mini-B USB cable
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×Breadboard
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

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

The Pump Pinout

Arduino Nano Pump pinout
image source: diyables.io

The 12V Pump typically has two pins:

  • The negative (-) pin (black) should be connected to the GND of the DC power supply
  • The positive (+) pin (red) should be connected to the 12V of the DC power supply

How to Control Pump using Arduino Nano

If a 12V pump is powered by a 12V power supply, it will work. In order to control the pump, a relay must be used between the Arduino Nano and the pump. The Arduino Nano can then control the pump through the relay.

If you are unfamiliar with relays (pinout, how they work, how to program them, etc.), please refer to the Arduino Nano - Relay tutorial for more information.

For the MOSFET way, you may want to read the Arduino Nano - MOSFET Module tutorial first. It explains the module pins and the PWM signal.

Pump Control: Relay or MOSFET Module

Relay MOSFET Module
Pump control running or stopped running or stopped, plus flow rate (PWM pump speed)
Switching noise audible click none
Reaction time slow (milliseconds) very fast
Lifetime contacts get worn over time no moving parts inside
Pump power AC or DC DC only
Pump supply any voltage within rating at least about 6V
Isolation yes (coil and contacts) yes (PC817 optocoupler)
Use it for AC pumps, plain ON/OFF 12V DC pumps when you need flow control

For a small 12V DC pump, the MOSFET module gives you more control: a gentle flow for plants, or full flow to fill a tank fast. For a pump that runs on AC mains, stay with the relay.

Wiring Diagram

The wiring diagram between Arduino Nano and Pump

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code

The code below runs an infinite loop that turns the pump ON for five seconds and OFF for five seconds.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-pump */ #define RELAY_PIN 2 // The Arduino Nano pin connected to the IN pin of relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin A5 as an output. pinMode(RELAY_PIN, OUTPUT); } // The loop function repeats indefinitely 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

  • Attach the Arduino Nano to your computer using a USB cable.
  • Launch the Arduino IDE and select the appropriate board and port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano.
  • Check out the pump's state change.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Check out this tutorial on Arduino Nano - if a button is pressed, turn the pump on after 10 seconds

How to Control Pump Flow with MOSFET Module

Now let's swap the relay for a MOSFET module. The pump wiring changes a little, and the Nano gets the power to set the pump speed.

Wiring Diagram

The wiring diagram between Arduino Nano and water pump MOSFET module

This image is created using Fritzing. Click to enlarge image

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

Make these connections:

  • Arduino Nano D9 → PWM pin of the MOSFET module
  • Arduino Nano GND → GND pin of the MOSFET module
  • 12V adapter + → + terminal, together with the pump + (red wire)
  • Pump − (black wire) → LOAD terminal
  • 12V adapter − → − terminal
  • Flyback diode (1N4007 or 1N5819) across the pump: stripe (cathode) to pump +, other end to pump −. The pump has a motor coil inside, so do not skip it.

Arduino Nano Code

The code runs the pump at full power for 2 seconds, stops it for 2 seconds, then increases the power from 10% to 100% and decreases it again. You will see the water flow grow stronger and then weaker.

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

  • If needed, set up the board and port in Arduino IDE as in the relay Quick Steps.
  • Do the wiring shown above. Keep the pump inlet in water, and do not plug in the 12V adapter yet.
  • Upload the code to the Arduino Nano.
  • Plug in the 12V adapter.
  • Open the Serial Monitor at 9600 baud. The output looks like this:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' 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 Nano on COM15
2

Good to know: the MOSFET module handles DC pumps only, with a supply of about 6V or more. For an AC pump, keep using the relay.

Video Tutorial

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