Arduino Nano ESP32 - Control Pump
This tutorial provides instructions on how to use Arduino Nano ESP32 to control a pump. You can extend this tutorial to build the DIY aquarium, cocktail machine, coffee machine, or irrigation system...
You can drive the pump in two ways. One is a relay: it only starts and stops the pump. The other is a MOSFET module: it starts and stops the pump too, but it can also change the pump speed with PWM, so you can give more or less water flow. It works quietly. You will learn both ways here.
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
Pump Pinout

A 12V Pump has two wires:
- Negative (-) wire (black): connect this wire to the negative wire of 12V DC power supply
- Positive (+) wire (red): connect this wire to the Positive wire of 12V DC power supply
How to Control Pump using Arduino Nano ESP32
If we connect a 12V power supply to a pump, the pump will work. We can programmatically control a pump by using Arduino Nano ESP32. To do so, we need a relay in between the pump Arduino Nano ESP32 board. We have a specific tutorial about relay, which contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, Arduino Nano ESP32 code... Learn about relay in the Arduino Nano ESP32 - Relay tutorial
A MOSFET module can take the place of the relay. The Arduino Nano ESP32 - MOSFET Module tutorial explains this module from the start, if you have not used it before.
Which One to Use with a Pump: Relay or MOSFET Module
| Relay | MOSFET Module | |
|---|---|---|
| Pump control | start and stop only | start, stop, and set the flow (PWM) |
| Switching sound | audible click | none |
| Speed of switching | slow (a few ms) | very fast |
| Life | contacts wear out | no moving parts |
| Pump type | AC or DC pump | DC pump only |
| Pump supply | as the relay allows | 6V or more |
| Isolation | yes, between coil and contacts | yes, by a PC817 optocoupler |
| Best use | AC pumps, plain ON/OFF | DC pumps, flow control, many ON/OFF cycles |
With a 12V DC pump, choose the MOSFET module when you want to give less water, for example a slow drip for plants. If your pump runs on AC, or ON/OFF is all you need, the relay is fine.
Wiring Diagram between ESP32, Relay and Pump

This image is created using Fritzing. Click to enlarge image
Arduino Nano ESP32 - Pump Code
The below code periodically turns the pump ON/OFF in every 4 seconds.
Detailed Instructions
To get started with Arduino Nano ESP32, follow these steps:
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Wire the components according to the provided diagram.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano ESP32 board and its corresponding COM port.* Copy the above code and paste it to Arduino IDE.
- Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
- See the pump's state
Line-by-line Code Explanation
The above Arduino Nano ESP32 code contains line-by-line explanation. Please read the comments in the code!
How to Control Pump Flow with MOSFET Module
Now let's run the same pump with a MOSFET module, so the Nano ESP32 can make the flow stronger or weaker.
Wiring Diagram between ESP32, MOSFET Module and Pump

This image is created using Fritzing. Click to enlarge image
Connections:
- D9 of the Nano ESP32 goes to the module's PWM pin, and GND of the Nano ESP32 goes to the module's GND pin.
- The 12V adapter + and the pump red wire (+) both go into the + terminal.
- The pump black wire (−) goes into the LOAD terminal.
- The 12V adapter − goes into the − terminal.
- Add a flyback diode (1N4007 or 1N5819) across the pump: stripe (cathode) to pump +, the other end to pump −. The pump motor is a coil, and the module has no diode on board.
Arduino Nano ESP32 - Pump Flow Code
We use the code from the MOSFET module tutorial without any change. The pump runs at full flow for 2 seconds and stops for 2 seconds. After that, the flow goes up from 10% to 100% and then down to 0%.
Detailed Instructions
- If needed, set up the Arduino IDE first, as shown in the relay Quick Steps.
- Put the pump in water. Do not run it dry.
- Wire the parts as in the diagram above, with the 12V adapter unplugged.
- Upload the code to the Arduino Nano ESP32.
- Plug in the 12V adapter and open the Serial Monitor at 9600 baud.
- Look at the water from the tube: it flows hard, stops, then grows and gets weak again.
Remember: the MOSFET module is for DC pumps only, with a pump supply of about 6V or higher. For an AC pump, keep using the relay.