Learn

Wiring a 4x4 Membrane Keypad to a Raspberry Pi 4B

Wire a 4x4 membrane keypad to 8 Raspberry Pi 4B GPIO pins and scan it in Python to detect key presses — wiring, code, and a real hardware limitation to expect.

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

Step by step

  1. Drag Raspberry Pi 4B onto the Canvas.
  2. Drag the 4x4 Membrane Keypad onto the Canvas — connects via drawn wires, not breadboard legs.
  3. Hover over all 8 pins to confirm labels: R1-R4 (rows), C1-C4 (columns).
  4. Wire all 8 pins to 8 separate GPIO pins — note down which physical pin number you used for each row and column.
  5. Check the button layout printed on the component itself (common real-world layout is 1 2 3 A / 4 5 6 B / 7 8 9 C / * 0 # D — confirm against what's actually drawn on your component).
import RPi.GPIO as GPIO
import asyncio
 
rows = [37, 35, 33, 31] # adjust to your R1-R4 wiring
cols = [29, 23, 21, 19] # adjust to your C1-C4 wiring
 
keys = [
["1", "2", "3", "A"],
["4", "5", "6", "B"],
["7", "8", "9", "C"],
["*", "0", "#", "D"],
]
 
GPIO.setmode(GPIO.BOARD)
for r in rows:
GPIO.setup(r, GPIO.OUT)
GPIO.output(r, 0)
for c in cols:
GPIO.setup(c, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
 
while True:
for i, r in enumerate(rows):
GPIO.output(r, 1)
for j, c in enumerate(cols):
if GPIO.input(c) == 1:
print(f"Key pressed: {keys[i][j]}")
GPIO.output(r, 0)
await asyncio.sleep(0.1)

What “working correctly” looks like

More component tutorials are in Learn.