ABC of Electronics cosycom.com
RASPBERRY PI 10 · GPIO Basics

Reading a Push Button with GPIO Input

Read a button press in Python using the Pi's built-in pull-up/pull-down support — no external resistor required.

You'll need: a push-button, jumper wires, a breadboard, and your Pi.

Lesson cb-08 explained why a floating digital input needs a pull-up or pull-down resistor. Unlike the plain Arduino build in that lesson, the Raspberry Pi's GPIO library can enable an internal pull-up or pull-down resistor in software, so you can wire the button with just two wires — one leg to a GPIO pin, the other to GND — with no separate physical resistor needed at all.

button.py
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    while True:
        if GPIO.input(27) == GPIO.LOW:
            print("Button pressed")
        time.sleep(0.1)
except KeyboardInterrupt:
    GPIO.cleanup()

pull_up_down=GPIO.PUD_UP enables the pin's internal pull-up resistor, meaning the pin reads HIGH by default and goes LOW when your button (wired to GND) is pressed — this is the internal-pull-up pattern mentioned back in Lesson ard-06, just enabled here in Python instead of hardware wiring. The logic in the loop is intentionally inverted from what you might expect at first: checking for GPIO.LOW to detect a press, not HIGH.

time.sleep(0.1) inside the loop isn't strictly required, but it's good practice — without any pause, this loop would run continuously as fast as the Pi possibly can, using far more CPU than a simple button check needs.

Pi GPIO27 Pi GND no external resistor neededabcofelectronics
Just the button between a GPIO pin and ground — the internal pull-up resistor is enabled entirely in software.
TRY THIS
With PUD_UP enabled, a pressed button reads LOW, not HIGH — the internal pull-up inverts the logic compared to an external pull-down wired the way Lesson cb-08 described.