PyRaspBeta

← All practice problems

Raspberry Pi practice problem · Buttons & input

Switch events

EasyBudget $2Python · gpiozero

A slide switch's common pin on GPIO27: pin 1 to ground, pin 2 to 3.3 V. The lever starts left (GPIO27 low).

Print a line only when the lever moves: ON when it slides right, OFF when it slides left. Print nothing at start-up and nothing while it stays put — that is edge detection rather than reading the level over and over.

The test slides it right at 1 s, left at 2.5 s and right again at 4 s.

More in Buttons & input