A 3x4 keypad gives you 12 buttons - digits 0-9 plus * and # - using only 7 wires instead of 12, which makes it a cheap way to add numeric input to an Arduino Mega project like a door lock, alarm panel, or menu-driven device. This walks through wiring one up and reading key presses, then building a simple password check on top of it.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Mega - Keypad 3x4
What you need
- Arduino MEGA
- DIYables MEGA Development Board
- USB 2.0 cable type A/B (for USB-A PC)
- USB 2.0 cable type C/B (for USB-C PC)
- Keypad 3x4
- Jumper Wires
- Screw Terminal Block Shield for Arduino Uno/Mega
- Sensors/Servo Expansion Shield for Arduino Mega
- Breadboard Shield for Arduino Mega
- Enclosure for Arduino Mega
- DIYables Sensor Kit (18 sensors/displays)
Wiring
The keypad has 7 pins: four rows (R1-R4) and three columns (C1-C3). R1 covers keys 1, 2, 3; R2 covers 4, 5, 6; R3 covers 7, 8, 9; R4 covers *, 0, #. C1 covers 1, 4, 7, *, and the other columns follow the same pattern across the grid. In the sketch, the four row pins connect to Mega digital pins 14, 15, 16, 17, and the three column pins connect to 18, 19, 20. No external resistors are needed, the DIYables_Keypad library handles the pin states internally.
How it works
Each button sits at the intersection of one row wire and one column wire, with no permanent connection between them. The Arduino scans the matrix by driving each column and checking which row reads active, which is how it can tell 12 buttons apart using only 7 pins instead of wiring each button separately. Pressing a key briefly bridges its row and column, and that unique row/column combination maps to a single character.
Code
The sketch defines the key layout as a 4x3 character grid matching the physical keypad, then tells DIYables_Keypad which Mega pins are rows and which are columns. In loop(), keypad.getKey() returns the pressed character or nothing if no key is down, and the example just prints whatever key it reads to Serial. For a password system, you'd build on this: append digits to a string as they come in, treat * as a clear button, and treat # as submit, comparing the typed string against a stored password when # is pressed.
arduino-mega-keypad-3x4.ino
#include <DIYables_Keypad.h> // DIYables_Keypad library
const int ROW_NUM = 4; // four rows
const int COLUMN_NUM = 3; // three columns
char keys[ROW_NUM][COLUMN_NUM] = {
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' },
{ '*', '0', '#' }
};
byte pin_rows[ROW_NUM] = {14, 15, 16, 17}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {18, 19, 20}; //connect to the column pinouts of the keypad
DIYables_Keypad keypad = DIYables_Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
void setup() {
Serial.begin(9600);
delay(1000);
Serial.println("Keypad 3x4 example");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
}
}
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-mega/arduino-mega-keypad-3x4

Top comments (0)