ESP32 S3 UNO - Rocker Switch Module

The black rocker with an "I" and an "O" on it is the switch you know from power strips and desk lamps. DIYables puts that same rocker on a small blue board with three labeled pins, so the ESP32 S3 Uno can read it like any other digital input. Press one side of the rocker and it stays down until someone presses the other side. In the second sketch of this page, the board keeps a tally: each time the rocker goes to "I", a counter goes up by one. Now you can see how often a machine, a test rig or a game box was switched on since the last reset.

The plan for this page:

  1. Meet the module: the rocker, its "I" and "O" marks, and the SIG, VCC and GND pins
  2. Join it to the ESP32 S3 Uno with three jumper wires, with VCC on 3.3V
  3. Print the rocker position from D7 (GPIO14) twice a second
  4. Turn the rocker into a flip counter with a debounce that uses millis()
ESP32 S3 Uno rocker switch module

You do not have to install anything extra in Arduino IDE. The two sketches only call pinMode(), digitalRead() and millis().

Hardware Preparation

1×ESP32 S3 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
1×Recommended: Screw Terminal Block Shield for ESP32 S3 Uno
1×Recommended: Sensors/Servo Expansion Shield for ESP32 S3 Uno
1×Recommended: Breadboard Shield for ESP32 S3 Uno
1×Recommended: Enclosure for ESP32 S3 Uno
1×Recommended: Power Splitter for ESP32 S3 Uno
1×Recommended: Prototyping Base Plate & Breadboard Kit for ESP32 S3 Uno

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

A rocker switch has a small seesaw on top. Push the "I" side down and the switch is ON. Push the "O" side down and it is OFF. The DIYables module uses a black KCD1 rocker, the size you find on power strips, soldered on a blue board of about 24 x 24 mm with four mounting holes. Its 3-pin male header is bent at a right angle, so the wires leave the board to the side.

The rocker is a maintained (latching) switch. It does not spring back when you take your finger away. That is the big difference from a push button: a button only gives a short "pressed" moment, while the rocker gives a steady state that lasts until someone flips it again. So the code asks two questions: "is it ON or OFF right now?" and "did it just change?"

A 10 kΩ pull-down resistor (marked "103") sits on the board. While the rocker is on "O", it holds SIG at 0V. While the rocker is on "I", the switch joins SIG to VCC. This is why the print next to the rocker says "ON (H)" and "OFF (L)": ON gives a HIGH level and OFF gives a LOW level.

ON/OFF rocker switch module pinout with SIG, VCC and GND pins
image source: diyables.io

The pin names are printed on the back of the board, next to the header:

  • SIG: the output. It carries the VCC voltage (HIGH) while the rocker is on "I", and 0V (LOW) while it is on "O". Here it goes to D7 (GPIO14).
  • VCC: power for the module, 3.3V to 5V. On the ESP32 S3 Uno, give it 3.3V, because SIG copies this voltage.
  • GND: ground. Any GND pin of the board is fine.
ON/OFF Rocker Switch Module
Switch KCD1 rocker, SPST, maintained (latching)
Rocker marks "I" = ON, "O" = OFF
Board print ON (H), OFF (L)
Supply voltage (VCC) 3.3V to 5V
SIG when ON HIGH, the same voltage as VCC
SIG when OFF LOW (0V)
Resistor on board 10 kΩ pull-down on SIG (marked 103)
Header 3 pins, 2.54 mm pitch, right angle
Size with pins about 24 x 29 x 22 mm
Pack 5 modules

The side of the rocker says "6A 250VAC / 10A 125VAC". These numbers are for the bare rocker. On this module, the rocker only switches a tiny logic signal into a GPIO pin. Never send mains power, a motor current or any other load through the three header pins. If a big load must turn on, let the ESP32 S3 Uno read the rocker and drive a relay.

ESP32 S3 Uno Pinout

Before you wire, look for three pins in the picture. 3.3V and GND are on the power side of the board. D7 is on the digital side. The chip itself calls D7 GPIO14, and that is the number you will see in the code.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The module brings its own header and its own resistor, so there is no breadboard in this circuit. Three jumper wires go from the module straight to the headers of the ESP32 S3 Uno.

The wiring diagram between ESP32 S3 Uno rocker switch module

This image is created using Fritzing. Click to enlarge image

Rocker Switch Module Pin ESP32 S3 Uno Pin
SIG D7 (GPIO14)
VCC 3.3V
GND GND

If the project will run for a long time, or sit inside a box, it is better to fix the wires with screws. To do that, mount a Proto Screw Terminal Block Shield or a Screw Shield V2 on the ESP32 S3 Uno. Here is the same circuit wired through the Screw Shield V2:

The wiring diagram between ESP32 S3 Uno rocker switch module Screw Shield

This image is created using Fritzing. Click to enlarge image

WARNING

Keep VCC on the 3.3V pin. Never move it to 5V or VIN. While the rocker is on "I", whatever voltage is on VCC also appears on SIG, and so on GPIO14. This board's input pins accept 3.3V at most and are not 5V tolerant, so 5V can damage the pin. The module works just as well at 3.3V.

A rocker is often left ON while the board powers up or takes a new sketch. On D7 that is no problem: GPIO14 has no boot strapping job, so a HIGH level on it during reset does not change how the board starts.

How To Program For Rocker Switch Module

The module already gives a clean HIGH or LOW, so reading it takes only a few lines.

  1. Name the pin. In Arduino code for this board you write the GPIO number, not the header label, so D7 becomes 14:
#define ROCKER_SIG_PIN 14
  1. Set the pin as a plain input once, inside setup(). The pull-down on the module sets the OFF level, so leave out INPUT_PULLUP:
pinMode(ROCKER_SIG_PIN, INPUT);
  1. Read the position. HIGH means ON and LOW means OFF. Nothing is inverted:
bool rockerIsOn = (digitalRead(ROCKER_SIG_PIN) == HIGH);
  1. To count or log flips, look for changes, not for the level. Keep the last trusted level, and accept a new one only after the pin has stayed the same for about 50 ms. millis() measures that time without stopping the loop.

ESP32 S3 Uno Code - Read the Rocker Switch State

A switch that stays put does not need fast code. This sketch reads SIG every half second and prints the raw number and the rocker position. Contact bounce does not matter here: when the next reading comes, the contacts are quiet again.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-rocker-switch-module */ #define ROCKER_SIG_PIN 14 // ESP32 S3 Uno pin D7 (GPIO14) connected to the SIG pin of the rocker switch module void setup() { Serial.begin(9600); // The module has its own pull-down resistor on SIG, so a plain INPUT is enough. // Do not use INPUT_PULLUP: it would work against that resistor. pinMode(ROCKER_SIG_PIN, INPUT); } void loop() { int sig = digitalRead(ROCKER_SIG_PIN); // 1 = rocker on "I" (ON), 0 = rocker on "O" (OFF) Serial.print("SIG = "); Serial.print(sig); if (sig == HIGH) Serial.println(" | rocker: ON (I)"); else Serial.println(" | rocker: OFF (O)"); delay(500); // two readings per second are plenty for a switch that stays where you put it }

Detailed Instructions

  1. Arduino IDE not ready for this board yet? Start with ESP32 S3 Uno - Getting Started.
  2. Connect the three wires: SIG to D7, VCC to 3.3V, GND to GND. Look at the VCC wire once more: it must sit on 3.3V.
  3. Plug in the USB Type-C cable between the ESP32 S3 Uno and your computer.
  4. Select ESP32S3 Dev Module and the COM port of the board in Arduino IDE.
  5. Paste the code into a new sketch and click Upload.
Arduino IDE - How to Upload Code
  1. Open the Serial Monitor and pick 9600 baud.
  2. Press the "I" side, wait about two seconds, then press the "O" side:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
SIG = 0 | rocker: OFF (O) SIG = 0 | rocker: OFF (O) SIG = 1 | rocker: ON (I) SIG = 1 | rocker: ON (I) SIG = 1 | rocker: ON (I) SIG = 1 | rocker: ON (I) SIG = 0 | rocker: OFF (O) SIG = 0 | rocker: OFF (O)
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Look at the result: the value does not change while you leave the rocker alone. A push button would show 1 only while a finger is on it.

ESP32 S3 Uno Code - Count How Many Times the Switch Is Turned ON

Now the rocker becomes a power switch with a memory. Think of a machine in a tutorial: you want to know how many times it was switched on today. The sketch watches SIG and adds one to a counter each time the level goes from LOW to HIGH. When the rocker goes back to "O", it prints a short line that also tells you how long the switch was ON.

A counter needs a clean signal. One flip can bounce for a few milliseconds and look like three flips. So the sketch notes the time of every raw change with millis(), and it trusts a new level only after 50 ms without a change. The loop never waits, so you can add more work to it later.

The position at power-up is not counted. If the rocker is already on "I" when the board starts, the sketch says so but leaves the total at 0. Only a real OFF to ON flip counts.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-rocker-switch-module */ #define ROCKER_SIG_PIN 14 // ESP32 S3 Uno pin D7 (GPIO14) connected to the SIG pin of the rocker switch module #define QUIET_TIME 50 // a new level must stay the same for 50 ms before it is trusted (debounce) int trusted_level; // the last level that passed the debounce check int last_raw_level; // the level seen in the previous loop unsigned long last_change_ms = 0; // when the raw level changed the last time unsigned long on_since_ms = 0; // when the switch was turned ON the last time unsigned long on_count = 0; // how many times the switch was turned ON since reset void setup() { Serial.begin(9600); pinMode(ROCKER_SIG_PIN, INPUT); // plain input: the module has its own pull-down resistor trusted_level = digitalRead(ROCKER_SIG_PIN); // where is the rocker right now? last_raw_level = trusted_level; Serial.println("Rocker flip counter on D7 (GPIO14)"); if (trusted_level == HIGH) { Serial.println("Start position: ON (not counted)"); // it was not turned ON, it was already ON on_since_ms = millis(); } else { Serial.println("Start position: OFF"); } } void loop() { int raw_level = digitalRead(ROCKER_SIG_PIN); if (raw_level != last_raw_level) { // the pin moved: a real flip or a bounce last_change_ms = millis(); // start the quiet time again last_raw_level = raw_level; } // the pin has been quiet long enough and its level is not the trusted one: a real flip if (millis() - last_change_ms >= QUIET_TIME && raw_level != trusted_level) { trusted_level = raw_level; if (trusted_level == HIGH) { // OFF -> ON on_count++; on_since_ms = millis(); Serial.print("Turned ON - total: "); Serial.println(on_count); } else { // ON -> OFF float on_seconds = (millis() - on_since_ms) / 1000.0; Serial.print("Turned OFF after "); Serial.print(on_seconds, 1); // one digit after the point Serial.println(" s ON"); } } }

Detailed Instructions

  1. Keep the wiring from the first sketch.
  2. Upload this code the same way.
  3. Set the rocker to "O", open the Serial Monitor (9600 baud), then tap RESET on the board. Now the start lines show up.
  4. Flip the rocker to "I" and back a few times. Hold each position for a moment:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
Rocker flip counter on D7 (GPIO14) Start position: OFF Turned ON - total: 1 Turned OFF after 3.8 s ON Turned ON - total: 2 Turned OFF after 0.9 s ON Turned ON - total: 3 Turned OFF after 12.4 s ON
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Press RESET again: the total starts from 0. The counter lives in RAM, so a reset or a power cut clears it.

Video Tutorial

Additional Knowledge

Rocker switch module, bare ON/OFF switch or button module?

This board has pages for the two other parts too. ESP32 S3 Uno - Switch reads a bare 2-pin ON/OFF switch, and ESP32 S3 Uno - Button Module reads a push button on a 3-pin board like this one.

Rocker switch module (this page) Bare ON/OFF switch Button module
Stays where you leave it yes yes no, it springs back
Pins SIG, VCC, GND, labeled 2 pins, no labels VCC, OUT, GND, labeled
Resistor pull-down on the board internal pull-up of the pin pull-down on the board
pinMode INPUT INPUT_PULLUP INPUT
Level when ON or pressed HIGH LOW (inverted) HIGH
Wires to the board 3 2 3

The bare switch saves one wire. The module saves soldering, and its reading needs no translation: 1 means ON.

Ideas for the flip counter

  1. Machine log: count how many times a fan, a lamp or a 3D printer box is switched on during a day.
  2. Total run time: add up the seconds of every ON period to know how long a device really ran.
  3. Game rounds: let the rocker start and stop each round of a game, and count the rounds.
  4. Service reminder: print a message after 100 ON flips, as a reminder to check a part of the machine.

FAQ

Why does VCC go to 3.3V and not 5V on the ESP32 S3 Uno?

SIG is joined to VCC while the rocker is on "I". With 5V on VCC, GPIO14 would get 5V, and the ESP32 S3 Uno pins are made for 3.3V only. At 3.3V the module gives a clean HIGH and nothing is at risk.

The rocker was ON at start, but the total is 0. Is that a bug?

No. The sketch counts flips from OFF to ON. A rocker that is already on "I" at power-up was not turned on after the reset, so the sketch only reports the start position. Flip it to "O" and back to "I", and the total becomes 1.

Does the first sketch need a debounce?

No. It reads the pin only every 500 ms, and the bounce is over after a few milliseconds. The flip counter is different: it reacts to every change, so without the 50 ms quiet time one flip could add 2 or 3 to the total.

Can I use INPUT_PULLUP with this module?

It is not needed, and it is better to leave it out. The module already has a pull-down on SIG. An internal pull-up would pull the same wire the other way, and the OFF level could read badly. Use pinMode(pin, INPUT).

Can I put SIG on another pin?

Yes. D4 or D5 work well. Change ROCKER_SIG_PIN to the GPIO number of that pin (see the pinout image). Keep away from D0 and D1, which carry the USB serial data. Also keep away from D6 (GPIO3) and D9 (GPIO46). The chip checks those two pins at boot, so a rocker left ON could hold one of them HIGH during reset and stop the board from starting. With soldered headers, GPIO35 to GPIO42, GPIO15 and GPIO16 in the extra rows are good inputs too. GPIO47 and GPIO48 are output only, so they cannot read the rocker.

How do I keep the total after a power cut?

The counter is a normal variable in RAM, so it starts again from 0. To keep it, save the number to the flash memory of the ESP32-S3 after each ON flip, and read it back in setup().

Troubleshooting

Problem Possible Cause Solution
Arduino IDE shows no COM port no USB driver, or the cable only charges Add the CP210x or CH340 driver, change to a data cable, or keep BOOT held while you tap RESET
SIG stays 0 in both positions VCC wire missing, or the code uses 7 Connect VCC to 3.3V and write 14 for D7
SIG stays 1 in both positions the SIG wire sits on the 3.3V pin, not on D7 Move the SIG wire to D7 (GPIO14)
Total goes up by 2 for one flip contact bounce Set QUIET_TIME to 80 instead of 50
The "Start position" line is missing it was printed before you opened the Serial Monitor Tap RESET on the board once
Strange characters in the Serial Monitor wrong baud rate Choose 9600 in the baud menu of the Serial Monitor

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