ESP32 S3 UNO - Slide Switch

Toy cars, pocket torches and battery boxes often have a tiny lever on the side that you push left or right. That lever is a slide switch. Move it once and it stays put, so it is a good fit for a setting that should not change by itself, like "sound on" or "test mode". In this guide, the ESP32 S3 Uno watches such a switch on header pin D7 and tells you in the Serial Monitor which side the lever is on.

Your path through this page:

  1. Look inside the DIYables SS12D10 and see which pins join for each lever position
  2. Build a 3-wire circuit with no resistor, with pin 3 on 3.3V
  3. Upload a sketch that prints the start position and then every move, with a 50 ms debounce
  4. Remove one wire and let the internal pull-up of GPIO14 do the work instead
ESP32 S3 Uno with SS12D10 slide switch - print the ON/OFF handle position on D7

No library is needed. Both sketches use only pinMode() and digitalRead(), which come with the ESP32 core in Arduino IDE.

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×Mini Slide Switch SS12D10
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 Slide Switch

The DIYables Mini Slide Switch SS12D10 is a small switch with two positions. A short plastic handle sticks out of the top of the metal body, and three pins come out of the bottom in one straight row. It is a latching switch: the handle stays on the side where you leave it. A push button works another way, because it jumps back as soon as you lift your finger.

Inside the body, a small metal bridge moves with the handle. It always touches the middle pin, and it also touches the outer pin on the handle's side. The middle pin takes part in both positions, so it is called COM (common).

Mini slide switch SS12D10 pinout - outer pin 1, middle COM pin 2, outer pin 3 and both handle positions

No labels are printed on the switch. Count the pins from one end:

  • Pin 1: an outer pin. On this page it always goes to GND.
  • Pin 2 (COM): the middle pin, the common one. It goes to the input pin D7 (GPIO14).
  • Pin 3: the other outer pin. It goes to 3.3V in the 3-wire circuit and stays free in the 2-wire circuit.
Handle position Pins joined inside Pin left open
Toward pin 1 pin 1 and pin 2 (COM) pin 3
Toward pin 3 pin 2 (COM) and pin 3 pin 1
Mini Slide Switch SS12D10
Contact type SPDT (1P2T), 2 positions
Pins 3 in one row, 4.7mm pitch
Terminal length 6.9mm
Handle length 5mm
Body size about 12.8 x 6.7 x 18.2mm
Mounting vertical, through-hole
Contacts silver-plated brass
Contact rating AC 125V 2A
Life more than 100,000 switching cycles
Working temperature -25°C to 80°C

The 125V 2A rating tells you how much the contacts can carry. In this project the switch only passes a tiny signal current into GPIO14, so it has a very easy job.

ESP32 S3 Uno Pinout

Here is the pinout of the ESP32 S3 Uno form board. This project needs three spots: the 3.3V pin and a GND pin on the power header, and D7 on the digital header. The code does not write 7 for D7. It uses the chip's own number for that pin, which is GPIO14.

ESP32 S3 Uno pinout diagram

Wiring Diagram

Three jumper wires are all this circuit needs. Pin 1 sits on GND and pin 3 sits on 3.3V, so COM, and with it D7, is always tied to one of them. The input never floats, and you do not need a pull-up or pull-down resistor.

The wiring diagram between ESP32 S3 Uno slide switch

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin ESP32 S3 Uno Pin
Pin 1 GND
Pin 2 (COM) D7 (GPIO14)
Pin 3 3.3V

Jumper wires can come loose when you move the project. A Screw Shield V2 or a Proto Screw Terminal Block Shield solves this. Plug it into the ESP32 S3 Uno and screw the wires in. Below is the wiring diagram with the Screw Shield V2.

The wiring diagram between ESP32 S3 Uno slide switch Screw Shield

This image is created using Fritzing. Click to enlarge image

WARNING

Pin 3 belongs on the 3.3V pin. Never connect it to 5V or VIN. When the handle points to pin 3, COM passes that voltage straight into GPIO14. The ESP32 S3 Uno GPIOs are 3.3V only and are not 5V tolerant, so 5V there can damage the pin or even the chip.

※ NOTE THAT:

Keep D7 as an INPUT in your code. Never set it to OUTPUT. The switch ties the pin hard to 3.3V or to GND, and an output pin that drives the other level would push a large current through the switch.

Why D7? On this board GPIO14 is a plain I/O pin with no boot strapping job. The handle can be in either position while you reset the board or upload a sketch.

How To Program For Slide Switch

The switch already pushes D7 firmly to 3.3V or GND, so the sketch has little to do: read one pin and decide what the level means.

  1. Give the pin a name. The ESP32 core counts in GPIO numbers, so header D7 is written as 14:
#define SLIDE_SWITCH_PIN 14
  1. In setup(), make it a normal input. No internal pull-up is needed, because the switch drives the pin by itself:
pinMode(SLIDE_SWITCH_PIN, INPUT);
  1. Read the level and turn it into ON or OFF. HIGH means COM touches pin 3, the 3.3V side:
if (digitalRead(SLIDE_SWITCH_PIN) == HIGH) { // handle toward pin 3: ON } else { // handle toward pin 1: OFF }
  1. Filter the bounce. The metal bridge rattles for a few milliseconds while it slides. When a reading differs from the last one, wait 50 ms and read again. Only a change that is still there counts as a real move.

ESP32 S3 Uno Code - Slide Switch (3 Wires)

This sketch puts the four steps together. Right after a reset, it prints where the handle is. After that it stays quiet, and it only writes a new line when you really move the handle.

/* * 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-slide-switch */ #define SLIDE_SWITCH_PIN 14 // ESP32 S3 Uno pin D7 (GPIO14) connected to pin 2 (COM) of the slide switch #define BOUNCE_TIME 50 // time in milliseconds for the metal contacts to stop bouncing int last_level; // the last confirmed level of the switch pin void printPosition(int level) { if (level == HIGH) // COM is joined to pin 3 (3.3V): handle toward pin 3 Serial.println("The slide switch is ON"); else // COM is joined to pin 1 (GND): handle toward pin 1 Serial.println("The slide switch is OFF"); } void setup() { Serial.begin(9600); pinMode(SLIDE_SWITCH_PIN, INPUT); // plain input: the switch itself drives the pin to 3.3V or GND Serial.println("Reading the slide switch on D7 (GPIO14)"); last_level = digitalRead(SLIDE_SWITCH_PIN); // where is the handle right now? printPosition(last_level); } void loop() { int reading = digitalRead(SLIDE_SWITCH_PIN); if (reading != last_level) { // the handle seems to have moved delay(BOUNCE_TIME); // wait until the contacts settle reading = digitalRead(SLIDE_SWITCH_PIN); // read the pin a second time if (reading != last_level) { // still different, so it is a real move last_level = reading; // remember the new position printPosition(last_level); } } }

Detailed Instructions

  1. Board not set up yet? Add the ESP32 core to Arduino IDE with ESP32 S3 Uno - Getting Started.
  2. Wire the switch as the table shows: pin 1 to GND, the middle pin to D7, pin 3 to 3.3V.
  3. Connect the ESP32 S3 Uno to your computer with a USB Type-C cable.
  4. Choose the board ESP32S3 Dev Module and its COM port in Arduino IDE.
  5. Copy the sketch, paste it into Arduino IDE and press Upload.
Arduino IDE - How to Upload Code
  1. Open the Serial Monitor at 9600 baud. Press the RESET button on the board once, so you can see the start lines.
  2. Move the handle from side to side a few times:
∞
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
Reading the slide switch on D7 (GPIO14) The slide switch is OFF The slide switch is ON The slide switch is OFF The slide switch is ON
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Check: one move gives one line. When you leave the handle alone, no new lines appear.

ESP32 S3 Uno Code - Slide Switch with INPUT_PULLUP (2 Wires)

Want fewer wires, with no 3.3V wire at all? Just remove the pin 3 wire. GPIO14 has a pull-up resistor inside the chip, and INPUT_PULLUP turns it on. It keeps D7 HIGH whenever COM is not joined to GND.

The wiring diagram between ESP32 S3 Uno slide switch INPUT_PULLUP

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin ESP32 S3 Uno Pin
Pin 1 GND
Pin 2 (COM) D7 (GPIO14)
Pin 3 not connected

With this wiring, the meaning of the levels turns around:

  • Handle toward pin 1: COM is joined to GND, so D7 reads LOW. The sketch calls this ON.
  • Handle toward pin 3: COM touches the empty pin, the pull-up wins, and D7 reads HIGH. The sketch calls this OFF.
/* * 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-slide-switch */ #define SLIDE_SWITCH_PIN 14 // ESP32 S3 Uno pin D7 (GPIO14) connected to pin 2 (COM) of the slide switch (pin 1 to GND, pin 3 free) #define BOUNCE_TIME 50 // time in milliseconds for the metal contacts to stop bouncing int last_level; // the last confirmed level of the switch pin // With INPUT_PULLUP the logic is flipped: // - handle toward pin 1: COM is joined to GND, so the pin reads LOW -> ON // - handle toward pin 3: COM touches the free pin, the internal pull-up keeps the pin HIGH -> OFF void printPosition(int level) { if (level == LOW) Serial.println("The slide switch is ON"); else Serial.println("The slide switch is OFF"); } void setup() { Serial.begin(9600); pinMode(SLIDE_SWITCH_PIN, INPUT_PULLUP); // turn on the internal pull-up resistor of GPIO14 Serial.println("Reading the slide switch on D7 (GPIO14), internal pull-up on"); last_level = digitalRead(SLIDE_SWITCH_PIN); // where is the handle right now? printPosition(last_level); } void loop() { int reading = digitalRead(SLIDE_SWITCH_PIN); if (reading != last_level) { // the handle seems to have moved delay(BOUNCE_TIME); // wait until the contacts settle reading = digitalRead(SLIDE_SWITCH_PIN); // read the pin a second time if (reading != last_level) { // still different, so it is a real move last_level = reading; // remember the new position printPosition(last_level); } } }

Detailed Instructions

  1. Pull out the wire on pin 3. The GND and D7 wires stay where they are.
  2. Upload this sketch the same way as the first one.
  3. Open the Serial Monitor at 9600 baud, press RESET, then slide the handle back and forth:
∞
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
Reading the slide switch on D7 (GPIO14), internal pull-up on The slide switch is ON The slide switch is OFF The slide switch is ON The slide switch is OFF The slide switch is ON
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Notice: the handle side that meant OFF in the first sketch now means ON. This is normal for the pull-up logic. If you want the old meaning back, swap the two words in printPosition().

Video Tutorial

Additional Knowledge

Slide switch, push button or ON/OFF switch?

The ESP32 S3 Uno folder has tutorials for all three. They are all simple switches, but they suit different jobs:

Slide switch (this page) Push button ON/OFF switch
Stays where you leave it yes no, it springs back yes
Pins 3 (pin 1, COM, pin 3) 4 legs in two joined pairs 2
Can work with no resistor and no pull-up yes, with 3 wires no no
Best for choosing a mode or a setting a short action, like "start" a big, easy-to-reach ON/OFF control

You can read more in ESP32 S3 Uno - Button and ESP32 S3 Uno - Switch. Need several settings in one small part? A DIP switch is a row of tiny slide switches, see ESP32 S3 Uno - DIP Switch.

Ideas for the slide switch

  1. Mute switch: stop all beeps while the handle is OFF, together with the ESP32 S3 Uno - Piezo Buzzer tutorial.
  2. Effect selector: blink an LED in one position and fade it in the other, with code from ESP32 S3 Uno - Fade LED.
  3. Test mode: print extra debug lines to the Serial Monitor only while the handle is ON.
  4. Unit selector: show a temperature in °C on one side and in °F on the other.

Function References

FAQ

Why must pin 3 go to 3.3V on the ESP32 S3 Uno?

When the handle points to pin 3, COM, and so GPIO14, gets the same voltage as pin 3. The ESP32 S3 Uno pins accept 3.3V at most. With 5V on pin 3, the pin could be damaged. 3.3V gives a clean HIGH reading.

Do I need a resistor for the slide switch?

No. In the 3-wire circuit, the switch connects D7 to 3.3V or to GND at all times, so the pin never floats. In the 2-wire circuit, the internal pull-up of GPIO14 takes the place of the 3.3V wire.

Should I use the 3-wire or the 2-wire version?

Both read the switch well. The 3-wire version reads the natural way: HIGH is ON. The 2-wire version saves a wire and the 3.3V pin, but LOW means ON. Choose the one that fits your project, and use the sketch that matches the wiring.

Why does the code use 14 and not 7?

In the ESP32 core for Arduino IDE, the pin number in code is the GPIO number of the chip. Header pin D7 of the ESP32 S3 Uno is GPIO14, so the sketch writes #define SLIDE_SWITCH_PIN 14.

My ON and OFF are the wrong way round. What is wrong?

Nothing is broken. The switch has no labels, so either outer pin can be "pin 1". Swap the GND and 3.3V wires on the two outer pins, or swap the words "ON" and "OFF" in the code.

Can the slide switch turn a mains lamp on and off?

Its contacts are rated AC 125V 2A, but this tutorial uses it only as a signal switch for the ESP32 S3 Uno. Never bring mains voltage near the board or the jumper wires. To control a big load, let the board read the switch and drive a relay module.

Can I add more slide switches?

Yes. Give each switch its own input pin and write the GPIO number of that pin in the code (see the pinout image). D4 or D5 work well. Do not use D0 and D1, because they carry the USB serial data. Also avoid D6 (GPIO3) and D9 (GPIO46): they are boot strapping pins, and a switch that holds them HIGH or LOW during reset can stop the board from starting. More inputs wait in the two extra rows of holes after you solder headers: GPIO35 to GPIO42, GPIO15 and GPIO16 are good choices. GPIO0 and GPIO45 are strapping pins too, so leave them free. Skip GPIO47 and GPIO48 as well: they are output only, and on some modules (with "V" in the name, like R8V or R16V) they run at 1.8V instead of 3.3V.

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE missing USB driver or a power-only cable Install the CP210x or CH340 driver, try a cable that carries data, or hold BOOT and press RESET
Always OFF with the 3-wire sketch pin 3 is not on 3.3V, or the code uses 7 Connect pin 3 to 3.3V and use 14 for D7
ON and OFF are swapped the outer pins are the other way round Swap the GND and 3.3V wires, or swap the words in the code
Random ON/OFF lines 3-wire sketch with only 2 wires, or a loose COM wire Use the INPUT_PULLUP sketch when pin 3 is free, and check the D7 wire
Two lines for one move contact bounce Raise BOUNCE_TIME from 50 to 80
No start lines in the Serial Monitor the sketch started before the Monitor was open Press the RESET button on the board
Garbled text in the Serial Monitor wrong baud rate Pick 9600 baud in 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!