PyRaspBeta

Push button

A push button is a momentary, normally-open switch in a 6 × 6 mm tactile case. It costs $0.50 and is the simplest way to get a human into the circuit: press to start, hold to stop, tap to count. It has no electronics inside — just a metal contact that joins its two sides while your finger is down — so the Pi needs a pull-up or pull-down to give the pin a defined level when the button is released.

Push button

Pins

PinWhat it isType
A1Side A, toppassive
A2Side A, bottompassive
B1Side B, toppassive
B2Side B, bottompassive

The two legs on each side are one piece of metal: A1 and A2 are internally joined, and so are B1 and B2. Pressing connects side A to side B, which is why the part's chain ends are A1 >> B1. On a breadboard, use legs from opposite sides (or diagonally) so you are not just shorting the same piece of metal.

Wiring it in circuit.py

Pressed = 3.3 V (the pull-down style): the button links the pin to 3V3 and a resistor keeps it low at rest, or you let the Pi's internal pull-down do that job in software.

circuit.py
from wiring import *

pi = Pi()

pi.GPIO27 >> Button() >> pi.V3_3   # pressed: GPIO27 sees 3.3 V

pi.GPIO17 >> Resistor(330) >> LED("green") >> pi.GND

The other common wiring is active-low: pi.GPIO27 >> Button() >> pi.GND with a pull-up — either an external 10 kΩ to 3V3, or gpiozero's internal pull-up. The button module kit is the pull-down version pre-wired.

Driving it in main.py

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

button = Button(27, pull_up=False, bounce_time=0.02)  # 3V3 side + internal pull-down
led = LED(17)

while True:
    led.value = button.value    # follow the button in real time
    sleep(0.02)

Callbacks are the idiomatic gpiozero style:

from gpiozero import Button
from signal import pause

button = Button(27, pull_up=False, bounce_time=0.02)
button.when_pressed = lambda: print("pressed")
button.when_released = lambda: print("released")
pause()

Electrical notes

  • Closed contact resistance is modelled at 0.1 Ω; the switch is rated 50 mA at 12 V, far above what a GPIO input draws, so a button can never overload a pin.
  • An input wired only to a button floats when released and reads random levels — always pair it with a pull-up or pull-down. External 10 kΩ resistors are the classic answer; the Pi's internal pulls are roughly 50 kΩ (the datasheet says 33–73 kΩ).
  • Contacts bounce for a few milliseconds on press and release. The datafeed turns each change into a burst of open/close chatter (5 ms, 4 bounces by default) before it settles, so software polling without debounce sees several presses. bounce_time=0.02 in gpiozero, or polling a stable state twice, fixes it.
  • Never wire a button between a GPIO and 5 V: the 5 V reaches the pin and the simulator raises an overvoltage warning that fails the judge's damage check.

Simulator vs real hardware

  • Click and hold the button on the canvas while the simulation runs; the feed's bounceMs and bounceCount knobs change how dirty the contacts are, from perfectly clean to 20 ms of chatter.
  • The model is a single resistor between A1 and B1 that flips between 0.1 Ω and open; real switches also wear out, suffer contact oxidation and differ press-to-press.
  • gpiozero's edge detection and bounce_time are simulated faithfully; the internal pulls are part of the Pi's pin model, so both wiring styles behave.
  • A real tactile switch has four physical legs; the canvas shows the same, and pressing anywhere on the cap closes A–B regardless of which leg pair you wired.

Going deeper

Further reading