ESP8266 - Rocker Switch Module
Many NodeMCU gadgets live two lives. First comes a short setup time, when you test the wiring or type in the WiFi name. Then comes the long normal life, when the gadget just does its job. A real switch on the box is a simple way to choose between the two before you plug in the power. This tutorial instructs you how to use the ESP8266 NodeMCU with the DIYables ON/OFF Rocker Switch Module for exactly that job. In detail, we will learn:
- What is on the module, and what the marks "I", "O", "ON (H)" and "OFF (L)" tell you
- How to wire it to the NodeMCU with three jumper wires: VCC to 3V3, SIG to D1 (GPIO5), GND to GND
- How to program the ESP8266 to show the rocker position on the Serial Monitor
- How to program the ESP8266 to start in SETUP MODE or NORMAL MODE, chosen by the rocker at power-up, and to report later flips with a millis() debounce
- Why D1 is a safe pin for a switch that is read at boot, and why D3, D4 and D8 are not

We suggest:
- Looking at the ESP8266 - Switch tutorial if your switch is a bare part with two legs and no board around it.
- Looking at the ESP8266 - Button Module tutorial if the key should spring back up after each push, or at ESP8266 - Slide Switch for a tiny switch that stands on the breadboard.
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 Rocker Switch Module

The module is a black KCD1 rocker switch soldered onto a small blue board, about 24 x 24mm, with a mounting hole in each corner. A 3-pin header sticks out of one side. Its pins are bent at a right angle, so the wires leave the board flat. DIYables sells the module in packs of five.
On the rocker you find two marks: "I" means ON and "O" means OFF. The print on the board next to the switch says "ON (H)" and "OFF (L)". That is a hint for the programmer: ON gives a HIGH level, OFF gives a LOW level.
A rocker switch is a maintained switch, also called a latching switch. You tip it to one side, and it stays there until you tip it back. Compare that with a push button: it is ON only while a finger holds it down. So with this module, the ESP8266 does not wait for a press. It asks "where is the rocker now?" and acts when the answer changes.
Inside, the switch is SPST: one contact, open or closed. Closed, it joins SIG to VCC. Open, a 10kΩ pull-down resistor on the board (marked "103") pulls SIG down to 0V, so the pin never floats.
| DIYables ON/OFF Rocker Switch Module | |
|---|---|
| Switch part | KCD1 rocker, SPST, maintained |
| Marks on the rocker | I = ON, O = OFF |
| Supply voltage (VCC) | 3.3V to 5V |
| SIG with the rocker on "I" | HIGH (same voltage as VCC) |
| SIG with the rocker on "O" | LOW (0V) |
| Pull-down on SIG | 10kΩ, on the board |
| Header | 3 pins, 2.54mm pitch, right-angle male |
| Size with pins | about 24 x 29 x 22mm |
The switch body also carries the print "6A 250VAC / 10A 125VAC". That is the rating of the bare switch part. On this module, the header pins only pass a small 3.3V logic signal to a GPIO pin. Never connect mains wires or motor current to them.
The Rocker Switch Module Pinout
The pin names are printed on the back of the board, beside the header. With the module face up and the header on the right, the pins from top to bottom are SIG, VCC and GND.
- SIG pin: must be connected to a digital input pin of the NodeMCU. This tutorial uses D1 (GPIO5). It is HIGH while the rocker is on "I" and LOW while it is on "O".
- VCC pin: must be connected to the 3V3 pin of the NodeMCU. Do not take power from VIN or 5V (see the note below).
- GND pin: must be connected to a GND pin of the NodeMCU (marked G on some boards).
※ NOTE THAT:
SIG simply passes on the VCC voltage when the switch is ON. The ESP8266 is a 3.3V chip. If VCC came from VIN or 5V, D1 would get 5V every time the rocker is on "I", and that can damage the pin. So 3V3 is the only right choice for VCC.
※ NOTE THAT:
The arrangement of pins on a module may differ from one manufacturer to another. It is essential to always refer to the labels printed on the module when using it. Take a close look!
Wiring Diagram
In the diagram, the NodeMCU is pressed into a breadboard, which just holds it steady. The rocker switch module does not go on the breadboard. It lies beside it, and three jumper wires run from its header to the NodeMCU pins: one brings 3.3V, one is ground, and the signal wire ends on D1.

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.
| Rocker Switch Module | ESP8266 NodeMCU |
|---|---|
| VCC | → 3V3 (not VIN, not 5V) |
| SIG | → D1 (GPIO5) |
| GND | → GND |
Why D1 and not another pin? D1 is close to the end of the pin row, so the wires do not cross. More important, the ESP8266 does not look at GPIO5 when it starts. This matters for the second example below, because the rocker can sit in either position at the moment you plug in the USB cable. Keep in mind that D1 is also the default SCL pin for I2C. If you add an I2C display later, move SIG to D5, D6 or D7 and change ROCKER_PIN in the code.
How To Program For Rocker Switch Module
You do not need any library. The ESP8266 core for Arduino IDE has all the functions below.
- Start by naming the pin. The ESP8266 core knows the NodeMCU labels, so you can write D1 directly. It means GPIO5:
- In setup(), make it a normal input with the pinMode() function. Do not use INPUT_PULLUP. The pull-down on the module already gives the OFF level.
- To catch a flip, compare each reading with the last one you trusted. When the rocker snaps over, its contacts bounce for a few milliseconds. So accept a new level only after it has stayed the same for about 50 milliseconds. The second sketch does this with millis(), so the loop never stops to wait.
ESP8266 NodeMCU Code for Rocker Switch Module - Show the Rocker Position
The first sketch is a wiring test. About three times per second, it reads D1 and prints the raw value (0 or 1) and the rocker position in words. It reads so slowly that contact bounce cannot confuse it.
Detailed Instructions
- If this is the first time you use ESP8266, see how to setup environment for ESP8266 on Arduino IDE.
- Wire the module to the NodeMCU as shown in the diagram. Check one more time that VCC is on 3V3.
- Connect the ESP8266 NodeMCU to your computer with the USB cable.
- Open Arduino IDE on your computer.
- Choose the board NodeMCU 1.0 (ESP-12E Module) and the COM port of your NodeMCU.
- Copy the code above into a new sketch and click the Upload button.
- Open the Serial Monitor and set it to 9600 baud.
- Start with the rocker on "O". After a few lines, tip it to "I", wait two seconds, and tip it back to "O".
The value does not change when you flip the rocker? Check the SIG wire and make sure it is on D1, not D2. The value jumps between 0 and 1 by itself? Then the GND or VCC wire is loose.
ESP8266 NodeMCU Code for Rocker Switch Module - Boot Mode Select
Now the rocker gets a real job. It decides how the NodeMCU starts:
- Rocker on "I" when the board powers up: the sketch runs in SETUP MODE, and the blue LED of the NodeMCU turns on.
- Rocker on "O" when the board powers up: the sketch runs in NORMAL MODE, and the blue LED stays off.
The switch is read only ONE time, inside setup(). Why not follow the rocker all the time? A mode often starts things that are hard to undo halfway, for example joining a WiFi network or opening a settings page. So the mode is fixed for the whole run. A new mode needs a fresh start: press the RST button or plug the USB cable in again.
The rocker is still watched in loop(), though. Each flip is reported with a short note. A millis() debounce makes sure that one flip gives one message: a new level counts only after it stays the same for 50 milliseconds. Right now the modes only print a banner. In your own project, you would replace the comment at the end of loop() with the real setup and normal tasks.
Detailed Instructions
- Keep the wiring as it is.
- Upload the code above in the same way as the first sketch.
- Open the Serial Monitor at 9600 baud.
- Put the rocker on "O" and press the RST button of the NodeMCU. The NORMAL MODE banner appears.
- Tip the rocker to "I", then back to "O", then to "I" again.
- Leave it on "I" and press RST once more. Now the SETUP MODE banner appears, and the blue LED near the antenna lights up.
- Tip the rocker to "O".
Each time you press RST, a few odd characters may show up before the banner. They are the boot messages of the ESP8266, sent at 74880 baud. You can ignore them.
Notice one thing: the rocker sat on "I" while you pressed RST, and the NodeMCU started without any problem. With the rocker on D1, both positions are safe at power-up. On some other pins this is not true. The table in Additional Knowledge shows why.
Additional Knowledge
Boot Pins and a Switch That Is Read at Power-Up
When the ESP8266 starts, it checks the level of three pins to decide how to boot. These are called strapping pins. A rocker switch holds its level all the time, also at that moment. So on a strapping pin, one of the two rocker positions can stop your sketch from running.
| NodeMCU Pin | GPIO | Level needed at boot | Rocker position that causes trouble |
|---|---|---|---|
| D1 | GPIO5 | not checked | none, both positions are safe |
| D3 | GPIO0 | HIGH | "O" (the pull-down pulls it toward LOW, and the chip may wait in flash mode) |
| D4 | GPIO2 | HIGH | "O" (the pull-down pulls it toward LOW, and the boot may fail) |
| D8 | GPIO15 | LOW | "I" (SIG pushes it HIGH, and the sketch does not start) |
D2, D5, D6 and D7 are not strapping pins either, so they are as safe as D1. For a boot-mode switch, pick one of these five pins and stay away from D3, D4 and D8.
Rocker Switch Module vs Bare ON/OFF Switch on the ESP8266
| Rocker Switch Module | Bare ON/OFF Switch | |
|---|---|---|
| Wires to the NodeMCU | 3 (VCC, SIG, GND) | 2 (one leg to a pin, one leg to GND) |
| Resistor | 10kΩ pull-down on the board | the internal pull-up of the ESP8266 |
| pinMode() | INPUT | INPUT_PULLUP |
| Level when the switch is ON | HIGH | LOW (inverted) |
| Pin labels | SIG, VCC, GND on the back | none |
| Soldering | no, it has header pins | usually yes, wires on the legs |
| How to mount it | 4 screw holes in the board | snaps into a hole in a case |
It is evident that the module is the quicker choice on the workbench, and its ON = HIGH logic is easier to read in code. The bare switch fits better into the wall of a finished box.
Ideas for Using the Rocker Switch Module
- WiFi setup switch: in SETUP MODE, the NodeMCU opens its own access point with a page for the WiFi name and password. In NORMAL MODE, it joins that network.
- Logging switch: send sensor values to a cloud service only while the rocker is on "I".
- Night switch for a WiFi clock: with the rocker on "O", the display goes dark.
- Debug switch: print long debug messages only when the rocker was on "I" at boot, and keep the Serial Monitor quiet the rest of the time.