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:
- Learn what the I and O marks mean and what the SIG, VCC and GND pins do
- Run three jumper wires to the board, the power wire to 3.3V
- Print ON or OFF twice a second
- Turn the switch into an ON-time stopwatch with a 50 ms debounce

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) |
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?

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.

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.

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.

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.
Upload and Test
- Never used this board before? Follow ESP32 Uno - Getting Started to add the ESP32 core to Arduino IDE.
- Make the three connections: VCC to 3.3V, SIG to D6, GND to GND.
- Connect the board to the computer with the USB Type-C cable.
- Set up Arduino IDE: choose ESP32 Dev Module as the board and pick the COM port of the CH340 chip.
- Paste the sketch into a new sketch window.
- Press Upload and wait for the "Done uploading" message.
- Open the Serial Monitor and choose 9600 baud.
- Press the I side, wait a second, then press the O side. You see something like this:
- 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.
Upload and Test
- Leave the wiring as it is.
- Replace the first sketch with this one and upload it.
- Keep the Serial Monitor at 9600 baud, then tap the EN (reset) button so the start line appears.
- Flip the rocker ON and OFF a few times, with short and long pauses. A run can look like this:
- Reset the board with the rocker on I. The sketch sees that the switch is already ON and starts timing at once:
- 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.
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.