基于RPi GPIO与Python的4×4矩阵键盘连接及编程求助
Hey there! Let's break this down step by step since you're new to GPIO and can't open the keyboard—no problem, we can get this working without taking it apart.
Step 1: Map Your Keyboard's Pinout (Without Opening It)
4x4 matrix keyboards always have 8 pins total: 4 for rows, 4 for columns. To figure out which is which, use one of these two methods:
- Multimeter Method (if you have one): Set your multimeter to continuity mode. Press a key and test pairs of pins—when you hear a beep, those two pins are connected via the key. Repeat this with different keys: you’ll notice 4 pins each connect to 4 others (those are your rows), and 4 pins each connect to 4 others (those are your columns).
- Raspberry Pi GPIO Test: Connect all 8 keyboard pins to unused GPIO pins on your Pi. Set all pins as inputs with internal pull-up resistors. Then, one by one, set each pin to output low and read the others. Any pin that reads low is connected to the output pin via a key. Do this for all pins to group rows and columns.
Step 2: Wire the Keyboard to Your Pi
Once you’ve identified 4 row pins and 4 column pins, connect them to your Raspberry Pi’s GPIO pins. Here’s a common example (adjust to match your pinout):
- Row 1 → GPIO17
- Row 2 → GPIO27
- Row 3 → GPIO22
- Row 4 → GPIO5
- Column 1 → GPIO6
- Column 2 → GPIO13
- Column 3 → GPIO19
- Column 4 → GPIO26
Use standard male-to-female jumper wires. No external resistors needed if you use the Pi’s internal pull-up resistors (we’ll set that in code).
Step 3: Simple, Easy-to-Understand Python Code
This code uses the RPi.GPIO library and has clear, commented logic so you can follow along. First, install the library if you haven’t:
pip install RPi.GPIO
Then save this as keyboard_scan.py:
import RPi.GPIO as GPIO import time # Update these lists to match YOUR row/column GPIO wiring ROW_PINS = [17, 27, 22, 5] COL_PINS = [6, 13, 19, 26] # This matches the standard 4x4 keyboard layout (adjust if yours is different) KEYBOARD_LAYOUT = [ ['1', '2', '3', 'A'], ['4', '5', '6', 'B'], ['7', '8', '9', 'C'], ['*', '0', '#', 'D'] ] def setup_gpio(): # Use BCM GPIO numbering (matches the numbers we used for wiring) GPIO.setmode(GPIO.BCM) # Set row pins as outputs, starting HIGH for row_pin in ROW_PINS: GPIO.setup(row_pin, GPIO.OUT) GPIO.output(row_pin, GPIO.HIGH) # Set column pins as inputs with internal pull-up resistors # This means columns stay HIGH until a key connects them to a LOW row for col_pin in COL_PINS: GPIO.setup(col_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) def scan_for_key(): pressed_key = None # Check each row one by one for row_index, row_pin in enumerate(ROW_PINS): # Pull the current row LOW GPIO.output(row_pin, GPIO.LOW) # Check each column to see if it's pulled LOW (key pressed) for col_index, col_pin in enumerate(COL_PINS): if GPIO.input(col_pin) == GPIO.LOW: # Wait for the key to be released to avoid duplicate readings while GPIO.input(col_pin) == GPIO.LOW: time.sleep(0.05) # Look up the key from our layout pressed_key = KEYBOARD_LAYOUT[row_index][col_index] # Pull the row back HIGH before moving to the next GPIO.output(row_pin, GPIO.HIGH) return pressed_key try: setup_gpio() print("Keyboard is ready! Press Ctrl+C to stop.") while True: key = scan_for_key() if key: print(f"You pressed: {key}") time.sleep(0.05) finally: # Clean up GPIO settings when we exit GPIO.cleanup()
How the Code Works
- Setup: We configure rows as outputs (starting high) and columns as inputs with pull-up resistors. Columns stay high unless a key connects them to a low row.
- Scanning: We pull one row low at a time, then check each column. If a column goes low, we know the key at that row/column intersection is pressed.
- Debouncing: The
whileloop waits for the key to be released, so you don’t get multiple "press" signals from one key press.
Quick Troubleshooting
- No keys register? Double-check your wiring matches the
ROW_PINS/COL_PINSin the code. Make sure you’re using BCM numbering (not BOARD). - False key presses? Increase the
time.sleep(0.05)value a little to add more debounce time, or check for loose wires. - Library errors? Make sure you installed
RPi.GPIOcorrectly, and run the script with sudo (if needed):sudo python3 keyboard_scan.py
内容的提问来源于stack exchange,提问作者Matthew Miles

