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.

Note: the 4x4 Membrane Keypad is a Premium-tier component in the simulator — you’ll need a Premium or VIP plan to use it there.

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

  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)
  1. Click Start. Then click and hold a key on the keypad. The key stays pressed for as long as you hold the mouse button.

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).

If something’s wrong

  • Nothing prints when you click a key → hold the mouse button down. The script only checks every 0.1 seconds, so a very quick click can be missed.
  • Nothing prints even when holding → check all 8 wires. R1 to R4 go to the row pins your script drives, and C1 to C4 go to the column pins it reads. The order in the lists must match your wiring.
  • The wrong character prints → your rows and columns are swapped, or the wires are in the wrong order. Hover each pin to confirm its R or C label.
  • The same key prints many times → that is normal while you hold it. It prints once per scan cycle.
  • The Console fills with "Pin 37 → HIGH" lines and hides your key messages → turn off "Show pin changes" in the Console panel header.
  • Only one key can be held at a time, because the mouse can only press one key.