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
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
The Pump Pinout

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

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.
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

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.
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:
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.