ESP32 UNO - Rocker Switch Module

Many things at home have a rocker switch: the power strip under your desk, a fan, a heater, an old printer. You press one side and it stays there until you press the other side. The DIYables ON/OFF Rocker Switch Module mounts the same kind of switch on a little board with a 3-pin header. The ESP32 Uno form board reads it through one pin, D6. Reading ON or OFF is the first step. The second sketch goes further and works like a stopwatch: flip the rocker to I when you start a job, flip it to O when you stop, and the Serial Monitor tells you how long it was ON.

What you will do here:

  1. Learn what the I and O marks mean and what the SIG, VCC and GND pins do
  2. Run three jumper wires to the board, the power wire to 3.3V
  3. Print ON or OFF twice a second
  4. Turn the switch into an ON-time stopwatch with a 50 ms debounce
ESP32 Uno with the DIYables ON/OFF rocker switch module - read the switch and time how long it stays ON

Components You Need

1×ESP32 Uno-form Board
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

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 .

The Rocker Switch Module, Explained

A quick look at the hardware first. It makes the wiring and the code easy to follow.

The black part is a KCD1 rocker switch, the same type you find on power strips. Under it is a square blue PCB, roughly 24 mm on each side. Its four mounting holes let you screw it behind a cut-out in a box lid. A 3-pin right-angle header sticks out on one side.

Look at the rocker: one side has an "I", the other side an "O". I means ON and O means OFF. The print on the board says the same thing in other words, "ON (H)" and "OFF (L)". H and L stand for the HIGH and LOW level on the SIG pin.

A rocker switch is a maintained switch, which means that it stays where you put it. A push button springs back when you let go, so a button sketch waits for a press. A sketch for this module asks two other questions: which state is the switch in now, and when did that state change?

Rocker switch module pinout - the SIG, VCC and GND header pins of the DIYables board
image source: diyables.io

Hold the module with the rocker facing you and the header on the right. From top to bottom, the pins are SIG, VCC and GND. The labels are on the back, so there they read GND, VCC, SIG, and GND is the pin with the square pad.

Pin What It Does Goes To on the ESP32 Uno
SIG Output, HIGH (the VCC voltage) in the I position and LOW (0V) in the O position D6 (GPIO27)
VCC Supply input, accepts 3.3V to 5V 3.3V
GND Ground GND

The board also carries a 10 kΩ pull-down resistor (code 103). It holds SIG at 0V while the switch is OFF. So the pin never floats, and the code needs no INPUT_PULLUP.

Feature Value
Switch KCD1 rocker, SPST, maintained (latching)
Positions I = ON, O = OFF
Supply voltage 3.3V to 5V
Output in the I position HIGH, equal to VCC
Output in the O position LOW, 0V
Pull-down resistor 10 kΩ, on the board
Header 3-pin male, bent 90°, 2.54 mm spacing
PCB roughly 24 x 24 mm, 4 mounting holes
Outside size, header included about 24 x 29 x 22 mm
In one pack 5 modules

The switch body also shows "6A 250VAC / 10A 125VAC". That is the rating of the bare switch alone. On this module, the switch only carries a tiny logic signal for a GPIO pin. Never connect mains, a lamp or a motor to the module header.

One more detail matters for the second sketch. When you flip the rocker, the metal contacts bounce for a few milliseconds. For that short moment the pin can read ON, OFF, ON, OFF before it settles.

ESP32 Uno Pinout

The SIG wire ends in the hole marked D6, yet both sketches say 27. Look at the diagram: that hole is wired to GPIO27 of the ESP32 chip, and the ESP32 core counts pins by their GPIO number.

ESP32 Uno pinout diagram

How to Connect

The module header is male and the board headers are female, so three female-to-male jumper wires are all you need. There is no breadboard and no extra resistor.

The wiring diagram between ESP32 Uno rocker switch module

This image is created using Fritzing. Click to enlarge image

Rocker Switch Module Pin ESP32 Uno Pin
VCC 3.3V
SIG D6 (GPIO27)
GND GND

Module wires and thick wires often do not sit well in the female headers. In this case, use a Proto Screw Terminal Block Shield or a Screw Shield V2. It plugs into the ESP32 Uno and gives each pin a screw terminal. Below is the wiring diagram with the Screw Shield V2.

The wiring diagram between ESP32 Uno rocker switch module Screw Shield

This image is created using Fritzing. Click to enlarge image

WARNING

VCC belongs on the 3.3V pin. In the I position the switch joins SIG to VCC, so SIG carries the VCC voltage. A VCC wire on 5V or VIN would send 5V into GPIO27, and ESP32 pins take 3.3V at most. 3.3V is inside the 3.3V to 5V range of the module, so it works just as well.

Good to know: pinMode() uses INPUT, not INPUT_PULLUP. The resistor on the module already pulls SIG down, and a pull-up inside the chip would only pull the other way.

Why D6 and not D7? While the ESP32 starts up, GPIO14 (the D7 hole) sends out a short signal of its own. A rocker can easily be left ON when you power the board, and then it holds its pin at 3.3V the whole time. GPIO27 has no job during boot, so D6 is the safer hole for this switch.

ESP32 Uno Code - Read the Rocker as ON or OFF

This first sketch is a simple check of the wiring. Every 500 ms it reads GPIO27 and prints the state as a word, plus the raw value of SIG.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-rocker-switch-module */ /* * ESP32 Uno sketch: read the DIYables ON/OFF Rocker Switch Module * and print its state to the Serial Monitor twice a second. * Rocker on "I" = ON -> SIG is HIGH (1) * Rocker on "O" = OFF -> SIG is LOW (0) * The pull-down resistor is on the module, so no resistor is needed. */ #define ROCKER_SWITCH_PIN 27 // D6 (GPIO27) is wired to the SIG pin of the module void setup() { Serial.begin(9600); pinMode(ROCKER_SWITCH_PIN, INPUT); // plain INPUT: the module already holds SIG LOW when OFF } void loop() { int sig = digitalRead(ROCKER_SWITCH_PIN); // 1 = ON, 0 = OFF if (sig == HIGH) Serial.print("Rocker: ON (SIG = "); else Serial.print("Rocker: OFF (SIG = "); Serial.print(sig); Serial.println(")"); delay(500); // two lines per second are easy to follow }

Upload and Test

  1. Never used this board before? Follow ESP32 Uno - Getting Started to add the ESP32 core to Arduino IDE.
  2. Make the three connections: VCC to 3.3V, SIG to D6, GND to GND.
  3. Connect the board to the computer with the USB Type-C cable.
  4. Set up Arduino IDE: choose ESP32 Dev Module as the board and pick the COM port of the CH340 chip.
  5. Paste the sketch into a new sketch window.
  6. Press Upload and wait for the "Done uploading" message.
  7. Open the Serial Monitor and choose 9600 baud.
  8. Press the I side, wait a second, then press the O side. You see something like this:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Rocker: OFF (SIG = 0) Rocker: OFF (SIG = 0) Rocker: ON (SIG = 1) Rocker: ON (SIG = 1) Rocker: ON (SIG = 1) Rocker: OFF (SIG = 0) Rocker: OFF (SIG = 0)
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Pro tip: flip the rocker to I and back to O very fast. Sometimes no ON line shows up, because the sketch only looks every 500 ms. To catch every flip, the sketch must watch for changes, as the next one does.

A polling sketch like this is fine for a quick look. But it prints the same line again and again, and it cannot tell you when the switch moved. The stopwatch below fixes both.

ESP32 Uno Code - ON-Time Stopwatch

How long did you practise the piano? How long did the test run on your workbench? Flip the rocker to I when you start, flip it to O when you stop, and the board measures the time for you:

  • When the switch goes from OFF to ON, the sketch saves millis(), the time since start-up in milliseconds, and prints "Switched ON".
  • When the switch goes back to OFF, it subtracts the saved time and prints the result in seconds, with one decimal.

Bounce is handled with millis(), not with delay(). A new level only counts after it has stayed the same for 50 ms. The ON flip and the OFF flip both get this same 50 ms wait, so the two waits cancel out and the measured time stays right.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-rocker-switch-module */ /* * ESP32 Uno sketch: ON-time stopwatch with the DIYables ON/OFF Rocker Switch Module. * Flip the rocker to "I" (ON): the sketch prints "Switched ON" and starts timing. * Flip it to "O" (OFF): the sketch prints how long it stayed ON, e.g. "Was ON for 8.6 s". * A new level counts only after it stays the same for 50 ms (debounce with millis()). */ #define ROCKER_SWITCH_PIN 27 // D6 (GPIO27) is wired to SIG #define DEBOUNCE_TIME 50 // ms a new level must stay the same before it counts int stable_state; // the accepted level: HIGH = ON, LOW = OFF int last_reading; // the raw level seen in the last pass of loop() unsigned long last_change_ms; // when the raw level changed the last time unsigned long on_start_ms; // when the switch was turned ON void setup() { Serial.begin(9600); pinMode(ROCKER_SWITCH_PIN, INPUT); // the module has its own pull-down stable_state = digitalRead(ROCKER_SWITCH_PIN); // where is the rocker at power-up? last_reading = stable_state; last_change_ms = millis(); Serial.println("ON-time stopwatch ready"); if (stable_state == HIGH) { // already ON: start timing from now on_start_ms = millis(); Serial.println("Already ON at power-up, timing starts now"); } } void loop() { int reading = digitalRead(ROCKER_SWITCH_PIN); if (reading != last_reading) { // the raw level moved: a real flip or a bounce last_reading = reading; last_change_ms = millis(); // start counting the stable time again } // accept the new level only after it has stayed the same for DEBOUNCE_TIME if (reading != stable_state && millis() - last_change_ms >= DEBOUNCE_TIME) { stable_state = reading; if (stable_state == HIGH) { // OFF -> ON on_start_ms = millis(); Serial.println("Switched ON"); } else { // ON -> OFF unsigned long on_ms = millis() - on_start_ms; Serial.print("Was ON for "); Serial.print(on_ms / 1000.0, 1); // seconds with one decimal Serial.println(" s"); } } // other code can run here: loop() never waits }

Upload and Test

  1. Leave the wiring as it is.
  2. Replace the first sketch with this one and upload it.
  3. Keep the Serial Monitor at 9600 baud, then tap the EN (reset) button so the start line appears.
  4. Flip the rocker ON and OFF a few times, with short and long pauses. A run can look like this:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
ON-time stopwatch ready Switched ON Was ON for 8.6 s Switched ON Was ON for 31.7 s Switched ON Was ON for 2.1 s
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Reset the board with the rocker on I. The sketch sees that the switch is already ON and starts timing at once:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
ON-time stopwatch ready Already ON at power-up, timing starts now Was ON for 16.2 s
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Pro tip: start the stopwatch app on your phone at the same moment as you flip the rocker. The two times should agree within a few tenths of a second. The rest is your thumb.

Video Tutorial

Code Explanation

Most lines of the stopwatch are about two things: a clean reading, and two moments in time. This table goes through them.

Code Part What It Does
#define ROCKER_SWITCH_PIN 27 The code uses GPIO27, which is the D6 hole where SIG is wired
pinMode(ROCKER_SWITCH_PIN, INPUT) No resistor inside the chip is switched on, since the module's own pull-down gives 0 in the O position
digitalRead(ROCKER_SWITCH_PIN) Returns 1 on the I side and 0 on the O side
last_reading and last_change_ms Remember the newest raw level and the moment it appeared
millis() - last_change_ms >= DEBOUNCE_TIME True only when the level has been steady for 50 ms
stable_state The accepted state, a change of it is a real flip
on_start_ms = millis() Saves the moment the switch went ON
millis() - on_start_ms The ON time in milliseconds, worked out at the OFF flip
on_ms / 1000.0 Turns milliseconds into seconds, and print(..., 1) keeps one decimal

Every time value is an unsigned long. This is the type that millis() returns, and it keeps the subtraction right.

Rocker Module, Bare Rocker Switch or Button Module?

All three give the ESP32 Uno a simple HIGH or LOW, yet each one fits a different job.

Item Bare ON/OFF Rocker Switch Rocker Switch Module Button Module
Stays where you put it Yes Yes No, springs back
Pins 2 legs, no labels SIG, VCC, GND VCC, OUT, GND
Resistor INPUT_PULLUP in the code Pull-down on the board Pull-down on the board
Reading when ON or pressed LOW (inverted) HIGH HIGH
Wires to the ESP32 Uno 2 3 3
What the code watches A state A state A press

The bare switch has its own page, ESP32 Uno - ON/OFF Switch. It uses the pull-up inside the chip, so ON reads as LOW there. The push-button cousin of this module is on ESP32 Uno - Button Module.

Send the ON Time to Your Phone

The ESP32 Uno has Bluetooth built in, so the USB cable can stay at home. Send the "Was ON for ..." line to the DIYables Bluetooth app and read it on your phone. The stopwatch never blocks loop(), so it runs well next to Bluetooth code.

Try These Next

  • Total time: add each ON time to a second variable and print the sum, like a usage meter.
  • Keep the total after a reset: save it in flash with the Preferences library, which comes with the ESP32 core. Save only at the OFF flip, not in every pass of loop().
  • Reminder: print a warning once the switch has been ON for more than 30 minutes.
  • Drive a real load: let the ESP32 Uno switch a lamp or a pump through a relay while the rocker is ON. See ESP32 Uno - Relay. The module itself still carries only the signal.

Troubleshooting

Problem Possible Cause Solution
The port list in Arduino IDE is empty No CH340 driver, or the cable only charges Install the CH340 driver and swap to a data cable
Upload stops with "Failed to connect" The ESP32 stayed in normal run mode Press and hold BOOT during "Connecting...", release it when the upload starts
Always OFF, also on the I side VCC wire loose, or SIG not on D6 Check VCC on 3.3V and SIG on D6
Always OFF with good wiring The code uses 6 instead of 27 Write 27, because D6 is GPIO27
Random 0 and 1 in one position SIG wire loose, and the pull-down is on the module, so GPIO27 floats Push the SIG wire in firmly, or use the screw shield
"Was ON for 0.0 s" right after "Switched ON" The contacts bounced for more than 50 ms Set DEBOUNCE_TIME to 80
The "stopwatch ready" line is missing It was printed before the Serial Monitor was open Tap EN to restart the sketch
A few garbled characters after a reset The ESP32 boot text uses 115200 baud Harmless, your own lines at 9600 baud come right after

FAQ

Is the rocker switch module the same as a button module?

No. A push button is ON only while your finger is on it. The rocker stays on I or O until you press the other side. So the code reads a state, not a press.

Why is ON HIGH here, but LOW on the bare rocker switch page?

The module has a pull-down resistor, and the switch connects SIG to VCC when ON, so ON is HIGH. The bare switch page uses the pull-up inside the chip and connects the pin to GND when ON, so ON is LOW there.

Can I connect VCC to 5V on the ESP32 Uno?

No. SIG follows VCC. Power the module from 5V and the I position puts 5V on GPIO27. ESP32 inputs are not made for more than 3.3V.

Can the module switch my desk lamp?

No. The "6A 250VAC" print is for the bare switch. The module and its header are made for a small logic signal. To control a lamp, let the board drive a relay.

Why debounce with millis() and not with delay()?

delay() stops the whole sketch. With millis(), loop() keeps running, so you can add Bluetooth, WiFi or a display later and nothing has to wait.

Does the 50 ms debounce make the time wrong?

No. Both the ON flip and the OFF flip are accepted 50 ms late. When the sketch subtracts the two moments, the two delays cancel out.

What happens when millis() rolls over?

millis() goes back to 0 after about 49.7 days. The sketch keeps all times in unsigned long and subtracts them, so an ON time across the roll-over is still right, as long as one ON period is shorter than 49.7 days.

Does the ON time survive a reset?

No. The times live in RAM, and millis() starts at 0 again after a reset. Use the Preferences library to keep a total in flash, as shown in Try These Next.

Which other pins can read SIG?

D2 to D5, D9 and D11 to D13 work well. The analog holes A2 to A5 (GPIO35, GPIO34, GPIO36, GPIO39) can only read, and that is all this module needs, since it brings its own pull-down. Leave D0 and D1 free for the USB link, and skip the boot strapping pins D8 (GPIO12), D10 (GPIO5) and A0 (GPIO2). Their level at power-up decides how the chip starts, so a switch on one of them can stop the board from starting or from taking an upload. Keep away from D7 as well, as explained under "Why D6 and not D7?" above.

I used this module on an UNO R4. What changes?

On the UNO R4, SIG goes to pin 7 and VCC may go to 5V. On the ESP32 Uno, VCC must go to 3.3V, and SIG moves to D6. The code calls that pin 27, because the ESP32 core uses GPIO numbers. The rest of the sketch stays the same.

※ 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!