Built-in project · Raspberry Pi Pico

MicroPython 4x4 keypad on a Pico, GP13 to GP6

MicroPython on a Raspberry Pi Pico: a 4x4 keypad with its rows on GP13 to GP10 and columns on GP9 to GP6 prints each key as it is pressed. The whole build, a Raspberry Pi Pico and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $5.50. On Mokxi they're free.

MicroPython 4x4 keypad on a Pico, GP13 to GP6 · Raspberry Pi Picolive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • How a matrix keypad scans rows and columns
  • Pull-down resistors on input pins
  • MicroPython Pin setup
  • Splitting code into a module and a main file
  • Reporting a key once per press
  • Difficulty: Intermediate
  • Time: About 15 minutes
  • Board: Raspberry Pi Pico
  • Parts: 2

How it works

A 4x4 keypad has 16 keys in a grid of 4 rows and 4 columns, so it uses 8 pins. The rows are on GP13 to GP10 and the columns on GP9 to GP6.

In keypad.py, the Pico drives one row high at a time. The columns are pulled down. When a key is pressed, it joins its row to its column, so that column reads high. The row and column together pick the character.

main.py scans every 20 ms and prints each key once per press, for example key 7. Holding the key down prints nothing more.

Parts list

2 parts, plus the jumper wires. They retail for about $5.50; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Keypad, 4x4 pin C4Raspberry Pi Pico pin GP6
Keypad, 4x4 pin C3Raspberry Pi Pico pin GP7
Keypad, 4x4 pin C2Raspberry Pi Pico pin GP8
Keypad, 4x4 pin C1Raspberry Pi Pico pin GP9
Keypad, 4x4 pin R4Raspberry Pi Pico pin GP10
Keypad, 4x4 pin R3Raspberry Pi Pico pin GP11
Keypad, 4x4 pin R2Raspberry Pi Pico pin GP12
Keypad, 4x4 pin R1Raspberry Pi Pico pin GP13

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico
# A 4x4 keypad: rows on GP13 to GP10, columns on GP9 to GP6. keypad.py,
# in the next tab, scans it; each key is printed once per press.
import time
from keypad import Keypad

keys = Keypad(rows=(13, 12, 11, 10), cols=(9, 8, 7, 6))
print("Press a key")

while True:
    key = keys.scan()
    if key:
        print("key", key)
    time.sleep_ms(20)
keypad.py · Raspberry Pi Pico
# keypad.py: a 4x4 matrix keypad.
# Each row is driven high in turn; a held key joins its row to its column,
# so that column reads high. The columns are pulled down when nothing is held.
from machine import Pin
import time

KEYS = ("123A", "456B", "789C", "*0#D")

class Keypad:
    def __init__(self, rows, cols):
        self.rows = [Pin(n, Pin.OUT, value=0) for n in rows]
        self.cols = [Pin(n, Pin.IN, Pin.PULL_DOWN) for n in cols]
        self.last = None

    def held(self):
        for r, row in enumerate(self.rows):
            row.value(1)
            time.sleep_us(50)
            for c, col in enumerate(self.cols):
                if col.value():
                    row.value(0)
                    return KEYS[r][c]
            row.value(0)
        return None

    def scan(self):
        """The key just pressed, once per press, or None."""
        key = self.held()
        pressed = key if key != self.last else None
        self.last = key
        return pressed

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The wrong character prints This project
    Check the row and column order in the Keypad call: rows 13, 12, 11, 10 and columns 9, 8, 7, 6 must match the R1 to R4 and C1 to C4 pins.
  • Nothing prints when you press a key This project
    Wait for "Press a key" in the console first. Also check that keypad.py is present in its own tab.
  • Pressing one key reports several keys, or the wrong key. Keypad
    Match the keymap and the row and column pin order to your keypad's pinout. Without diodes, pressing three keys at once can ghost a fourth.
  • No keys are detected. Keypad
    Rows need pull-ups, or use INPUT_PULLUP, while one column is driven low at a time. The Keypad library does this for you if you pass the pins in the right order.
  • The Pico does not show up as a drive, or the program will not load. Raspberry Pi Pico
    Hold the BOOTSEL button while you plug in USB. A drive named RPI-RP2 appears. Then copy the UF2 file onto it.
  • A 5 V signal or sensor is wired to a GPIO pin. Raspberry Pi Pico
    Pico GPIO pins are 3.3 V only and are not 5 V tolerant. Use a level shifter or a voltage divider for any 5 V output going into the board.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.