Built-in project · Arduino Uno R3

Membrane keypad on pins 9 to 2, on an Arduino Uno

A 4x4 membrane keypad with its rows on pins 9, 8, 7 and 6 and its columns on 5, 4, 3 and 2: the Keypad library prints every key that is pressed. The whole build, an Arduino Uno 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.

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

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

The circuit runs here once the page has loaded.

What you'll learn

  • How a keypad matrix saves pins
  • Using the Keypad library
  • A 2D array as a key map
  • Reading a press with getKey
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

A 4x4 membrane keypad has sixteen keys on eight wires. The rows R1 to R4 go to pins 9, 8, 7 and 6. The columns C1 to C4 go to pins 5, 4, 3 and 2. Each key joins one row wire to one column wire when pressed.

The Keypad library checks those crossings and tells the sketch which key closed. The keys table holds the character for each crossing. Each press prints "key: " and the character to the serial monitor.

Parts list

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

Kits that can build it

These 4 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 R1Arduino Uno R3 pin 9
Keypad, 4x4 pin R2Arduino Uno R3 pin 8
Keypad, 4x4 pin R3Arduino Uno R3 pin 7
Keypad, 4x4 pin R4Arduino Uno R3 pin 6
Keypad, 4x4 pin C1Arduino Uno R3 pin 5
Keypad, 4x4 pin C2Arduino Uno R3 pin 4
Keypad, 4x4 pin C3Arduino Uno R3 pin 3
Keypad, 4x4 pin C4Arduino Uno R3 pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A 4x4 membrane keypad: every key you press, printed.
//
//   keypad rows R1 to R4    -> pins 9, 8, 7, 6
//   keypad columns C1 to C4 -> pins 5, 4, 3, 2
//
// Sixteen keys on eight wires. Each key joins one row wire to one column wire
// when it is pressed. The Keypad library drives the columns low one at a time
// and reads the rows, so it can tell exactly which crossing closed.
//
// Click a key on the canvas.

#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] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

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

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: keypad, rows on 9 to 6, columns on 5 to 2. Press a key.");
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    Serial.print("key: ");
    Serial.println(key);
  }
}

Code walkthrough

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

setup sketch.ino

Starts serial at 9600 and prints a note about the pins. The keypad object was made earlier from the keys map, rowPins (9, 8, 7, 6) and colPins (5, 4, 3, 2).

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: keypad, rows on 9 to 6, columns on 5 to 2. Press a key.");
}

loop sketch.ino

Asks keypad.getKey() for a key. If one was pressed, it prints "key: " followed by the character. If not, key is 0 and nothing happens.

void loop() {
  char key = keypad.getKey();
  if (key) {
    Serial.print("key: ");
    Serial.println(key);
  }
}

Troubleshooting

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

  • The wrong characters show up This project
    The pin arrays must match the wiring order. R1 is pin 9 and C1 is pin 5.
  • Nothing prints This project
    Check that all eight keypad wires are connected, then click a key on the canvas.
  • 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.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

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