Arduino Nano ESP32 - Slide Switch

In this guide, we will learn how to use a mini slide switch with the Arduino Nano ESP32. Picture a small IoT gadget that should either join your home WiFi or open its own hotspot. That choice must not change by itself, and it must still be there after a power cut. A slide switch on the side of the case does this job: you set it once, and the Nano ESP32 reads it whenever it needs to. In detail, we will learn:

Arduino Nano ESP32 with SS12D10 slide switch - read the ON/OFF handle position on pin D2

Hardware Preparation

1×Arduino Nano ESP32
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×Breadboard
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano ESP32

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 SS12D10

Think of the SS12D10 as a tiny railway point. One track (the COM leg) comes in, and the handle decides which of two other tracks it leads to. The part is small enough to peek out of the side of a 3D-printed case, with a 5 mm handle and three legs in one row. Once you slide the handle, it does not spring back. It waits at that end until you move it again. That is called latching, and it is exactly what a push button does not do. The datasheet name for this kind of switch is SPDT (single pole, double throw), or 1P2T.

SS12D10 Slide Switch
Switch type SPDT (1P2T), 2 positions
Number of legs 3
Leg pitch 4.7 mm
Leg length 6.9 mm
Handle 5 mm long
Size of the body about 12.8 x 6.7 x 18.2 mm
Mounting style vertical, through-hole
Contact metal silver-plated brass
Rated load AC 125V 2A
Life span more than 100,000 switching cycles
Temperature range -25°C to 80°C

About the AC 125V 2A rating: you will not get close to it here. Reading a pin with the Nano ESP32 needs only a tiny current through the contacts.

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

The switch has no markings, so count the legs. The center one is COM. The legs at the two ends of the row are pin 1 and pin 3:

  • Pin 1 (one end of the row): connect to GND.
  • Pin 2, COM (center): connect to D2 of the Nano ESP32. Another free digital pin also works, but the code uses D2.
  • Pin 3 (other end of the row): connect to 3V3 of the Nano ESP32.

Under the handle sits a little metal bridge that travels with it. One end of the bridge rests on COM all the time. The other end lands on the outer leg that the handle points to:

  • Handle near pin 1 → COM connects to pin 1. Pin 3 is unused.
  • Handle near pin 3 → COM connects to pin 3. Pin 1 is unused.

Put GND on pin 1 and 3.3V on pin 3, and COM becomes a simple 0V-or-3.3V signal for D2. We call the 3.3V side (HIGH) "ON" and the GND side (LOW) "OFF". One small catch: metal contacts chatter for a few milliseconds after each move, so the sketch reads twice before it believes a change.

Wiring Diagram

For the main wiring, plug three jumper wires straight onto the switch legs and the Nano ESP32 headers. No resistor is used. D2 sees GND through pin 1 or 3.3V through pin 3, so it has a clear level in both positions, and plain INPUT mode is enough.

The wiring diagram between Arduino Nano ESP32 and slide switch

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin Arduino Nano ESP32 Pin
Pin 1 GND
Pin 2 (COM) D2
Pin 3 3V3 (not VBUS)

If the project will run for a long time, or sit inside a box, it is better to fix the wires with screws. The Arduino Nano ESP32 fits a Screw Terminal Expansion Board for Arduino Nano, which turns every pin into a screw terminal. See the diagram below for the screw terminal version.

The wiring diagram between Arduino Nano ESP32 and slide switch Screw Terminal

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Before you power up, check these two points:

  • The pin 3 wire belongs on 3V3. Do not move it to VBUS or VIN. With the handle near pin 3, COM hands that voltage directly to D2, and the GPIO pins of the ESP32-S3 chip accept 3.3V only. A 5V level can kill the pin.
  • Leave D2 in input mode. If a sketch ever makes it an OUTPUT, the switch would short that output to 3V3 or GND, and the pin can be damaged.

How To Program For Slide Switch

Reading the switch uses only built-in functions:

  • Pick a name for D2, the pin that listens to COM:
const int SLIDE_SWITCH_PIN = D2;
  • In setup(), make D2 a normal input. The internal pull-up stays off, since pin 1 and pin 3 already give both levels:
pinMode(SLIDE_SWITCH_PIN, INPUT);
  • Get the current level. You see HIGH when the handle is near pin 3 (ON) and LOW when it is near pin 1 (OFF):
int slideState = digitalRead(SLIDE_SWITCH_PIN);
  • Keep the previous level in a variable. If a new reading does not match it, pause 50 ms, read once more, and print the state only if the new level is still there.

Arduino Nano ESP32 Code - Slide Switch

The full sketch starts with one line that shows where the handle is. From then on, a new line appears only when you slide the handle to the other end.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-slide-switch */ const int SLIDE_SWITCH_PIN = D2; // Arduino Nano ESP32 pin connected to the COM pin (pin 2) of the slide switch const int DEBOUNCE_MS = 50; // time for the contacts to settle after the handle moves int lastState; // the last confirmed position (HIGH or LOW) void printState(int state) { if (state == HIGH) // COM is joined to pin 3 (3V3) Serial.println("ON - the handle is on the pin 3 side"); else // COM is joined to pin 1 (GND) Serial.println("OFF - the handle is on the pin 1 side"); } void setup() { Serial.begin(9600); // Plain INPUT: pin 1 is on GND and pin 3 is on 3V3, so COM always gives // a clear HIGH or LOW. No pull-up or pull-down resistor is needed. pinMode(SLIDE_SWITCH_PIN, INPUT); lastState = digitalRead(SLIDE_SWITCH_PIN); // where is the handle right now? Serial.print("Slide switch at start: "); printState(lastState); } void loop() { int reading = digitalRead(SLIDE_SWITCH_PIN); if (reading != lastState) { // The level changed. The contacts may still be bouncing. delay(DEBOUNCE_MS); // wait for them to settle reading = digitalRead(SLIDE_SWITCH_PIN); // read again after the wait if (reading != lastState) { // still different, so the handle really moved Serial.print("Slide switch moved: "); printState(reading); lastState = reading; // remember the new position } } }

Detailed Instructions

Follow these instructions step by step:

  • If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
  • Make the three connections from the table above. Check twice that pin 3 is on 3V3.
  • Plug the Nano ESP32 into a USB port of your PC with the Type-C cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Nano ESP32 board (e.g., Arduino Nano ESP32) and COM port.
  • Copy the code above into a new sketch, then click the Upload button.
  • Click the Serial Monitor icon and pick 9600 baud.
  • Push the handle back and forth about four times, pausing at each end for a second.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
Slide switch at start: ON - the handle is on the pin 3 side Slide switch moved: OFF - the handle is on the pin 1 side Slide switch moved: ON - the handle is on the pin 3 side Slide switch moved: OFF - the handle is on the pin 1 side Slide switch moved: ON - the handle is on the pin 3 side
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2

Four moves, four lines: the contact chatter never shows up as extra lines. Right after the upload, the Nano ESP32 resets and its USB port reconnects. If the "at start" line did not appear, the Serial Monitor simply missed it. Slide the handle and the next lines will show.

Arduino Nano ESP32 Code - Two-Wire Slide Switch with INPUT_PULLUP

Short on jumper wires, or is the 3V3 pin already busy with a sensor? The ESP32-S3 on this board has a pull-up resistor built into each GPIO, and INPUT_PULLUP switches it on. With it, the 3V3 wire is no longer needed. Just remove the pin 3 wire: COM stays on D2, pin 1 stays on GND, and pin 3 is left free.

The wiring diagram between Arduino Nano ESP32 and slide switch INPUT_PULLUP

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin Arduino Nano ESP32 Pin
Pin 1 GND
Pin 2 (COM) D2
Pin 3 not connected

Now the meaning of HIGH and LOW is reversed:

  • Handle near pin 1: the bridge ties COM, and so D2, to GND. D2 reads LOW, and the sketch calls that ON.
  • Handle near pin 3: COM touches only the free pin 3, so nothing drives D2. The pull-up lifts it to HIGH, and the sketch calls that OFF.
/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-slide-switch */ const int SLIDE_SWITCH_PIN = D2; // Arduino Nano ESP32 pin connected to the COM pin (pin 2) of the slide switch const int DEBOUNCE_MS = 50; // time for the contacts to settle after the handle moves int lastState; // the last confirmed position (HIGH or LOW) void printState(int state) { // In the 2-wire version the logic is flipped: // handle toward pin 1 -> COM is joined to GND -> LOW -> ON // handle toward pin 3 -> COM is open (pin 3 has no wire) -> the pull-up gives HIGH -> OFF if (state == LOW) Serial.println("ON - the handle is on the pin 1 side"); else Serial.println("OFF - the handle is on the pin 3 side"); } void setup() { Serial.begin(9600); // INPUT_PULLUP turns on the pull-up resistor inside the ESP32-S3. // It keeps D2 HIGH while COM is not connected to anything. pinMode(SLIDE_SWITCH_PIN, INPUT_PULLUP); lastState = digitalRead(SLIDE_SWITCH_PIN); // where is the handle right now? Serial.print("Slide switch at start: "); printState(lastState); } void loop() { int reading = digitalRead(SLIDE_SWITCH_PIN); if (reading != lastState) { // The level changed. The contacts may still be bouncing. delay(DEBOUNCE_MS); // wait for them to settle reading = digitalRead(SLIDE_SWITCH_PIN); // read again after the wait if (reading != lastState) { // still different, so the handle really moved Serial.print("Slide switch moved: "); printState(reading); lastState = reading; // remember the new position } } }

Detailed Instructions

  • Pull off the wire between pin 3 and 3V3. Keep the other two wires.
  • Upload the code above to the Arduino Nano ESP32.
  • Watch the Serial Monitor (still 9600 baud) while you flip the handle three times.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
Slide switch at start: OFF - the handle is on the pin 3 side Slide switch moved: ON - the handle is on the pin 1 side Slide switch moved: OFF - the handle is on the pin 3 side Slide switch moved: ON - the handle is on the pin 1 side
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2

Which version should you pick? The 3-wire one reads the natural way (HIGH = ON). The 2-wire one is tidier inside a small case. If you need a panel-mount rocker that you flip with a finger, go to Arduino Nano ESP32 - Switch. Several tiny switches in one chip-shaped block? Try Arduino Nano ESP32 - DIP Switch. And for a key that does not latch, read Arduino Nano ESP32 - Button Module.

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!