DEV Community

Cover image for Raspberry Pi - Slide Switch
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Raspberry Pi - Slide Switch

A headless Raspberry Pi running one script still needs a way to take simple manual input: record or idle, real run or dry run, log or stay quiet. A slide switch is perfect for that, since it latches in place and keeps its setting even when the Pi is powered off. This walks through reading a DIYables SS12D10 slide switch from Python over SSH, both with three wires and with only two using the Pi's internal pull-up.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Raspberry Pi - Slide Switch

What you need

Wiring

Raspberry Pi - Slide Switch wiring diagram

The SS12D10 is an SPDT switch with three legs in a row. The middle leg, COM, always touches whichever outer leg the handle is pushed toward. Wire pin 1 to a Raspberry Pi GND pin, COM to GPIO17 (physical pin 11), and pin 3 to 3.3V (physical pin 17 or 1). Since GPIO17 is always tied to either 3.3V or ground by the switch itself, it never floats, so no resistor is needed and the script turns off the Pi's internal pull resistors. Watch out: GPIO17 lives on physical pin 11, not physical pin 17, which is actually a 3.3V pin. The legs are long enough to take a female jumper directly, no breadboard required.

How it works

Inside the switch, a metal bridge moves with the handle and always rests on COM plus one outer leg. Slide the handle to the pin 1 end and COM connects to ground; slide it to the pin 3 end and COM connects to 3.3V. Because the Pi reads GPIO17 through COM, the pin's voltage directly reflects the handle's position, high or low, with nothing in between.

Code

The script uses only RPi.GPIO and time, no extra libraries. It sets GPIO17 as an input with GPIO.PUD_OFF, since the switch already drives the pin to 3.3V or ground and doesn't need the Pi's internal pull resistor. It reads the handle position once at startup, then watches for changes and prints a timestamped report only when the position flips. For the two-wire version, pin 3 is left disconnected and the setup switches to GPIO.PUD_UP, which uses the Pi's built-in pull-up resistor instead of the 3.3V wire; this reverses the logic, so a reading of 0 now means ON.

raspberry-pi-slide-switch.py

"""
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
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

Mixing up physical pin 11 and physical pin 17 is the easiest mistake, since both involve the number 17 but one is GPIO17 and the other is a 3.3V supply pin. If you use GPIO.PUD_UP for the two-wire wiring, remember the ON/OFF logic is inverted compared to the three-wire version, so check which setup your code expects. For anything long-term, solder the wires to the switch legs instead of relying on jumper wires alone.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-slide-switch

Top comments (0)