Board and part

Raspberry Pi Pico Keypad Simulator: Wiring, Code and a Live Circuit

This is a 4x4 membrane keypad wired to a Raspberry Pi Pico and scanned live with the Keypad library: open it in the editor, press a key, and the serial monitor prints it.

  • 3.3 V logic
  • Library: Keypad.h
  • 8 pins, no resistors
  • Debounced in the library
Raspberry Pi Pico · Keypadlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Connections

Wiring the Keypad to the Raspberry Pi Pico

Eight wires and nothing else: the rows go to 2, 3, 6, 7 and the columns to 8, 9, 10, 11.

Every connection in the circuit above, with the board’s own pin names.

Keypad pinRaspberry Pi Pico pinWhy
R1, R2, R3, R4GP2 (physical pin 4), GP3 (physical pin 5), GP6 (physical pin 9), GP7 (physical pin 10)The rows: the library drives one low at a time.
C1, C2, C3, C4GP8 (physical pin 11), GP9 (physical pin 12), GP10 (physical pin 14), GP11 (physical pin 15)The columns: inputs with pull-ups, read for each row.

Power

A membrane keypad has no power pin: it is sixteen switches at the crossings of four row wires and four column wires. Eight pins of the Raspberry Pi Pico scan it, using the chip’s internal pull-ups, so no resistors are needed.

Pins to leave alone on the Raspberry Pi Pico

The numbers in the code are GP numbers, not the physical pin numbers printed beside the header: GP15 is pin 20. GP23 to GP25 are used on the board itself, and GP25 is the LED.

On a real bench

Solder headers on the Pico and press it into the breadboard across the center channel; the Keypad goes in beside it. GND pins sit every fifth pin down both sides (pins 3, 8, 13, 18 and so on), so there is always one close. The first upload needs BOOTSEL held while plugging in.

The sketch

The code, and what it does.

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

4x4 keypad on Raspberry Pi Pico
#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'},
};
byte rowPins[ROWS] = {2, 3, 6, 7};   // R1 to R4
byte colPins[COLS] = {8, 9, 10, 11};   // C1 to C4

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  Serial.begin(115200);
  Serial.println("Press a key");
}

void loop() {
  char key = keypad.getKey();   // 0 when nothing new was pressed
  if (key) {
    Serial.print("Key: ");
    Serial.println(key);
  }
}

makeKeymap(keys) tells the library which character sits at each crossing. rowPins and colPins list the board pins in the order of the keypad’s connector, R1 to R4 and C1 to C4.

getKey() scans the matrix: it pulls each row low in turn and reads the four columns, so a column that reads low is a key held in that row. It returns the key once, when it is first pressed, with a debounce built in, and 0 otherwise.

Serial.begin(115200) opens the serial monitor. Serial is the USB port at 115200 baud in this sketch; on a real Pico it appears once the board has enumerated.

Pitfalls

Common mistakes with the Keypad on the Raspberry Pi Pico

Rows and columns swapped

Swap the two arrays and every key prints its mirror across the diagonal, so 2 prints 4. If the wrong keys come out in a pattern, swap rowPins and colPins.

Pins listed in the wrong order

The connector order is R1 to R4 then C1 to C4 on most keypads, left to right with the keys facing you. List the pins in exactly that order or keys move around.

Running out of pins

A 4x4 keypad takes eight pins, which on the Raspberry Pi Pico leaves little room. An I2C port expander such as the PCF8574 scans it over two wires instead.

FAQ

Questions and answers

Can I simulate a keypad with a Raspberry Pi Pico?

Yes. The circuit at the top of this page is a Raspberry Pi Pico with a 4x4 keypad, running in your browser on a simulated RP2040. Click a key to press it; the simulated keypad connects that row to that column the way the membrane does. Press Run it in the editor to change the wiring or the code; it is free and needs no account.

Can I detect two keys at once?

Keypad.h has getKeys() for that, and up to about three keys work. More can ghost: a missing key appears to be pressed because of the paths through the others.

How do I make a password lock?

Collect the keys into a string until # is pressed, compare it with the code, and drive a relay or a servo when it matches. The keypad lock project does exactly that.

Does a 4x3 keypad work the same way?

Yes, with COLS set to 3, three column pins and a 4 by 3 keys array. The simulator has a 4x3 keypad part too.

Build your own Keypad project

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