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
- Raspberry Pi 4B
- 4x4 Membrane Keypad
Step by step
- Drag Raspberry Pi 4B onto the Canvas.
- Drag the 4x4 Membrane Keypad onto the Canvas — connects via drawn wires, not breadboard legs.
- Hover over all 8 pins to confirm labels: R1-R4 (rows), C1-C4 (columns).
- Wire all 8 pins to 8 separate GPIO pins — note down which physical pin number you used for each row and column.
- 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 GPIOimport asyncio rows = [37, 35, 33, 31] # adjust to your R1-R4 wiringcols = [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
- Pressing any single button prints its correct matching character exactly once per press-check cycle, matching the correct row/column intersection.
- Releasing stops the printing.
- Pressing two buttons at once is expected to behave oddly or only register one — that's not a bug, it's a deliberate real-hardware limitation this component was built to match (genuine membrane keypads need extra anti-ghosting diodes to reliably read multiple simultaneous presses, which this simulator was scoped to skip).
More component tutorials are in Learn.