Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.

4×4 keypad with Arduino Mega 2560 — a calculator on pins 22–36

Wire a 4×4 membrane keypad to the Arduino Mega 2560’s double-row header (pins 22–36) and build a four-function calculator that prints to the Serial Monitor.

Arduino C++Verified — core avr 1.8.8, Keypad 3.1.1, 3 Nov 2026Compile-only

Community draft — report a mistake

The Mega has 54 digital pins, so a keypad’s eight wires are no longer a big decision. This guide puts them on the double-row header (22–36), out of the way of everything else.

The sketch turns the keypad into a small calculator: digits, A = +, B = −, C = ×, D = ÷, # = equals, * = clear. Results print in the Serial Monitor; add an LCD to make it standalone.

Wiring

Arduino Mega 2560Only the pins used are shown · 5 V logic4×4 matrix membrane keypadD22D24D26D28D30D32D34D36R1R2R3R4C1C2C3C4
Every connection
Arduino Mega 25604×4 matrix membrane keypadWireNotes
D22R1orange
D24R2orange
D26R3orange
D28R4orange
D30C1blue
D32C2blue
D34C3blue
D36C4blue

No supply — passive switch matrix part, 5 V board. The keypad has no electronics, so it works at 3.3 V or 5 V. It does need eight GPIOs; on ESP32 avoid the strapping pins (0, 2, 5, 12, 15) and the input-only pins (34–39) for the row outputs.

Board quirks

  • On the double-row header, even pins (22, 24, 26 …) are one row and odd pins the other. Using one row means an 8-wire ribbon lines up with 22–36.
  • Putting the keypad there keeps the PWM pins (2–13), the serial ports (14–19) and I2C (20/21) free for a display, motors and modules.
  • No resistors: the Keypad library enables the internal pull-ups.
  • Integer maths only: 7 / 2 = 3. Use float for decimals.

Code

keypad-mega.inoArduino C++Open in PlaygroundOpen in Playground
// 4x4 keypad on an Arduino Mega 2560's double-row header as a 4-function calculator.
// R1-R4 → D22, D24, D26, D28; C1-C4 → D30, D32, D34, D36 (one side of the header).
// A = +, B = -, C = x, D = /, # = equals, * = clear.
#include <Keypad.h>

const byte ROWS = 4, COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'},
};
byte rowPins[ROWS] = {22, 24, 26, 28};
byte colPins[COLS] = {30, 32, 34, 36};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

long acc = 0, entry = 0;
char op = 0;

long apply(long a, char o, long b) {
  switch (o) {
    case 'A': return a + b;
    case 'B': return a - b;
    case 'C': return a * b;
    case 'D': return b ? a / b : 0;
    default: return b;
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println(F("Calculator: A+ B- Cx D/  #=  *clear"));
}

void loop() {
  char k = keypad.getKey();
  if (!k) return;
  if (k >= '0' && k <= '9') {
    entry = entry * 10 + (k - '0');
    Serial.print(k);
  } else if (k == '*') {
    acc = entry = 0;
    op = 0;
    Serial.println(F("\nCleared"));
  } else if (k == '#') {
    acc = apply(acc, op, entry);
    Serial.print(F(" = "));
    Serial.println(acc);
    entry = 0;
    op = 0;
  } else {  // A-D
    acc = apply(acc, op, entry);
    entry = 0;
    op = k;
    Serial.print(k == 'A' ? F(" + ") : k == 'B' ? F(" - ") : k == 'C' ? F(" x ") : F(" / "));
  }
}

Library: Keypad (Mark Stanley, Alexander Brevig) — compiled with Keypad 3.1.1.

Expected serial output

Serial Monitor · 115200Output
Calculator: A+ B- Cx D/  #=  *clear
12 + 30 = 42
42 x 3 = 126

Common errors

Some keys print the wrong character

Rows and columns swapped. Print each key and adjust rowPins/colPins.

Nothing happens on key presses

Ribbon on the odd row (23, 25 …) instead of the even row. Move it or change the pin arrays.

↑↓ to move · Enter to open