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:

ESP8266 NodeMCU NodeMCU rocker switch module - choose setup or normal mode at power-up

We suggest:

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×ON/OFF Rocker Switch Module
1×Breadboard
1×Jumper Wires
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 Rocker Switch Module

Rocker switch module pinout - the SIG, VCC and GND header beside the I/O rocker
image source: diyables.io

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.

The wiring diagram between ESP8266 NodeMCU and NodeMCU rocker switch 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.

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:
#define ROCKER_PIN D1 // D1 = 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.
pinMode(ROCKER_PIN, INPUT);
  • Read the rocker with the digitalRead() function. HIGH means "I" (ON), LOW means "O" (OFF):
int sig = digitalRead(ROCKER_PIN);
  • 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.

/* * 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-rocker-switch-module */ #define ROCKER_PIN D1 // ESP8266 pin D1 (GPIO5) wired to the SIG pin of the rocker switch module void setup() { Serial.begin(9600); Serial.println(); // begin on a clean line, after the boot text of the ESP8266 // Plain INPUT: the 10k pull-down on the module keeps SIG at 0V while the switch is OFF. // INPUT_PULLUP is not needed and would work against that resistor. pinMode(ROCKER_PIN, INPUT); } void loop() { int sig = digitalRead(ROCKER_PIN); // 1 = rocker on "I" (ON), 0 = rocker on "O" (OFF) Serial.print("D1 reads "); Serial.print(sig); if (sig == HIGH) Serial.println(" -> rocker ON (I)"); else Serial.println(" -> rocker OFF (O)"); delay(300); // about 3 lines per second; at this slow pace contact bounce does not matter }

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".
∞
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
D1 reads 0 -> rocker OFF (O) D1 reads 0 -> rocker OFF (O) D1 reads 0 -> rocker OFF (O) D1 reads 1 -> rocker ON (I) D1 reads 1 -> rocker ON (I) D1 reads 1 -> rocker ON (I) D1 reads 1 -> rocker ON (I) D1 reads 1 -> rocker ON (I) D1 reads 1 -> rocker ON (I) D1 reads 0 -> rocker OFF (O) D1 reads 0 -> rocker OFF (O)
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2

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.

/* * 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-rocker-switch-module */ #define ROCKER_PIN D1 // ESP8266 pin D1 (GPIO5) wired to the SIG pin of the rocker switch module #define SETTLE_MS 50 // a new switch level must stay the same this long (ms) before we trust it bool setup_mode; // chosen ONE time in setup(); loop() never changes it int trusted_level; // last debounced level of the switch int last_reading; // last raw reading, used to spot a flip (or a bounce) unsigned long changed_at = 0; // millis() time of the last raw change void setup() { Serial.begin(9600); Serial.println(); // begin on a clean line, after the boot text of the ESP8266 pinMode(ROCKER_PIN, INPUT); // the module has its own pull-down resistor pinMode(LED_BUILTIN, OUTPUT); // the blue LED of the NodeMCU (on D4 / GPIO2) // The ONE reading that picks the mode for this whole run trusted_level = digitalRead(ROCKER_PIN); last_reading = trusted_level; setup_mode = (trusted_level == HIGH); // rocker on "I" at power-up = SETUP MODE // The NodeMCU LED is active LOW: LOW turns it on, HIGH turns it off digitalWrite(LED_BUILTIN, setup_mode ? LOW : HIGH); Serial.println("========================================="); if (setup_mode) Serial.println(" SETUP MODE (rocker was ON at power-up)"); else Serial.println(" NORMAL MODE (rocker was OFF at power-up)"); Serial.println("========================================="); } void loop() { int reading = digitalRead(ROCKER_PIN); // Any raw change (a real flip or a bounce) restarts the settle timer if (reading != last_reading) { last_reading = reading; changed_at = millis(); } // Accept the new level only after it stayed the same for SETTLE_MS if (reading != trusted_level && millis() - changed_at >= SETTLE_MS) { trusted_level = reading; Serial.print("Rocker flipped to "); Serial.println(trusted_level == HIGH ? "ON (I)" : "OFF (O)"); bool wants_setup = (trusted_level == HIGH); if (wants_setup == setup_mode) { Serial.println(" -> same as the running mode. Nothing to do."); } else { Serial.print(" -> still in "); Serial.print(setup_mode ? "SETUP MODE" : "NORMAL MODE"); Serial.println(". Press RST to start the other mode."); } } // The real work of your project goes here, for example: // if (setup_mode) { ... open a settings page ... } else { ... do the normal job ... } }

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".
∞
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
========================================= NORMAL MODE (rocker was OFF at power-up) ========================================= Rocker flipped to ON (I) -> still in NORMAL MODE. Press RST to start the other mode. Rocker flipped to OFF (O) -> same as the running mode. Nothing to do. Rocker flipped to ON (I) -> still in NORMAL MODE. Press RST to start the other mode. ========================================= SETUP MODE (rocker was ON at power-up) ========================================= Rocker flipped to OFF (O) -> still in SETUP MODE. Press RST to start the other mode.
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2

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.

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!