ESP8266 - Pump
This tutorial instructs you how to use ESP8266 to control a pump. By adapting this tutorial, you can construct an irrigation system, an aquarium, a water refilling system, and more.
We will learn two ways to drive the pump: with a relay and with a MOSFET module. A relay can only start and stop the pump. A MOSFET module can start and stop it as well, and it can also change the pump speed with PWM, which means you can pump more or less water. It also works with no click sound.
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 ESP8266
If a 12V pump is supplied with a 12V power source, it will function. In order to control the pump, a relay must be used between the ESP8266 and the pump. The ESP8266 can then control the pump via the relay.
If you are unfamiliar with relays (pinout, operation, programming, etc.), please refer to the ESP8266 - Relay tutorial for more information.
In the same way, the ESP8266 - MOSFET Module tutorial teaches you the basics of the MOSFET module, if you have not used one yet.
Relay vs MOSFET Module for Pump
| Point | Relay | MOSFET Module |
|---|---|---|
| Pump control | start/stop only | start/stop and flow rate (PWM) |
| Noise when switching | click | silent |
| Reaction time | slow, in milliseconds | very fast |
| Durability | contacts get worn over time | no moving parts |
| Pump power | AC or DC | DC only |
| Pump voltage | within the relay rating | at least about 6V |
| Isolation | yes (coil / contacts) | yes (PC817 optocoupler) |
| Ideal for | AC pumps, simple ON/OFF | DC pumps, flow control, switching many times |
A 12V DC pump works very well with the MOSFET module, because you can make the flow gentle or strong. If your pump runs on AC mains, the relay is the one to use.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
ESP8266 Code for controlling Pump
The code below will keep the pump toggling between ON and OFF every five seconds.
Detailed Instructions
To get started with ESP8266 on Arduino IDE, follow these steps:
- Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
- Wire the components as shown in the diagram.
- Connect the ESP8266 board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
- Connect your ESP8266 to your computer using a USB cable.
- Launch the Arduino IDE, 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 ESP8266.
- Check out the state of the pump changing.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!
Check out this tutorial on ESP8266 - if button pressed, turn pump on in 10 seconds.
ESP8266 Code for Controlling Pump Flow with MOSFET Module
This part replaces the relay with a MOSFET module. NodeMCU pin D5 outputs PWM, and the duty cycle decides how fast the pump turns and how much water it moves.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.
- NodeMCU D5 (GPIO14) → module PWM
- NodeMCU G (GND) → module GND
- 12V adapter + → module +
- Pump red wire (+) → module + (together with the adapter + wire)
- Pump black wire (−) → module LOAD
- 12V adapter − → module −
The pump motor is a coil. Connect a flyback diode (1N4007 or 1N5819) across the pump wires, with the striped side (cathode) on the pump + wire.
ESP8266 Code
How the pump behaves: 2 seconds at full flow, 2 seconds stopped, then the flow grows from 10% to 100% and shrinks back to 0%. At low power, the pump may not be able to push water up the tube.
Detailed Instructions
- Do the Arduino IDE and NodeMCU setup from the relay Quick Steps above.
- Put the pump in water and wire it as in the diagram. Leave the 12V adapter unplugged.
- Upload the code above.
- Plug in the 12V adapter and look at the water flow.
- Open the Serial Monitor at 9600 baud.
Note that the MOSFET module needs a DC pump and a supply of about 6V or more. An AC pump must stay on the relay. Never let the pump run dry.