PyRaspBeta

Slide switch

A slide switch is a latching single-pole double-throw (SPDT) switch: the lever stays where you put it, joining the common pin to one throw or the other. It costs $0.50 and is what you reach for when the setting should survive without a finger on it — a mode selector, a door/window contact, a manual override. Because it has two throws, one GPIO can watch it and the pin is never left floating.

Slide switch

Pins

PinWhat it isType
1Throw 1 (lever left)passive
COMCommonpassive
2Throw 2 (lever right)passive
SettingTypeRangeDefault
positionenumleft, rightleft

COM is the moving contact. With the lever left it connects COM to 1; with the lever right to 2. The part's position setting (SlideSwitch("left") or SlideSwitch("right")) chooses where the lever starts, and because the two throws are often wired to 3V3 and GND, the pin has a defined level either way.

Wiring it in circuit.py

circuit.py
from wiring import *

pi = Pi()

switch = SlideSwitch("left")       # starts with COM joined to pin 1

pi.GPIO17 >> switch.common        # lever left: 3.3 V; lever right: 0 V
switch.pin1 >> pi.V3_3
switch.pin2 >> pi.GND

pi.GPIO18 >> Resistor(330) >> LED("red") >> pi.GND

With one GPIO you read one throw. To know both positions with a single pin, wire the throws to 3V3 and GND as above and treat "not left" as right: the level is meaningful either way. If you want the lever to switch a load rather than signal it, the relay is the part that handles current.

Driving it in main.py

main.py
from gpiozero import DigitalInputDevice, LED
from time import sleep

switch = DigitalInputDevice(17, pull_up=False, bounce_time=0.02)  # 1 = left, 0 = right
led = LED(18)

while True:
    led.value = switch.value
    sleep(0.05)

Unlike a push button, the level latches: read it whenever you like rather than waiting for a press. For an event, switch.when_activated and when_deactivated work as they do on any DigitalInputDevice.

Electrical notes

  • Closed contact resistance is modelled at 0.05 Ω and the switch is rated 300 mA — far beyond what a GPIO input draws, so the contacts are never the limiting factor.
  • Like every mechanical contact it bounces when moved: 1–5 ms typical, reproduced by the shared button/switch feed. A bounce_time of a few milliseconds in software is enough.
  • Never wire a throw to 5 V while COM goes to a GPIO: whichever way the lever sits, the 5 V reaches the pin and the simulator raises an overvoltage warning that fails the judge's damage check.
  • The 3V3 throw is a signal, not a supply: a slide switch is not a power switch for a motor or strip.

Simulator vs real hardware

  • Click the switch on the canvas while the simulation runs to flip the lever; the feed's bounce settings are the same knobs the push button uses.
  • The behaviour is two resistors, COM–1 and COM–2, swapping between 0.05 Ω and open, with the canvas lever following the position state. A real SPDT switch also has a break-before-make moment that the model approximates through the bounce burst.
  • DigitalInputDevice on a real Pi also lets you choose pull_up=None and rely on the external 3V3/GND wiring; gpiozero accepts that, but the simulator's default internal pull-down changes nothing here.
  • Multi-position and centre-off variants exist in hardware; the shop models the plain two-throw part.

Going deeper

Further reading