Built-in project · Raspberry Pi Pico W

Pico W 4x4 keypad on GP2 to GP9

MicroPython on a Pico W: a 4x4 keypad with rows on GP2 to GP5 and columns on GP6 to GP9 with pull-downs, scanned row by row, prints each key. The whole build, a Raspberry Pi Pico W 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.

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

The circuit runs here once the page has loaded.

What you'll learn

  • Scanning a keypad matrix row by row
  • Pull-down inputs
  • 16 keys on 8 pins
  • Printing only when something changes
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 2

How it works

A 4x4 keypad has 16 keys but only 8 wires. The rows go to GP2 to GP5 and the columns go to GP6 to GP9. Each column pin has a pull-down, so it reads 0 unless something pushes it high.

The code raises one row at a time. If a key on that row is held, it joins the row to its column, and that column reads high. Checking all four rows and four columns finds every held key. The code prints the keys it finds, but only when the set of held keys changes.

Parts list

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

Kits that can build it

These 2 starter kits have 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 R1Raspberry Pi Pico W pin GP2
Keypad, 4x4 pin R2Raspberry Pi Pico W pin GP3
Keypad, 4x4 pin R3Raspberry Pi Pico W pin GP4
Keypad, 4x4 pin R4Raspberry Pi Pico W pin GP5
Keypad, 4x4 pin C1Raspberry Pi Pico W pin GP6
Keypad, 4x4 pin C2Raspberry Pi Pico W pin GP7
Keypad, 4x4 pin C3Raspberry Pi Pico W pin GP8
Keypad, 4x4 pin C4Raspberry Pi Pico W pin GP9

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 W
# 4x4 keypad: the rows are on GP2 to GP5 and the columns on GP6 to GP9,
# each column held at 0 V by the Pico W's own pull-down (the kit uses a
# 10k on each). Raise one row at a time: a column
# that goes high has its key held on that row.
from machine import Pin
import time

KEYS = ["123A", "456B", "789C", "*0#D"]
rows = [Pin(n, Pin.OUT, value=0) for n in (2, 3, 4, 5)]
cols = [Pin(n, Pin.IN, Pin.PULL_DOWN) for n in (6, 7, 8, 9)]

def scan():
    held = []
    for r, row in enumerate(rows):
        row.value(1)
        for c, col in enumerate(cols):
            if col.value():
                held.append(KEYS[r][c])
        row.value(0)
    return held

last = []
while True:
    keys = scan()
    if keys and keys != last:
        print("key", " ".join(keys))
    last = keys
    time.sleep_ms(20)

Code walkthrough

The key functions of the sketch, and what each one does.

scan main.py

Goes through each row. It sets the row pin high, checks every column, and for each column that reads high it adds that key from KEYS to the held list. Then it sets the row low again and moves on. It returns the list of everything held right now.

def scan():
    held = []
    for r, row in enumerate(rows):
        row.value(1)
        for c, col in enumerate(cols):
            if col.value():
                held.append(KEYS[r][c])
        row.value(0)
    return held

Troubleshooting

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

  • Wrong key printed This project
    Check that R1 to R4 go to GP2 to GP5 and C1 to C4 go to GP6 to GP9, in order. The row and column numbers pick the letter from KEYS.
  • Keys seem to press themselves This project
    The columns need their pull-downs. The code turns on Pin.PULL_DOWN, so make sure the column wires are really on GP6 to GP9.
  • 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.
  • Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
    Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts.
  • WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
    Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.

Change it and keep it

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