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 2560 | 4×4 matrix membrane keypad | Wire | Notes |
|---|---|---|---|
| D22 | R1 | orange | |
| D24 | R2 | orange | |
| D26 | R3 | orange | |
| D28 | R4 | orange | |
| D30 | C1 | blue | |
| D32 | C2 | blue | |
| D34 | C3 | blue | |
| D36 | C4 | blue |
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
floatfor decimals.
Code
// 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(" / "));
}
}// 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
Calculator: A+ B- Cx D/ #= *clear
12 + 30 = 42
42 x 3 = 126Calculator: 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.