Wiring guide

How to wire a 4x4 keypad to an Arduino Mega

To wire a 4x4 keypad to an Arduino Mega, connect R1, R2, R3, R4 to pin 9, pin 8, pin 7, pin 6; and C1, C2, C3, C4 to pin 5, pin 4, pin 3, pin 2. A membrane keypad is sixteen switches with no power pin, so nothing on it can exceed the Arduino Mega’s own 5 V levels and nothing needs shifting. The sketch below uses the Keypad (Keypad.h) library. It scans the rows and columns and prints each key you press.

  • 5 V logic
  • Library: Keypad.h
  • 8 wires
Arduino Mega · 4x4 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.
Pin to pin

4x4 keypad to Arduino Mega wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

4x4 keypad pinArduino Mega pinWire colorWhy
R1, R2, R3, R4pin 9, pin 8, pin 7, pin 6amberThe rows: the library drives one low at a time.
C1, C2, C3, C4pin 5, pin 4, pin 3, pin 2greenThe columns: inputs with pull-ups, read for each row.
Voltage

Power and logic levels for 4x4 keypad and Arduino Mega

Logic level

The Arduino Mega’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.

This pair

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 Arduino Mega scan it, using the chip’s internal pull-ups, so no resistors are needed.

Pins to leave alone

Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.

The sketch

The working code for the Arduino Mega

Library: Keypad (Keypad.h). This is the sketch the circuit above runs.

4x4 keypad on Arduino Mega
#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};   // R1 to R4
byte colPins[COLS] = {5, 4, 3, 2};   // C1 to C4

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

void setup() {
  Serial.begin(9600);
  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);
  }
}
Output

What you should see

Open the serial monitor at 9600 baud. The sketch prints Press a key and then a Key line with the character of each key you press. The library debounces the keys itself.

If the wrong characters come out, the rows and columns are swapped: R1 to R4 must go to the row pins in the sketch, C1 to C4 to the column pins.

The serial monitor prints this, recorded from the circuit above with the 5 key pressed after 300 ms:

Press a key
Key: 5
Pitfalls

Common mistakes wiring a 4x4 keypad to an Arduino Mega

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.

Using pins 0 and 1

With 54 pins it is tempting to use any eight. Pins 0 and 1 are the USB serial port, so a keypad there corrupts what the serial monitor shows. The circuit uses 2 to 9 for that reason.

Build your own 4x4 keypad project

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