ESP8266 - Switch

The toggle switch, also known as the ON/OFF switch, has two states: ON (closed) and OFF (open). When pressed, the switch will toggle between the two states and maintain its state even when released.

This tutorial instructs you how to use ESP8266 with the ON/OFF switch. In detail, we will learn:

ESP8266 NodeMCU ON/OFF Switch

Do not mix up the following:

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Wires
1×ON/OFF Square Switch
1×(Alternative) ON/OFF Round Switch
1×(Optional) Heat Shrink Tubing
1×(Optional) Soldering Iron
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 ON/OFF Switch

A switch that can be toggled between two states, ON and OFF, is known as an ON/OFF Switch. When pressed, it will change its state, either from ON to OFF or OFF to ON. The switch will remain in this state until it is pressed again.

The Switch Pinout

There are two varieties of the ON/OFF Switch: two-pin and three-pin.

In this tutorial, we will be utilizing a two-pin switch. With this kind, there is no requirement to differentiate between the two pins.

ON/OFF Switch pinout

How It Works

There are two methods of using an ON/OFF switch. The following is a wiring table for the switch and the reading state on ESP8266 in both cases:

pin 1 pin 2 ESP8266 Input Pin's State
1 GND ESP8266 Input Pin (with pull-up) HIGH OFF, LOW ON
2 VCC ESP8266 Input Pin (with pull-down) HIGH ON, LOW OFF

We must select one of the two options presented. The remainder of the tutorial will utilize the first option.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and ON/OFF Switch

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.

For a secure and reliable wiring connection, we suggest using a Soldering Iron to solder the wires and pins of the ON/OFF switch. To ensure safety, we recommend using Heat Shrink Tube to cover the connection.

ESP8266 Code - ON/OFF Switch

As with a button, an ON/OFF switch also requires debouncing (for more information, please see Why needs debounce for the button, ON/OFF switch?). This can make coding complex. Fortunately, the ezButton library provides this debouncing function, as well as an internal pull-up register, making programming simpler.

※ NOTE THAT:

There are two common applications:

  • The first: If the switch is set to ON, perform one action. If the switch is set to OFF, take a different action in response.
  • The second: If the switch is flipped from ON to OFF (or OFF to ON), do something.
/* * 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-switch */ #include <ezButton.h> ezButton switch(D7); // create ezButton object that attach to ESP8266 pin D7 void setup() { Serial.begin(9600); switch.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { switch.loop(); // MUST call the loop() function first if (switch.isPressed()) Serial.println("The switch: OFF -> ON"); if (switch.isReleased()) Serial.println("The switch: ON -> OFF"); int state = switch.getState(); if (state == HIGH) Serial.println("The switch: OFF"); else Serial.println("The switch: ON"); }

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.
  • Do the wiring as indicated in the wiring diagram.
  • Connect your ESP8266 to a PC using a USB cable.
  • Open the Arduino IDE.
  • Install the ezButton library. Refer to the instructions on How To.
  • Choose the correct board and port.
  • Click the Upload button on the Arduino IDE to upload the code to the ESP8266.
  • Turn the switch to the ON position.
  • Check out the result on the Serial Monitor.
  • Then turn the switch to the OFF position.
  • Check out the result on the Serial Monitor.
COM6
Send
The switch: OFF The switch: OFF The switch: OFF The switch: OFF -> ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON -> OFF The switch: OFF The switch: OFF The switch: OFF
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial