ESP32 S3 - Rocker Switch Module

Some choices in a project are made once and then left alone for hours: test mode or normal mode, sound on or off, metric or imperial. A rocker switch suits this kind of choice well, because everyone can see its position from across the desk, and it never moves by itself. In this guide, the ESP32 S3 first reads the DIYables ON/OFF Rocker Switch Module, and then gives it a real job: deciding if the temperature of the ESP32-S3 chip is shown in Celsius or in Fahrenheit.

What you'll build in this tutorial:

  1. What the rocker switch module is, and why its SIG pin is HIGH when the rocker is on "I"
  2. How to connect SIG, VCC and GND to GPIO21, 3V3 and GND of the ESP32 S3
  3. A simple test sketch that shows ON or OFF about three times per second
  4. A second sketch where the rocker selects the unit of the chip temperature: OFF for °C, ON for °F
ESP32 S3 with an ON/OFF rocker switch module

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×Alternatively, ESP32 S3 SuperMini Dev Module
1×Alternatively, ESP32 S3 Uno-form Board
1×Alternatively, ESP32 S3 Camera 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 Expansion Board for ESP32 S3
1×Recommended: Breakout Expansion Board for ESP32 S3
1×Recommended: Power Splitter for ESP32 S3

Or you can buy the following kits:

1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
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: SIG, VCC and GND on a right-angle header
image source: diyables.io

You have seen this switch many times. It is the black rocker on power strips and on the back of many devices. DIYables mounts a KCD1 rocker of this kind on a small blue board with a mounting hole in each corner and a 3-pin right-angle header on one edge. One end of the rocker carries the mark "I", which means ON. The other end carries "O", which means OFF. Beside the rocker, the board says "ON (H)" and "OFF (L)": H stands for a HIGH level on the signal pin, L for a LOW level.

In switch language, this rocker is "maintained" (some shops say "latching"). Push one end down and it stays there until you push the other end. A push button behaves in another way: it is only active while your finger is on it. So there is no "press" for the code to catch. The ESP32 S3 looks at the current position, ON or OFF, and it can do something at the moment that position changes. Think of the rocker as a setting, not as a command.

Inside, the module is simple. The switch is a single SPST contact between VCC and SIG, and a 10 kΩ pull-down resistor (marked "103") is soldered on the board as well. With the rocker on "O", the resistor holds SIG at 0V. With the rocker on "I", the contact connects SIG to VCC. You add no resistor of your own, and nothing is inverted: ON reads HIGH, OFF reads LOW.

Key Specifications

Item Value
Switch KCD1 rocker, SPST, maintained (latching)
Marks on the rocker I = ON, O = OFF
Supply voltage (VCC) 3.3V to 5V (3.3V on the ESP32 S3)
SIG when ON HIGH, the same voltage as VCC
SIG when OFF LOW (0V)
Resistor on the board 10 kΩ pull-down (103)
Header 3 pins, 2.54 mm pitch, right-angle male
Board about 24 x 24 mm, 4 mounting holes
Size with the pins about 24 x 29 x 22 mm
Pack 5 modules

There is also "6A 250VAC / 10A 125VAC" molded into the side of the switch. Those numbers describe the switch as a loose part, not the module. Here the switch only passes a small logic signal to a GPIO pin, so never send mains power or a motor current through the three header pins.

Rocker Switch Module Pinout

Turn the board over to read the pin names; they sit right behind the header. Seen from the top, with the header pointing to the right, the order from top to bottom is:

  1. SIG — the output; HIGH (the VCC voltage) while the rocker is on "I", LOW (0V) while it is on "O" (GPIO21 in this guide)
  2. VCC — supply input, rated 3.3V to 5V; on the ESP32 S3 it always goes to the 3V3 pin
  3. GND — ground (0V), shared with the ESP32 S3

How does this compare with the parts on our other pages? The bare 2-leg switch in ESP32 S3 - Switch also stays in place, but it has no resistor, so it needs INPUT_PULLUP and reads LOW for ON, which feels upside down. The ESP32 S3 - Button Module has the same 3-pin shape and the same pull-down, but its key springs back. The rocker module takes the best part of both: it holds its position like the bare switch, and it gives a plain HIGH for ON like the button module.

Wiring Diagram

The resistor is already on the module, so three jumper wires make the whole circuit. Run SIG to GPIO21, VCC to 3V3, and GND to one of the GND pins of the ESP32 S3.

The wiring diagram between ESP32 S3 ON/OFF rocker switch module

This image is created using Fritzing. Click to enlarge image

Rocker Switch Module Pin ESP32 S3 Pin
SIG GPIO21
VCC 3V3
GND GND

Pushing jumper wires into the headers is quick, but the wires can slip out. The ESP32 S3 fits a Screw Terminal Block for ESP32 S3, which turns every pin into a screw terminal. See the diagram below for the screw terminal version.

The wiring diagram between ESP32 S3 ON/OFF rocker switch module Screw Terminal

This image is created using Fritzing. Click to enlarge image

Another choice is a GPIO Breakout Board for ESP32 S3. Each GPIO has a signal pin with 3.3V and GND pins next to it, as shown below:

The wiring diagram between ESP32 S3 ON/OFF rocker switch module GPIO Breakout Board

This image is created using Fritzing. Click to enlarge image

Safety Notes

Take VCC from the 3V3 pin, never from the 5V pin. When the rocker is ON, SIG simply carries whatever voltage VCC has, and the GPIO pins of the ESP32 S3 are made for 3.3V only, so a 5V supply would push 5V into GPIO21. At 3.3V the module works just as well. A rocker is also often left ON while you plug in the USB cable or upload a sketch. That is fine on GPIO21, because it is not one of the pins the chip checks at boot, so the board starts normally in both rocker positions.

How To Program For Rocker Switch Module

Reading the module needs one pin and two functions. In setup(), pinMode() makes GPIO21 a plain INPUT; INPUT_PULLUP stays out, because the module already brings its pull-down. In loop(), digitalRead() returns HIGH for ON and LOW for OFF. The first sketch only prints that value. The second sketch also reacts to changes, so it accepts a new rocker position only after the pin has stayed the same for 50 ms, and it measures that time with millis() instead of delay(). It reads the chip temperature with temperatureRead(), a function of the ESP32 Arduino core, so you install no library for either sketch.

ESP32 S3 Code - Read the Rocker Switch Position

This sketch checks your wiring. About three times per second it reads GPIO21 and prints the rocker position next to the raw value. Contact bounce does not matter here. The contacts settle within a few milliseconds, and the next reading comes 300 ms later.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-rocker-switch-module */ #define ROCKER_SIG_PIN 21 // ESP32 S3 GPIO21 connected to the SIG pin of the rocker switch module void setup() { Serial.begin(9600); // plain INPUT: the 10k pull-down on the module keeps SIG LOW while the rocker is OFF, // so INPUT_PULLUP is not needed here pinMode(ROCKER_SIG_PIN, INPUT); } void loop() { int sig = digitalRead(ROCKER_SIG_PIN); // HIGH = rocker on "I", LOW = rocker on "O" if (sig == HIGH) Serial.print("Rocker is ON "); else Serial.print("Rocker is OFF"); Serial.print(" (GPIO21 = "); Serial.print(sig); // the raw value: 1 or 0 Serial.println(")"); delay(300); // a switch that stays in place does not need fast reading }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Make the three connections from the table: SIG to GPIO21, VCC to 3V3, GND to GND. Make sure that VCC goes to 3V3 and not to 5V.
  3. Plug the ESP32 S3 into your computer with the Type-C cable.
  4. In the Arduino IDE, pick the ESP32 S3 board under Tools > Board and its port under Tools > Port.
  5. Copy the code into a new sketch and click Upload.
  6. Start the Serial Monitor with the baud rate at 9600.
  7. Push the "I" end of the rocker down, wait a second or two, then push the "O" end.
  8. Pro Tip: If the monitor always says OFF, the VCC wire is probably loose. If it always says ON, the wire on GPIO21 may come from VCC instead of SIG; check the labels on the back of the module.
∞
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 is OFF (GPIO21 = 0) Rocker is OFF (GPIO21 = 0) Rocker is OFF (GPIO21 = 0) Rocker is ON (GPIO21 = 1) Rocker is ON (GPIO21 = 1) Rocker is ON (GPIO21 = 1) Rocker is ON (GPIO21 = 1) Rocker is ON (GPIO21 = 1) Rocker is OFF (GPIO21 = 0) Rocker is OFF (GPIO21 = 0)
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Take your hand away from the rocker and the line does not change. That is the latching behavior at work. A button module would drop back to 0 the moment you let go.

ESP32 S3 Code - Rocker Switch Selects Celsius or Fahrenheit

Now the rocker gets a real job. The ESP32-S3 chip has a small temperature sensor inside, and temperatureRead() returns its value in degrees Celsius as a float. The sketch prints this value once per second. With the rocker on "O" (OFF), you see Celsius. With the rocker on "I" (ON), the same value is turned into Fahrenheit with °F = °C × 9 / 5 + 32. Each time you flip the rocker, one extra line names the new unit, so you know exactly when the change happened.

※ NOTE THAT:

This is the temperature of the chip itself, not of the air in the room. The chip warms up while it runs, so the value is usually far above room temperature, often between 35 and 50 °C. It is a fine number for this demo and for keeping an eye on the board. To measure the room, use a real sensor, for example the DS18B20 in ESP32 S3 - Temperature Sensor.

This time the code reacts to a change, so bounce matters: one flip could look like ON, OFF, ON within a few milliseconds and print three unit lines. To stop that, the sketch saves the time of every raw change on GPIO21 with millis(). It trusts the new position only after GPIO21 has stayed the same for 50 ms. Since there is no delay() in loop(), the once-per-second print keeps its pace while you play with the rocker.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-rocker-switch-module */ #define ROCKER_SIG_PIN 21 // ESP32 S3 GPIO21 connected to the SIG pin of the rocker switch module #define DEBOUNCE_TIME 50 // milliseconds: a new rocker position must stay this long to count #define PRINT_INTERVAL 1000 // milliseconds: print the chip temperature once per second int unitLevel; // trusted rocker position: LOW (OFF) = Celsius, HIGH (ON) = Fahrenheit int lastReading; // the last raw reading of SIG, used to spot contact bounce unsigned long lastChangeTime = 0; // when the raw reading changed for the last time unsigned long lastPrintTime = 0; // when the last temperature line was printed // print one line that tells which unit is in use now void printUnit() { if (unitLevel == HIGH) Serial.println("Rocker ON -> unit: Fahrenheit (°F)"); else Serial.println("Rocker OFF -> unit: Celsius (°C)"); } void setup() { Serial.begin(9600); pinMode(ROCKER_SIG_PIN, INPUT); // the module has its own pull-down, so no INPUT_PULLUP unitLevel = digitalRead(ROCKER_SIG_PIN); // start with the unit the rocker shows right now lastReading = unitLevel; printUnit(); } void loop() { // 1. Watch the rocker. This debounce uses millis(), so the loop never stops. int reading = digitalRead(ROCKER_SIG_PIN); if (reading != lastReading) { // SIG moved: a real flip or one bounce of the contacts lastReading = reading; lastChangeTime = millis(); // start the quiet time again } if (reading != unitLevel && millis() - lastChangeTime >= DEBOUNCE_TIME) { unitLevel = reading; // SIG stayed the same for 50 ms: the flip is real printUnit(); } // 2. Once per second, print the chip temperature in the unit the rocker selects if (millis() - lastPrintTime >= PRINT_INTERVAL) { lastPrintTime = millis(); float tempC = temperatureRead(); // temperature of the ESP32-S3 chip itself (not the air), in °C Serial.print("Chip temperature: "); if (unitLevel == HIGH) { float tempF = tempC * 9.0 / 5.0 + 32.0; // convert °C to °F Serial.print(tempF, 1); // 1 digit after the point Serial.println(" °F"); } else { Serial.print(tempC, 1); Serial.println(" °C"); } } }

Detailed Instructions

  1. Keep the wiring as it is.
  2. Upload this sketch the same way as the first one.
  3. Open the Serial Monitor at 9600 baud. The line with the start unit is printed only once, at power-up, so press the RESET button of the ESP32 S3 if you missed it.
  4. Wait a few seconds, flip the rocker to "I", wait again, then flip it back to "O".
  5. Pro Tip: Prefer ON for Celsius? Change unitLevel == HIGH to unitLevel == LOW in both places, and the rocker works the other way round.
∞
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 OFF -> unit: Celsius (°C) Chip temperature: 40.7 °C Chip temperature: 40.9 °C Chip temperature: 41.2 °C Rocker ON -> unit: Fahrenheit (°F) Chip temperature: 106.2 °F Chip temperature: 106.3 °F Chip temperature: 106.3 °F Chip temperature: 106.5 °F Rocker OFF -> unit: Celsius (°C) Chip temperature: 41.4 °C Chip temperature: 41.6 °C
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

In the first minutes after power-up the value creeps up slowly, while the chip warms to its working temperature. After that it stays almost flat.

Application and Project Ideas

A rocker fits any setting that should stay put and be easy to see at a glance.

  1. Metric or imperial display: Show a real air sensor on a small screen, and let the rocker pick °C or °F, hPa or inHg.
  2. Night mute: Silence a buzzer alarm by turning the rocker OFF, while the rest of the project keeps working.
  3. Service mode: With the rocker ON, the ESP32 S3 skips its normal task and prints debug data instead.
  4. Upload pause: Keep a WiFi data logger measuring, but stop sending to the cloud while the rocker is OFF.
  5. Chip heat watch: Print a warning when the chip temperature passes a limit you choose, and use the rocker to turn the warning on or off.

Challenge Yourself

  1. Beginner: Print the temperature every 3 seconds instead of every second, and put the seconds since start (millis() / 1000) at the front of each line.
  2. Intermediate: Remember the highest chip temperature since power-up, and print it in the selected unit after every 10 readings.
  3. Advanced: Show the chip temperature on a small web page over WiFi, and let the rocker choose the unit that the page displays.

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!