ESP32 MicroPython Slide Switch
This tutorial instructs you how to use the DIYables Mini Slide Switch SS12D10 with the ESP32 and MicroPython. Its small handle stays on the side where you leave it, so your MicroPython script can ask the ESP32 at any moment which side that is. In detail, we will learn:
- What the three unmarked legs of the slide switch do, and which one carries the signal to the ESP32.
- How to wire the switch to GND, GPIO21 and 3V3 of the ESP32, with no resistor at all.
- How to test the wiring by typing three short lines into the Thonny Shell.
- How to write MicroPython code that prints ON or OFF at start and again after every move, with a 50 ms check against contact bounce.
- How to remove the 3V3 wire and use Pin.PULL_UP instead, and why ON then reads 0.
https://newbiely.com/images/cover/esp32-micropython-slide-switch.jpg
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables ESP32 Starter Kit (ESP32 included) | |
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Slide Switch
You may know this kind of switch from a pocket calculator or an LED tea light: one tiny handle that slides between two ends. The DIYables Mini Slide Switch SS12D10 is a small version with three legs, made for circuit boards and DIY projects. It is a latching switch. Push the handle to one end, and it stays there until someone pushes it back, even while the ESP32 is turned off. A push button is different: its spring lifts it back up the moment your finger leaves it. In parts lists, this type is called SPDT (single pole, double throw) or 1P2T.
| Mini Slide Switch SS12D10 | |
|---|---|
| Contact type | SPDT (1P2T), 2 positions |
| Pins | 3 in one row, 4.7mm pitch |
| Terminal length | 6.9mm |
| Handle | 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 |
| Switching life | more than 100,000 cycles |
| Operating temperature | -25°C to 80°C |
The contact rating is the most that the metal parts inside can switch. On the ESP32, the switch only passes a very small signal current to GPIO21, so this project never comes close to that limit.
Pinout

There are no labels on the switch. You tell the pins apart by their place in the row:
- Pin 1 (an outer pin): connect to GND (0 volts) of the ESP32.
- Pin 2, COM (the middle pin): connect to GPIO21 of the ESP32. This is the pin your code reads.
- Pin 3 (the other outer pin): connect to the 3V3 pin (3.3 volts) of the ESP32. In the 2-wire version further down, this pin stays free.
Inside the switch, a little metal bridge rides along with the handle. It always touches COM, and it also touches the outer pin on the handle's side:
- Handle toward pin 1: pins 1 and 2 are connected, and pin 3 is open. GPIO21 gets 0 volts, so value() returns 0. This tutorial calls it OFF.
- Handle toward pin 3: pins 2 and 3 are connected, and pin 1 is open. GPIO21 gets 3.3 volts, so value() returns 1. This tutorial calls it ON.
The switch itself has no ON or OFF mark, so the names are your choice. If you want the pin 1 side to be ON, just swap the GND and 3V3 wires.
One thing to plan for: the bridge does not stop cleanly. For a few milliseconds after a move, it bounces on the contact, and a fast MicroPython loop can see that as 0, 1, 0, 1. The code below waits 50 ms and reads GPIO21 again before it believes a change.
Wiring Diagram
The ESP32 and the slide switch are plugged into the same breadboard. Three jumper wires go from the holes next to the ESP32's GND, GPIO21 and 3V3 pins to the columns of the switch's three legs. You do not need a resistor. In both handle positions, COM is joined to GND or to 3V3, so GPIO21 always has a clear level and the code can use a plain Pin.IN.

This image is created using Fritzing. Click to enlarge image
| Slide Switch Pin | ESP32 Pin |
|---|---|
| Pin 1 | GND |
| Pin 2 (COM) | GPIO21 |
| Pin 3 | 3V3 (3.3 volts, never 5V or VIN) |
※ NOTE THAT:
Pin 3 belongs on the 3V3 pin. When the handle is toward pin 3, COM sends the pin 3 voltage straight into GPIO21, and the ESP32 GPIO pins can only take 3.3 volts. A wire from pin 3 to VIN or 5V would put 5 volts on GPIO21 each time you slide the handle, and that can damage the ESP32.
Keep GPIO21 an input in every script you run with this wiring. If some code sets it to Pin.OUT and drives the other level, the switch shorts that output straight to 3V3 or GND.
A small MicroPython detail: the number in Pin(21) is the GPIO number, the same number that is printed next to the pin on the board (for example 21, D21 or IO21). It is not the place of the pin in the header row. To use another pin, change only SLIDE_SWITCH_PIN in the code.
Quick Wiring Test in the Thonny Shell
You can check the wiring before you write any script. Open Thonny with the ESP32 connected (the setup is in the Quick Steps below), click in the Shell, and type these three lines, pressing Enter after each one:
The Shell answers with 1 or 0. Now slide the handle to the other end, press the Up arrow key to bring back sw.value(), and press Enter again. The answer should flip: the handle toward pin 3 gives 1, the handle toward pin 1 gives 0. If the answer never changes, check the COM wire on GPIO21 first.
ESP32 MicroPython Code - Slide Switch
The script below sets GPIO21 as an input with Pin(SLIDE_SWITCH_PIN, Pin.IN), with no pull resistor. At the start, it reads the pin once and prints where the handle is. Then the while loop keeps reading. As long as the value stays the same, it prints nothing, so the Shell stays clean. When the value changes, the script sleeps 50 ms with time.sleep_ms(), reads again, and prints the new position only if the change is still there. The small print_state() function turns 1 into ON and 0 into OFF.
Detailed Instructions
Here's instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:
- Install Thonny IDE on your computer and flash MicroPython firmware onto the ESP32, if you have not done so yet.
- First time with MicroPython on the ESP32? The ESP32 MicroPython Getting Started guide walks you through both steps.
- Make the three connections from the wiring table (GND, GPIO21 and 3V3).
- Connect the ESP32 to your computer with the USB cable and open Thonny.
- Go to Tools Options, then on the Interpreter tab select MicroPython (ESP32).
- Choose the port if Thonny does not pick it by itself (for example COM4 on Windows or /dev/ttyUSB0 on Linux).
- Copy the code above into a new Thonny editor tab.
- Save it on the ESP32:
- Press Ctrl+S, or click the Save button.
- Click MicroPython device in the dialog that pops up.
- Type main.py as the file name and click OK.
- Push the handle toward pin 3, then click the green Run button or press F5.
- Slide the handle to the other end and back, two times. Pause for a second at each end.
- The Shell at the bottom of Thonny shows the start position and each move:
If you name your script main.py and save it to the MicroPython device, it will run automatically every time the ESP32 is powered on.
The first ON line is the start position. After that, each move adds exactly one line, and the bounce adds none. Do lines appear while nobody touches the switch? Then GPIO21 has lost its connection to COM and picks up noise. Push each wire firmly into its breadboard hole and onto its switch leg. Do you want ON and OFF the other way round? Swap the two outer wires (GND and 3V3). The code stays the same.
ESP32 MicroPython Code - Slide Switch on 2 Wires (Pin.PULL_UP)
Short on wires, or need the 3V3 pin for another part? GPIO21 has a pull-up resistor inside the ESP32 chip. With Pin.PULL_UP, that resistor lifts the pin to 1 by itself, so the 3V3 wire on pin 3 can go. Two wires are left: pin 1 to GND and COM to GPIO21.

This image is created using Fritzing. Click to enlarge image
| Slide Switch Pin | ESP32 Pin |
|---|---|
| Pin 1 | GND |
| Pin 2 (COM) | GPIO21 |
| Pin 3 | not connected |
This flips the meaning of the values:
- Handle toward pin 1: the bridge ties GPIO21 to GND through COM. The pin reads 0, which the script shows as ON.
- Handle toward pin 3: COM only touches the free pin 3. The pull-up keeps GPIO21 at 1, which the script shows as OFF.
Moving COM to another pin? Skip GPIO34 to GPIO39 for this version. They are input-only pins with no internal pull-up, so with two wires they would float.
Detailed Instructions
- Click the red Stop button in Thonny. Then pull out only the pin 3 wire. The GND and GPIO21 wires stay in place.
- Paste the new code into the editor and save it to the MicroPython device as main.py. Let Thonny replace the first script.
- Push the handle toward pin 3, then press F5.
- Slide the handle toward pin 1, then back toward pin 3.
Look at the values. The pin 3 side still reads 1, just like in the first script, but the word next to it is now OFF. The handle and GPIO21 did not change, only the wiring did. So always keep each script with its own wiring: the 3-wire script expects pin 3 on 3V3, and the 2-wire script lets Pin.PULL_UP do that job.
Project Ideas
A slide switch on the ESP32 is handy wherever a setting must survive a restart:
- A "run / edit" switch for main.py: at power-on, the script reads the switch and stops right away when it is OFF. The ESP32 then waits at the >>> prompt instead of starting your project, which helps a lot while you are still editing files in Thonny.
- A pause switch for a data logger, like the one in ESP32 MicroPython - Log Data with Timestamp to SD Card: OFF stops the writing, so you can take the card out safely.
- A night mode for a display: ON dims an OLED or LCD, OFF makes it bright again.
- An arm switch for an alarm with a motion sensor: OFF ignores the sensor, ON lets it sound the alarm.
Looking for another kind of switch? A bigger ON/OFF rocker switch for the panel of a box is covered in ESP32 MicroPython Switch. A switch that a moving part presses is in ESP32 MicroPython Limit Switch, and a key that springs back is in ESP32 MicroPython Button Module.