Raspberry Pi - Slide Switch

A Raspberry Pi often works with no screen and no keyboard. It sits in a box and runs one Python script. But sometimes you still want to give it a simple order by hand: take photos or record video, do the real job or only a dry run, log data or stay idle. A small slide switch is a good tool for this. You set the handle once, and it keeps that setting until you change it. This tutorial instructs you how to use Raspberry Pi to read the position of the DIYables Mini Slide Switch SS12D10 from a Python script that runs over SSH. In detail, we will learn:

https://newbiely.com/images/cover/raspberry-pi-slide-switch.jpg

We suggest:

Hardware Preparation

1×Raspberry Pi 5
1×Mini Slide Switch SS12D10
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Raspberry Pi
1×Recommended: Raspberry Pi Prototyping Base Plate & Breadboard Kit

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 SS12D10 from DIYables is a two-position switch, barely bigger than a fingernail. On top, there is a short plastic handle that you move with a fingertip. Under the body, three metal legs stand in one row. The switch is latching: the handle stays at the end where you leave it, even while the Raspberry Pi is turned off. A push button is the opposite. It goes back to its rest position as soon as you let go. In electronics words, the SS12D10 is an SPDT switch (single pole, double throw), also sold as 1P2T.

What happens inside? A small metal bridge slides together with the handle. It always rests on the middle leg, called COM. At the same time, it rests on only one outer leg: the leg on the side where the handle is.

SS12D10 slide switch pinout with the middle COM leg between pin 1 and pin 3, and both handle positions

The switch carries no markings, so this tutorial counts the legs along the row:

  • Pin 1 (outer leg): This pin must be connected to ground (0V) of the Raspberry Pi
  • Pin 2, COM (middle leg): This pin must be connected to a GPIO pin of the Raspberry Pi. It passes on whatever voltage sits on the outer leg that the handle has selected
  • Pin 3 (outer leg): This pin must be connected to the 3.3V supply of the Raspberry Pi. In the 2-wire version later on this page, it stays free

The two handle positions in the picture:

  • Position 1: slide the handle to the pin 1 end. The bridge now connects pin 1 with COM, and pin 3 is not connected to anything.
  • Position 2: slide the handle to the pin 3 end. The bridge now connects COM with pin 3, and pin 1 is not connected to anything.

With the 3-wire wiring of this tutorial, the Python script sees this on GPIO17:

Handle position Voltage on COM GPIO.input(17) returns The script prints
Pin 1 end 0V (ground) 0 (LOW) OFF
Pin 3 end 3.3V 1 (HIGH) ON

The main data of the switch, from the DIYables product page:

DIYables Mini Slide Switch SS12D10
Positions 2, latching
Contact type SPDT, also called 1P2T
Contacts silver-plated brass, rated AC 125V 2A
Number of legs 3, vertical through-hole
Distance between legs 4.7 mm
Leg length 6.9 mm
Body size roughly 12.8 x 6.7 x 18.2 mm
Handle 5 mm
Lifetime more than 100,000 switching cycles
Works from -25°C to 80°C

Do not worry about the AC 125V 2A rating. That number is only the upper limit of the contacts. Here, the switch only feeds a very small logic current to GPIO17.

One more thing to know: when the handle moves, the metal bridge bounces for a few milliseconds. A fast Python loop can read 0, 1, 0, 1 before the level settles. Both scripts below check every change a second time after 50 milliseconds, so one move gives one line.

※ NOTE THAT:

Do not look for labels on the SS12D10, because there are none. What counts is the place in the row: the leg in the middle is COM. The two outer legs work the same way. If you mix them up, nothing breaks. The script simply prints ON for the side that used to be OFF.

Wiring Diagram

The wiring diagram between Raspberry Pi and slide switch

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin Raspberry Pi Pin
Pin 1 GND
Pin 2 (COM) GPIO17
Pin 3 3.3V

Where each wire goes on the 40-pin header:

  • Slide switch pin 1 to Raspberry Pi GND (pin 6, or any other GND pin). This is the black wire in the diagram.
  • Slide switch pin 2 (COM) to Raspberry Pi GPIO17 (pin 11). This is the yellow wire.
  • Slide switch pin 3 to Raspberry Pi 3.3V (pin 17, or pin 1). This is the red wire.

In both handle positions, GPIO17 is joined either to 3.3V or to ground. The pin never floats, so you need no resistor at all, and the script turns off the internal pull resistors of the Pi.

Watch out for one trap: the number 17 shows up twice here. GPIO17 is on physical pin 11, while physical pin 17 is a 3.3V pin. The scripts use BCM numbering (GPIO.setmode(GPIO.BCM)), so the code says 17 and means GPIO17 on pin 11.

The legs of the switch are long enough for the end of a female jumper wire to slip on, so you do not need a breadboard. For a project that must last for years, solder the wires to the legs.

※ NOTE THAT:

  • Pin 3 of the switch must go to 3.3V (pin 17 or pin 1). NEVER connect it to a 5V pin (pin 2 or pin 4). In the pin 3 position, the switch connects GPIO17 directly to whatever voltage is on pin 3. Raspberry Pi GPIO pins are made for 3.3V only. They are not 5V tolerant, and 5V on GPIO17 can kill that pin or even the whole board.
  • GPIO17 must stay an input. Do not use GPIO.OUT on it in any other script while the switch is connected. The switch holds the pin at 3.3V or at ground all the time. If the Pi drove the pin to the opposite level, current would flow straight through the switch: a short circuit.

How To Program for Raspberry Pi to Read the Slide Switch Position

No extra library is needed. RPi.GPIO and the built-in time module do all the work. The most important line sets GPIO17 as an input:

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_OFF)

GPIO.PUD_OFF turns off the pull-up and pull-down resistors inside the Pi. The switch itself already drives the pin to 3.3V or to ground, so the Pi does not need to help.

The script reads the handle once at start and reports its position. From then on, the terminal shows nothing until the handle reaches the other end. Each report starts with the time from the Raspberry Pi clock, so you can see when the setting was changed.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Wire the slide switch to the Raspberry Pi as shown in the wiring diagram above. Check that the red wire is on pin 17 (3.3V), not on a 5V pin.
  • Create a Python script file slide_switch.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-slide-switch """ import RPi.GPIO as GPIO import time # BCM numbering: 17 means GPIO17, which is physical pin 11 # (physical pin 17 is a 3.3V pin, do not mix them up) GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) SLIDE_SWITCH_PIN = 17 # GPIO17 (pin 11) connected to the COM pin (middle leg) of the slide switch DEBOUNCE_SECONDS = 0.05 # 50 ms, longer than the contact bounce of the switch # Pin 1 of the switch is on GND and pin 3 is on 3.3V. # COM always touches one of them, so GPIO17 is always driven to 0V or 3.3V and never floats. # That is why the internal pull-up / pull-down resistors are turned off with PUD_OFF. GPIO.setup(SLIDE_SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_OFF) def position_text(level): # 1 (HIGH): handle toward pin 3, COM is on 3.3V -> ON # 0 (LOW): handle toward pin 1, COM is on GND -> OFF if level == GPIO.HIGH: return "ON" return "OFF" def now(): return time.strftime("%H:%M:%S") # the clock of the Raspberry Pi print("Reading the slide switch on GPIO17. Press Ctrl + C to stop.") last_state = GPIO.input(SLIDE_SWITCH_PIN) # the last confirmed position print("[{}] Start position: The slide switch is {} (GPIO17 = {})".format(now(), position_text(last_state), last_state)) try: while True: reading = GPIO.input(SLIDE_SWITCH_PIN) if reading != last_state: # The handle is moving, and the metal contacts bounce for a few milliseconds. # Wait 50 ms, then read again before we trust the change. time.sleep(DEBOUNCE_SECONDS) reading = GPIO.input(SLIDE_SWITCH_PIN) if reading != last_state: # still different after the wait -> the handle really moved last_state = reading print("[{}] The slide switch is {} (GPIO17 = {})".format(now(), position_text(reading), reading)) time.sleep(0.01) # check about 100 times per second without using 100% of one CPU core except KeyboardInterrupt: print("\nScript stopped") finally: GPIO.cleanup() # release GPIO17
  • Save the file and run the Python script by executing the following command in the terminal:
python3 slide_switch.py
  • Before you start the script, set the handle to the pin 3 end. Then move it four times, with a short pause after each move: pin 1 end, pin 3 end, pin 1 end, pin 3 end.
  • Check the terminal:
PuTTY - Raspberry Pi
Reading the slide switch on GPIO17. Press Ctrl + C to stop. [10:26:05] Start position: The slide switch is ON (GPIO17 = 1) [10:26:09] The slide switch is OFF (GPIO17 = 0) [10:26:13] The slide switch is ON (GPIO17 = 1) [10:26:20] The slide switch is OFF (GPIO17 = 0) [10:26:24] The slide switch is ON (GPIO17 = 1)

The first line tells you where the handle was when the script started. After that, four moves give exactly four lines, each with its own time. The loop checks the pin about 100 times per second, so even a quick flick of the handle is caught. Press Ctrl + C keys in the terminal to stop the script. It then calls GPIO.cleanup() to free GPIO17.

Nothing changes when you move the handle? Check these points:

  • The yellow wire must sit on the middle leg of the switch. On an outer leg, GPIO17 only ever sees one level.
  • The GPIO17 wire must be on pin 11. Pin 17 is the 3.3V pin, not GPIO17.
  • Lines that show up by themselves, with no hand on the switch, mean a loose wire: GPIO17 is floating.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

How To Program for Raspberry Pi to Read the Slide Switch with GPIO.PUD_UP (2 Wires)

The Broadcom chip of the Raspberry Pi has a pull-up resistor built in for each GPIO pin. GPIO.PUD_UP turns it on, and then it can do the job of the 3.3V wire. This is useful when the 3.3V pins are already busy with other modules, or when you want one wire less in a small case. Only two wires are left: COM to GPIO17 and pin 1 to ground.

The wiring diagram between Raspberry Pi and slide switch INPUT_PULLUP

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield
Slide Switch Pin Raspberry Pi Pin
Pin 1 GND
Pin 2 (COM) GPIO17
Pin 3 not connected
  • Slide switch pin 1 to Raspberry Pi GND (pin 6, or any other GND pin)
  • Slide switch pin 2 (COM) to Raspberry Pi GPIO17 (pin 11)
  • Slide switch pin 3 stays free

The setup line changes to this:

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

Be careful, because the meaning of 0 and 1 is now reversed:

  • Handle at the pin 1 end: GPIO17 is linked to ground through COM, so GPIO.input(17) gives 0. The script shows ON.
  • Handle at the pin 3 end: pin 3 is unwired, so COM leads nowhere. The internal pull-up holds GPIO17 at 3.3V, GPIO.input(17) gives 1, and the script shows OFF.

Detailed Instructions

  • If the first script is still running, stop it with Ctrl + C.
  • Remove the red wire completely: take it off pin 3 of the switch and off pin 17 of the Raspberry Pi. Then no loose 3.3V wire end hangs near the board. The black wire and the yellow wire stay where they are.
  • Create a Python script file slide_switch_input_pullup.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-slide-switch """ import RPi.GPIO as GPIO import time # BCM numbering: 17 means GPIO17, which is physical pin 11 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) SLIDE_SWITCH_PIN = 17 # GPIO17 (pin 11) connected to the COM pin (middle leg) of the slide switch DEBOUNCE_SECONDS = 0.05 # 50 ms, longer than the contact bounce of the switch # 2-wire version: pin 1 of the switch is on GND, pin 3 is NOT connected. # When the handle is toward pin 3, COM touches nothing, so the pin needs help to stay HIGH. # PUD_UP turns on the pull-up resistor inside the Raspberry Pi. It does the job of the 3.3V wire. GPIO.setup(SLIDE_SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) def position_text(level): # The logic is flipped compared with the 3-wire version: # 0 (LOW): handle toward pin 1, COM joins GPIO17 to GND -> ON # 1 (HIGH): handle toward pin 3, COM is open, the pull-up wins -> OFF if level == GPIO.LOW: return "ON" return "OFF" def now(): return time.strftime("%H:%M:%S") # the clock of the Raspberry Pi print("Reading the slide switch on GPIO17 (internal pull-up). Press Ctrl + C to stop.") last_state = GPIO.input(SLIDE_SWITCH_PIN) # the last confirmed position print("[{}] Start position: The slide switch is {} (GPIO17 = {})".format(now(), position_text(last_state), last_state)) try: while True: reading = GPIO.input(SLIDE_SWITCH_PIN) if reading != last_state: # Contact bounce: wait 50 ms and read again before we trust the change time.sleep(DEBOUNCE_SECONDS) reading = GPIO.input(SLIDE_SWITCH_PIN) if reading != last_state: # still different after the wait -> the handle really moved last_state = reading print("[{}] The slide switch is {} (GPIO17 = {})".format(now(), position_text(reading), reading)) time.sleep(0.01) # check about 100 times per second without using 100% of one CPU core except KeyboardInterrupt: print("\nScript stopped") finally: GPIO.cleanup() # release GPIO17
  • Save the file and run it with this command:
python3 slide_switch_input_pullup.py
  • This time, put the handle at the pin 1 end before you start the script. Then move it three times: first to the pin 3 end, then back to pin 1, then to pin 3 again.
  • Check the terminal:
PuTTY - Raspberry Pi
Reading the slide switch on GPIO17 (internal pull-up). Press Ctrl + C to stop. [10:41:37] Start position: The slide switch is ON (GPIO17 = 0) [10:41:42] The slide switch is OFF (GPIO17 = 1) [10:41:45] The slide switch is ON (GPIO17 = 0) [10:41:51] The slide switch is OFF (GPIO17 = 1)

Look at the GPIO17 numbers. In the first test, 0 went with OFF. Now 0 goes with ON: with the handle at the pin 1 end, the switch pulls GPIO17 to ground, and the script treats that as ON. Press Ctrl + C keys to stop the script.

Additional Knowledge

3 Wires (PUD_OFF) vs 2 Wires (PUD_UP)

Let's compare the two wirings from this tutorial:

3 Wires with GPIO.PUD_OFF 2 Wires with GPIO.PUD_UP
Wires GND, GPIO17 and 3.3V GND and GPIO17
Pin 3 of the switch on 3.3V (pin 17) not connected
What sets the HIGH level the 3.3V wire through the switch the pull-up resistor inside the Pi
Handle at the pin 3 end 1 (HIGH), printed as ON 1 (HIGH), printed as OFF
Handle at the pin 1 end 0 (LOW), printed as OFF 0 (LOW), printed as ON
Risk if wired wrong 5V on pin 3 can damage GPIO17 low, no supply wire goes near the switch

Evidently, three wires give the easy logic, where 1 means ON. Two wires keep the 3.3V pins free and are a little safer, because no supply voltage is routed to the switch at all.

Project Ideas with the Slide Switch

  • Boot mode selector: a Python script that starts at boot reads the switch once and picks its job, for example "camera recorder" or "web server".
  • Maintenance switch: while the handle is on OFF, a kiosk or display script pauses, so you can log in over SSH and update files in peace.
  • Privacy switch: stop the camera or microphone recording as soon as the handle moves to OFF.
  • Data logger switch: write sensor values to a CSV file only while the switch is ON.

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!