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

1×ESP8266 NodeMCU ESP-12F
1×Alternatively, ESP8266 D1 Mini NodeMCU ESP-12F
1×Alternatively, ESP8266 NodeMCU ESP-12E (Uno-form)
1×Micro USB Cable
1×Alternatively, ESP8266 NodeMCU ESP-12F (SMD-form)
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
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 ESP8266

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

ESP8266 NodeMCU 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 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

The wiring diagram between ESP8266 NodeMCU and Pump

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.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-pump */ #define RELAY_PIN D8 // The ESP8266 pin connected to the IN pin of relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin D8 as an output. pinMode(RELAY_PIN, OUTPUT); } // The loop function repeats indefinitely void loop() { digitalWrite(RELAY_PIN, HIGH); // turn on pump 4 seconds delay(4000); digitalWrite(RELAY_PIN, LOW); // turn off pump 4 seconds delay(4000); }

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

The wiring diagram between ESP8266 NodeMCU and NodeMCU water pump MOSFET module

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

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-pump */ const int MOSFET_PIN = D5; // ESP8266 pin D5 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); }

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.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' 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
Nodemcu 1.0 (ESP-12E Module) on COM15
2

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.

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!