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.
Pins
| Pin | What it is | Type |
|---|---|---|
| 1 | Throw 1 (lever left) | passive |
| COM | Common | passive |
| 2 | Throw 2 (lever right) | passive |
| Setting | Type | Range | Default |
|---|---|---|---|
position | enum | left, right | left |
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
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.GNDWith 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
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_timeof a few milliseconds in software is enough. - Never wire a throw to 5 V while
COMgoes to a GPIO: whichever way the lever sits, the 5 V reaches the pin and the simulator raises anovervoltagewarning 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–1andCOM–2, swapping between 0.05 Ω and open, with the canvas lever following thepositionstate. A real SPDT switch also has a break-before-make moment that the model approximates through the bounce burst. DigitalInputDeviceon a real Pi also lets you choosepull_up=Noneand 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
- Primer: pull-up and pull-down resistors — why a throw to 3V3 and a throw to GND keep the pin defined.
- Sibling input: push button (momentary) and rotary encoder (rotary).
- Switching real current: relay module.
- Recipe: a door alarm is a switch plus a GPIO, which is exactly this part's day job.
Further reading
- VIMEX SS-12D00 slide switch specification — the SPDT slide switch the datasheet models (the Kobiconn-branded PDF is the same part).
- gpiozero: DigitalInputDevice — reading a latching contact.
- SparkFun: Button and switch basics — SPST vs SPDT, pull-ups and debouncing.