ESP32 S3 UNO - Switch

This ESP32 S3 Uno switch tutorial shows you how to connect an ON/OFF switch to the ESP32 S3 Uno form board and read it with Arduino code. An ON/OFF switch is also called a toggle switch. You will print its state to the Serial Monitor and catch the moment it flips from OFF to ON or back.

In this tutorial, you will:

  1. Wire an ON/OFF toggle switch to the ESP32 S3 Uno.
  2. Read the switch state (ON or OFF) with the ezButton library.
  3. Detect switch events, like OFF to ON and ON to OFF.
  4. Debounce the switch so you get clean, stable readings.
ESP32 S3 Uno switch

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×Wires
1×ON/OFF Square Switch
1×Alternatively, ON/OFF Round Switch
1×Alternatively, On/Off Switch Module
1×Optionally, Heat Shrink Tubing
1×Optionally, Soldering Iron

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

Knowing how a switch behaves helps you write the right code for it. Unlike a push button, an ON/OFF switch keeps its position after you let it go.

When you flip it, it moves from OFF to ON, or from ON to OFF, and it stays there. To go back to the first state, you flip it again. This makes it a good choice for things like a power or mode selector in your project.

Pinout

Most ON/OFF switches come in two kinds: a version with two pins and a version with three pins.

This guide uses the two-pin version. Its two pins are the same, so you can connect them either way round.

ON/OFF Switch Pinout

How It Works

There are two ways to connect an ON/OFF switch to the ESP32 S3 Uno. Each way gives a different reading on the input pin, as the table shows.

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

You only need one of these two ways. This tutorial uses the first one: one pin goes to GND, and the other goes to an input pin with the internal pull-up resistor turned on.

※ NOTE THAT:

ESP32 S3 Uno pins work at 3.3V and are NOT 5V tolerant. If you use the second way, connect the switch to 3.3V, never to 5V.

ESP32 S3 Uno Pinout

The image below shows the pinout of the ESP32 S3 Uno form board. Use it to find the Uno header pins (D7, GND…) and their GPIO numbers when you wire the switch.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The switch needs only two wires: one to pin D7 and one to GND. The image and table below show the connections.

The wiring diagram between ESP32 S3 Uno ON/OFF Switch

This image is created using Fritzing. Click to enlarge image

ON/OFF Switch Pin ESP32 S3 Uno Pin
Pin 1 D7 (GPIO14)
Pin 2 GND

For a strong and safe joint, solder the wires to the switch pins with a Soldering Iron. Then slide a piece of Heat Shrink Tube over each joint to cover it.

ESP32 S3 Uno Code - ON/OFF Switch

A switch has the same problem as a button: its metal contacts bounce for a few milliseconds when you flip it, so the board can see many fast ON/OFF changes. You can learn more at Why do buttons and ON/OFF switches need debouncing?. Writing debounce code by hand takes extra work, so this sketch uses the ezButton library. It handles debouncing for you and turns on the internal pull-up resistor of the pin.

※ NOTE THAT:

Most switch projects fall into one of two cases:

  • Case 1: Do one thing while the switch is ON, and the opposite thing while it is OFF.
  • Case 2: Do something only at the moment the switch changes, from OFF to ON or from ON to OFF.

The sketch below covers both cases. It prints a message when the switch changes position, and it prints the current state on every loop.

/* * 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-switch */ #include <ezButton.h> ezButton toggleSwitch(14); // create ezButton object that attach to the ESP32 S3 Uno pin D7 (GPIO14); void setup() { Serial.begin(9600); toggleSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { toggleSwitch.loop(); // MUST call the loop() function first if (toggleSwitch.isPressed()) Serial.println("The switch: OFF -> ON"); if (toggleSwitch.isReleased()) Serial.println("The switch: ON -> OFF"); int state = toggleSwitch.getState(); if (state == HIGH) Serial.println("The switch: OFF"); else Serial.println("The switch: ON"); }

The switch is read on GPIO14, which is pin D7 on the Uno header. isPressed() becomes true when the switch goes from OFF to ON, and isReleased() becomes true when it goes back to OFF. Because the pull-up is on, getState() returns HIGH when the switch is OFF and LOW when it is ON.

Detailed Instructions

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram above.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Install the ezButton library. See the steps here.
  6. Copy the code above and paste it into Arduino IDE.
  7. Upload the code by clicking the Upload button.
  8. Open the Serial Monitor and set the baud rate to 9600.
  9. Flip the switch to ON and watch the output.
  10. Flip the switch to OFF and check the output again.
  11. Tip: If the state looks reversed, that is normal for the pull-up method: LOW means ON. You can change the text in the code to match how your switch is mounted.
∞
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
The switch: OFF The switch: OFF The switch: OFF The switch: OFF -> ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON -> OFF The switch: OFF The switch: OFF The switch: OFF
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

FAQ

Do I need an external resistor for the switch on the ESP32 S3 Uno?

No. The ezButton library turns on the internal pull-up resistor of the input pin. You only need two wires: one to D7 (GPIO14) and one to GND.

Can I connect the switch to 5V on the ESP32 S3 Uno?

No. ESP32 S3 Uno pins work at 3.3V and are not 5V tolerant. If you wire the switch to a supply instead of GND, use the 3.3V pin, and set the input to pull-down mode.

Why do I write 14 in the code instead of 7?

The ESP32 S3 Uno uses GPIO numbers in code, not the Uno header labels. Header pin D7 is GPIO14 on this board, so the code uses ezButton toggleSwitch(14);. Check the pinout image for other pins.

Is this code different from the UNO R4 version?

Only the pin number changes. The UNO R4 version uses pin 7, and the ESP32 S3 Uno version uses GPIO14 for the same D7 header position. The ezButton library works on both boards.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs: GPIO15 and GPIO16 (I/O, PWM, analog), GPIO45 and GPIO35 to GPIO42 (I/O, PWM), and GPIO47/GPIO48 (output only, PWM). Solder pin headers to use them and write the GPIO number in code. For extra switches, GPIO35 to GPIO42 or GPIO15/GPIO16 are good choices. Do not use GPIO47/GPIO48, because they are output only. Also note two warnings: (1) GPIO47/GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely; (2) GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Why does the Serial Monitor print the state so fast?

The sketch prints the state on every pass of loop(), with no delay. This is fine for testing. In a real project, act only on isPressed() and isReleased(), or print the state only when it changes.

Troubleshooting

Problem Possible Cause Solution
No COM port shows up in Arduino IDE USB driver missing or board not in upload mode Install the CP210x or CH340 driver, use a data USB cable, or hold BOOT while pressing RESET
State never changes Loose wire or wrong pin Check that one switch pin goes to D7 (GPIO14) and the other to GND
State is reversed (ON shows OFF) Pull-up logic means LOW is ON This is normal, or swap the text in the code
Many fast OFF/ON messages for one flip Contact bounce Keep setDebounceTime(50) or raise it to 100
Compile error about ezButton.h Library not installed Install the ezButton library from the Library Manager
Serial Monitor shows strange characters Wrong baud rate Set the Serial Monitor to 9600 baud
Board resets or acts oddly Switch wired to 5V Use GND (or 3.3V with pull-down), never 5V

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