Arduino Nano ESP32 - Rocker Switch Module

Look at a power strip. Its big switch often has a small lamp, so you can see from across the room if the power is on. A homemade project can have the same thing. The DIYables ON/OFF Rocker Switch Module gives you that kind of switch on a small board, and the Arduino Nano ESP32 already has an RGB LED on the board to act as the lamp. Use it as the main switch of a project box: flip it to pause a WiFi gadget without pulling the USB cable, and the LED tells everyone what state the gadget is in. In this guide, you will wire the module to the Nano ESP32 and build that status light. In detail, we will learn:

Arduino Nano ESP32 with ON/OFF rocker switch module - the built-in RGB LED shows green for ON and red for OFF

Hardware Preparation

1×Arduino Nano ESP32
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×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano ESP32

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

The part on top is a black KCD1 rocker, the kind built into power strips and desk lamps. It sits on a small blue PCB, roughly 24 mm square, with a hole in each corner for screws or standoffs. The rocker shows two symbols. Push down the end marked "I" to switch ON. Push down the end marked "O" to switch OFF. The two labels beside the rocker, "ON (H)" and "OFF (L)", give one more hint: H for HIGH and L for LOW on the signal pin.

Let go of the rocker, and nothing moves back. It rests on the end you pushed until you push the other end. Switches like this are called maintained, or latching. The short technical name is SPST: a single circuit that is either closed or open. So your sketch does not wait for a press. It asks "is the switch ON or OFF right now?", and it reacts when the answer changes.

Rocker switch module pinout showing the three header labels
image source: diyables.io

You find the pin names on the underside of the PCB, beside the right-angle header:

  • SIG pin: connect to D2 of the Arduino Nano ESP32. In the I position it carries the VCC voltage and reads HIGH. In the O position it sits at 0V and reads LOW.
  • VCC pin: connect to 3V3. 3.3V to 5V is allowed on the module, yet with this board only the 3.3V choice is safe.
  • GND pin: connect to GND (0V).

Also on the PCB: a pull-down resistor of 10 kΩ, labelled "103". Thanks to it, SIG stays at a clean LOW whenever the rocker is on O. That is why you add no resistor of your own, and why D2 is set as a plain INPUT, not INPUT_PULLUP. The logic is the natural one: HIGH stands for ON, LOW stands for OFF.

ON/OFF Rocker Switch Module
Switch type KCD1 rocker, one circuit (SPST), stays in place
Symbols on the rocker I for ON, O for OFF
SIG level VCC level (HIGH) when ON, 0V (LOW) when OFF
Supply voltage (VCC) 3.3V to 5V
Pull-down on the PCB 10 kΩ
Header 3 pins (SIG, VCC, GND), 2.54 mm pitch, right-angle male
PCB about 24 x 24 mm, one mounting hole per corner
Size including the header about 24 x 29 x 22 mm

The switch body may show "6A 250VAC / 10A 125VAC". Those numbers belong to the switch as a stand-alone part. On this module, the switch only passes a tiny signal to a GPIO pin. Never send mains power or motor current through its header pins.

※ NOTE THAT:

In the I position, the switch simply joins SIG to VCC. If VCC came from VBUS (5V), D2 would receive 5V. The ESP32-S3 chip of the Nano ESP32 handles 3.3V on its GPIO pins, not 5V, so D2 could be damaged. Keep VCC on the 3V3 pin.

Wiring Diagram

The module needs only three wires, one for each header pin. No breadboard and no resistor are used.

The wiring diagram between Arduino Nano ESP32 and rocker switch module

This image is created using Fritzing. Click to enlarge image

Rocker Switch Module Arduino Nano ESP32
SIG → D2
VCC → 3V3 (never VBUS)
GND → GND

Jumper wires are good for a quick test, but not for a final build. You can plug the Arduino Nano ESP32 into a Screw Terminal Expansion Board for Arduino Nano and connect the wires to its screw terminals. The wiring stays the same. Only the wires now go to the screw terminals, as shown below.

The wiring diagram between Arduino Nano ESP32 and rocker switch module Screw Terminal

This image is created using Fritzing. Click to enlarge image

How To Program For Rocker Switch Module

Nothing has to be installed for this part. The two sketches share these lines:

  • Give the SIG pin a name:
const int ROCKER_SWITCH_PIN = D2;
  • In setup(), make it a normal input. The pull-down on the module does the rest:
pinMode(ROCKER_SWITCH_PIN, INPUT);
  • Read the state. HIGH means the I end is down (ON). LOW means the O end is down (OFF):
int switchState = digitalRead(ROCKER_SWITCH_PIN);
  • For the second sketch, store the state you trust in a variable. A reading that disagrees with it becomes the new state only when it holds steady for 50 ms.

Arduino Nano ESP32 Code - Read the Rocker Position

This first sketch only reports. Every 500 ms, it checks D2 and shows where the rocker is, plus the raw value. Run it once to make sure the wires are right.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-rocker-switch-module */ const int ROCKER_SWITCH_PIN = D2; // Arduino Nano ESP32 pin connected to the SIG pin of the rocker switch module void setup() { Serial.begin(9600); // Plain INPUT is enough: the 10k pull-down resistor on the module // keeps SIG at LOW while the rocker is on "O". No INPUT_PULLUP here. pinMode(ROCKER_SWITCH_PIN, INPUT); } void loop() { int switchState = digitalRead(ROCKER_SWITCH_PIN); // HIGH = "I" side down (ON), LOW = "O" side down (OFF) Serial.print("Rocker: "); if (switchState == HIGH) Serial.print("ON (I side down)"); else Serial.print("OFF (O side down)"); Serial.print(", SIG = "); Serial.println(switchState); // the raw value: 1 or 0 delay(500); // two readings per second: plenty for a switch that stays where you put it }

Detailed Instructions

Try it step by step:

  • New to the Arduino Nano ESP32? Install its board package first, as explained in how to setup environment for Arduino Nano ESP32 on Arduino IDE.
  • Connect SIG, VCC and GND as in the table above. Look once more at the VCC wire: it must end on 3V3.
  • Plug the Nano ESP32 into your computer with the USB Type-C cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Nano ESP32 board (e.g., Arduino Nano ESP32) and COM port.
  • Paste the sketch into an empty window and press Upload.
  • Choose 9600 baud in the Serial Monitor.
  • Leave the rocker on O for two seconds, switch it to I for about three seconds, then back to O.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
Rocker: OFF (O side down), SIG = 0 Rocker: OFF (O side down), SIG = 0 Rocker: OFF (O side down), SIG = 0 Rocker: OFF (O side down), SIG = 0 Rocker: ON (I side down), SIG = 1 Rocker: ON (I side down), SIG = 1 Rocker: ON (I side down), SIG = 1 Rocker: ON (I side down), SIG = 1 Rocker: ON (I side down), SIG = 1 Rocker: ON (I side down), SIG = 1 Rocker: OFF (O side down), SIG = 0 Rocker: OFF (O side down), SIG = 0 Rocker: OFF (O side down), SIG = 0
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2

The state does not change between two flips. That is the latching rocker at work. A push button would go back to 0 as soon as you let go. If you see 0 on both sides of the rocker, check that the VCC wire is on 3V3 and the SIG wire is on D2.

When you flip the switch, its metal contacts bounce for a few milliseconds. This sketch looks only every half second, so it never catches the bounce. The next sketch reacts to every change, and there the bounce matters.

Arduino Nano ESP32 Code - Green or Red Status Light on the Board

Now the rocker gets its lamp, like on a power strip. ON lights the RGB LED of the Nano ESP32 in green, and OFF lights it in red. No extra LED and no extra wire are needed. The board package names the three colours LED_RED, LED_GREEN and LED_BLUE. This LED is active LOW: digitalWrite(..., LOW) turns a colour on, and HIGH turns it off. The sketch keeps blue off all the time.

The sketch also prints one line for each flip. To ignore the bounce, it notes the time of every change of the raw level with millis(). A new level counts only after it stayed the same for 50 ms. The loop never waits, so you can later add WiFi or sensor code to it, and the switch still responds at once.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-rocker-switch-module */ const int ROCKER_SWITCH_PIN = D2; // Arduino Nano ESP32 pin connected to the SIG pin of the rocker switch module const unsigned long SETTLE_TIME_MS = 50; // a new level must stay the same this long before we believe it int switchState; // the accepted state: HIGH = ON, LOW = OFF int lastReading; // the raw reading of the previous loop unsigned long lastChangeTime = 0; // when the raw reading changed for the last time int flipCount = 0; // how many times the rocker was flipped // The RGB LED of the Nano ESP32 is active LOW: LOW = colour on, HIGH = colour off void showOnLed(int state) { if (state == HIGH) { digitalWrite(LED_RED, HIGH); // red off digitalWrite(LED_GREEN, LOW); // green on } else { digitalWrite(LED_GREEN, HIGH); // green off digitalWrite(LED_RED, LOW); // red on } } void setup() { Serial.begin(9600); pinMode(ROCKER_SWITCH_PIN, INPUT); // the module has its own pull-down resistor pinMode(LED_RED, OUTPUT); pinMode(LED_GREEN, OUTPUT); pinMode(LED_BLUE, OUTPUT); digitalWrite(LED_BLUE, HIGH); // blue is not used: keep it off // Show the position the rocker already has when the board starts switchState = digitalRead(ROCKER_SWITCH_PIN); lastReading = switchState; showOnLed(switchState); Serial.print("Rocker starts at "); Serial.println(switchState == HIGH ? "ON, LED is green" : "OFF, LED is red"); } void loop() { int reading = digitalRead(ROCKER_SWITCH_PIN); if (reading != lastReading) { // the level moved: a real flip or just contact bounce lastChangeTime = millis(); // start the 50 ms timer again lastReading = reading; } // Accept the new level only after it stayed the same for SETTLE_TIME_MS. // No delay() is used, so the loop never stops and can do other work too. if (reading != switchState && millis() - lastChangeTime >= SETTLE_TIME_MS) { switchState = reading; flipCount++; showOnLed(switchState); Serial.print("Flip #"); Serial.print(flipCount); Serial.println(switchState == HIGH ? ": ON - LED green" : ": OFF - LED red"); } }

Detailed Instructions

  • Do not change the wiring.
  • Upload the code above to the Arduino Nano ESP32.
  • Keep the Serial Monitor open at 9600 baud. Watch the RGB LED on the board while you flip the rocker four times.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
Rocker starts at OFF, LED is red Flip #1: ON - LED green Flip #2: OFF - LED red Flip #3: ON - LED green Flip #4: OFF - LED red
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2

Four flips give four lines. The bounce never shows up as an extra flip. Right after an upload or a reset, the USB port of the Nano ESP32 needs a moment to come back, so the Serial Monitor may miss the "Rocker starts at" line. The LED does not need USB: it shows the start position anyway.

Using it in your own project: in loop(), put your main work under the check if (switchState == HIGH). A weather station could then upload its readings only in the I position. Flip to O, and the uploads stop while the board stays powered, with a red light as a reminder.

Module or bare ON/OFF switch?

This board also has a page for the bare 2-leg rocker switch: Arduino Nano ESP32 - Switch. The main differences:

2-leg ON/OFF switch This module
Wire count 2 3
Resistor internal pull-up (INPUT_PULLUP) 10 kΩ pull-down on the module (INPUT)
Level when ON LOW (inverted) HIGH
Pin labels none SIG, VCC, GND

For a key that returns by itself when you let go, read Arduino Nano ESP32 - Button Module.

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!