ESP32 S3 - Switch

An ON/OFF or toggle switch has two states: ON (closed) and OFF (open). Each time the switch is pressed, its state is toggled between ON and OFF and it remains in that state even when released. This tutorial will guide you on how to use an ON/OFF switch with an ESP32 S3 microcontroller.

ESP32 S3 ON/OFF Switch

It is important to note that the ON/OFF switch is distinct from the following and should not be confused with them:

Hardware Preparation

1×ESP32 S3 WROOM N16R8
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×Wires
1×ON/OFF Square Switch
1×Alternatively, ON/OFF Round Switch
1×Alternatively, On/Off Switch Module
1×Optionally, DC Power Jack
1×Breadboard
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 .

Overview of ON/OFF Switch

An ON/OFF Switch is a type of switch that, when pressed, changes its state between ON and OFF and then maintains that state even when released. To change the state again, the switch must be pressed a second time. This latching behavior makes the ON/OFF switch well suited for controlling devices that need to stay in a particular state without continuous user input.

Key Specifications

  • Switch Type: Latching toggle (maintains state after release)
  • Pins: Two-pin or three-pin configurations available
  • Operating Principle: Mechanically locks in ON or OFF position
  • Common Package: Square or round panel-mount form factors

Pinout

The ON/OFF switch is available in two common configurations. There are generally two types of ON/OFF Switches, the two-pin switch and the three-pin switch. This tutorial will focus on using the two-pin switch, where the distinction between the two pins is not necessary.

ON/OFF Switch Pinout

How It Works

There are two methods for using an ON/OFF switch with the ESP32 S3. The table below illustrates the wiring and reading state on an ESP32 S3 for each of these two methods:

pin 1 pin 2 ESP32 S3 Input Pin's State
1 GND ESP32 S3 Input Pin (with pull-up) HIGH OFF, LOW ON
2 VCC ESP32 S3 Input Pin (with pull-down) HIGH ON, LOW OFF

Out of the two methods, the tutorial will focus on one, specifically the first method will be used as an example.

Wiring Diagram

The following diagrams show how to connect the ON/OFF switch to the ESP32 S3. Use whichever connection method best suits your project setup.

The wiring diagram between ESP32 S3 ON/OFF Switch

This image is created using Fritzing. Click to enlarge image

Safety Notes

For a stable and secure connection, it is recommended to use a soldering iron to solder the wires and ON/OFF Switch pins together, and then use heat shrink tubes for added safety.

ESP32 S3 Code - ON/OFF Switch

Similar to a button, an ON/OFF switch also requires debouncing (more information can be found at Why needs debounce for the button, ON/OFF switch?). Debouncing can make the code more complex. Fortunately, the ezButton library offers debouncing functionality and utilizes internal pull-up registers, making programming easier.

※ NOTE THAT:

Two common use cases for an ON/OFF switch are:

  • The first use case: If the switch is in the ON state, perform a certain action. If the input state is OFF, perform the opposite action.
  • The second use case: If the switch's state changes from ON to OFF (or OFF to ON), perform a specific action.
/* * 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-switch */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-switch */ #include <ezButton.h> ezButton mySwitch(1); // create ezButton object that attach to ESP32-S3 pin GPIO1 void setup() { Serial.begin(9600); mySwitch.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { mySwitch.loop(); // MUST call the loop() function first if (mySwitch.isPressed()) Serial.println("The switch: OFF -> ON"); if (mySwitch.isReleased()) Serial.println("The switch: ON -> OFF"); int state = mySwitch.getState(); if (state == HIGH) Serial.println("The switch: OFF"); else Serial.println("The switch: ON"); }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Follow the wiring diagram provided above to connect the ON/OFF switch to the ESP32 S3.
  3. Connect your ESP32 S3 to your PC using a USB cable.
  4. Open the Arduino IDE software.
  5. Install the ezButton library. Refer to the instructions
  6. Select the appropriate board and port in the Arduino IDE.
  7. Compile and upload the code to the ESP32 S3 board by clicking the Upload button in the Arduino IDE.
  8. Test the ON/OFF switch by turning it ON.
  9. Observe the output on the Serial Monitor in the Arduino IDE.
  10. Turn the switch OFF.
  11. Observe the output on the Serial Monitor in the Arduino IDE.
  12. Pro Tip: Try wiring the switch using method 2 (pull-down with VCC) and update the logic in the code to observe how the state readings differ.
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
2026-01-15 09:12:01 -> The switch: OFF 2026-01-15 09:12:02 -> The switch: OFF 2026-01-15 09:12:03 -> The switch: OFF 2026-01-15 09:12:04 -> The switch: OFF -> ON 2026-01-15 09:12:05 -> The switch: ON 2026-01-15 09:12:06 -> The switch: ON 2026-01-15 09:12:07 -> The switch: ON 2026-01-15 09:12:08 -> The switch: ON 2026-01-15 09:12:09 -> The switch: ON 2026-01-15 09:12:10 -> The switch: ON 2026-01-15 09:12:11 -> The switch: ON -> OFF 2026-01-15 09:12:12 -> The switch: OFF 2026-01-15 09:12:13 -> The switch: OFF 2026-01-15 09:12:14 -> The switch: OFF
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Applications

The ON/OFF switch is one of the most versatile input components for embedded projects, enabling reliable latching control across a wide range of real-world scenarios.

  1. Power Control: Toggle power to peripheral devices or subsystems without needing continuous button holds.
  2. Mode Selection: Switch between operating modes in a device, such as manual vs. automatic or day vs. night mode.
  3. Enable/Disable Features: Activate or deactivate optional functionality, such as enabling a buzzer alarm or disabling a sensor module.
  4. Panel Controls: Use in control panels or enclosures for durable, user-facing on/off interfaces for fans, lights, or motors.
  5. Safety Interlock: Serve as a hardware enable switch that must be toggled before a system can operate, adding a safety layer to high-power projects.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

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