ESP8266 - Button - Pump

This tutorial instructs you how to use ESP8266 to turn on a pump for a few seconds when a button is pressed, and then turn it off.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Push Button
1×(Optional) Panel-mount Push Button
1×Relay
1×12V Pump
1×Vinyl Tube
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of Button and Pump

If you are unfamiliar with pump and button (including pinout, functionality, and programming), the following tutorials can provide guidance:

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Button controls Pump

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.

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-button-pump */ #include <ezButton.h> // include ezButton library #include <ezOutput.h> // include ezOutput library ezOutput pump(A5); // create ezOutput object attached to pin A5 ezButton button(12); // create ezButton object attached to pin 12 void setup() { Serial.begin(9600); button.setDebounceTime(50); // set debounce time to 50 milliseconds pump.low(); // turn pump off } void loop() { pump.loop(); // MUST call the loop() function first button.loop(); // MUST call the loop() function first if (button.isPressed()) { Serial.println("Pump is started"); pump.low(); pump.pulse(10000); // turn on for 10000 milliseconds ~ 10 seconds // after 10 seconds, pump will be turned off by pump.loop() function } }

※ NOTE THAT:

The code above performs the following tasks:

  • Debouncing the button, which is supported by the ezButton library. To learn more, please read Why do we need debouncing?
  • Turning the pump on for 10 seconds when the button is pressed, and then turning it off, which is supported by the ezOutput library
  • All code is non-blocking, which is supported by the ezButton and ezOutput libraries

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 a USB cable to the ESP8266 and the PC.
  • Open the Arduino IDE and select the correct board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “ezButton” and locate the button library provided by ArduinoGetStarted.
  • Press the Install button to install the ezButton library.
ESP8266 NodeMCU button library
  • Look for “ezOutput” and locate the output library provided by ArduinoGetStarted.
  • Press the Install button to install ezOutput library.
ESP8266 NodeMCU output library
  • Copy the code and open it with the Arduino IDE.
  • Then, press the Upload button to send the code to the ESP8266.
Arduino IDE Upload Code
  • Press the button
  • Check out the pump's condition

Code Explanation

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

Video Tutorial