Learn

Reading a Push Button (Tact Switch) on a Raspberry Pi 4B — Pull-Down Input

Wire a tact switch to a Raspberry Pi 4B GPIO input pin with a pull-down resistor, read it in Python, and light an LED while it's held — the foundation for every button-driven project.

Try this directly in the free Raspberry Pi 4B simulator — no hardware or signup required. New to the GPIO header? Start with the interactive pinout guide.

What you’ll need

  • Raspberry Pi 4B
  • Tact Switch
  • Resistor (~10kΩ, pull-down)
  • Resistor (~220Ω, for the LED)
  • LED

Step by step

  1. Drag Raspberry Pi 4B and the Tact Switch onto the Canvas.
  2. Hover over its 4 pins to confirm the labels: Terminal 1.1, Terminal 2.1 (the top pair — these two only connect to each other while the switch is HELD) and Terminal 1.2, Terminal 2.2 (the bottom pair — Terminal 1.2 is always internally connected to Terminal 1.1, and Terminal 2.2 to Terminal 2.1, whether pressed or not).
  3. Wire the switch using its top pair: Terminal 1.1 → a Pi 3.3V pin. Terminal 2.1 → a GPIO pin (e.g. pin 37).
  4. Wire the pull-down resistor: one leg → the same GPIO pin (pin 37). The other leg → a Pi GND pin. This resistor is what makes the pin read a clean 0 when the switch isn't pressed, instead of an unreliable floating value.
  5. Drag a second Resistor and an LED onto the Canvas for output feedback: a different GPIO pin (e.g. pin 35) → the Resistor → the LED's anode (+). LED's cathode (−) → a Pi GND pin.
  6. Go to the Code tab and write:
import RPi.GPIO as GPIO
import asyncio
 
buttonPin = 37
ledPin = 35
 
GPIO.setmode(GPIO.BOARD)
GPIO.setup(buttonPin, GPIO.IN)
GPIO.setup(ledPin, GPIO.OUT)
 
while True:
pressed = GPIO.input(buttonPin)
GPIO.output(ledPin, pressed)
print(f"Button: {pressed}")
await asyncio.sleep(0.2)
  1. Click Start.
  2. Click and hold the switch on the canvas.

What “working correctly” looks like

  • At rest, the Console repeats "Button: 0" and the LED stays off.
  • The instant you press and hold the switch, it flips to "Button: 1" and the LED turns on, for as long as you hold it — release, and both go back to 0/off.

If something’s wrong

  • The Console always reads 0, even while held → double-check the switch is wired using ONE pin from each pair (e.g. Terminal 1.1 and Terminal 2.1) — two pins from the SAME side (like Terminal 1.1 and Terminal 1.2) are always connected to each other and will never register a press.
  • The Console always reads 1, even at rest → confirm the pull-down resistor's second leg genuinely reaches a Pi GND pin — without a real path to ground, the input pin is left floating and can read an unpredictable value.