Arduino Mega - Keypad 3x4
Welcome to this comprehensive Arduino Mega keypad 3x4 tutorial! In this detailed Arduino Mega 3x4 keypad guide, you'll discover how to integrate a professional membrane keypad into your Arduino Mega projects for secure password entry, numeric input, and menu navigation.
The 3x4 keypad module is a popular choice for adding user input capabilities to embedded systems. With 12 tactile buttons arranged in a compact 3-column by 4-row matrix, this keypad provides a familiar numeric interface similar to phone keypads and security systems. Throughout this Arduino Mega keypad tutorial, we'll master essential keypad programming techniques:
- Understanding how matrix keypads work and their internal scanning mechanism
- Step-by-step instructions for properly connecting a 3x4 keypad to your Arduino Mega microcontroller
- Programming techniques for detecting and reading key presses accurately using the Keypad library
- Implementing secure password verification systems with start and end delimiters
- Handling multiple-digit number input for calculator and data entry applications
This Arduino Mega 3x4 keypad project opens up exciting possibilities for secure access control systems, digital locks, calculators, menu-driven interfaces, ATM-style input systems, and any application requiring numeric or symbolic user input. The compact design and reliable membrane switch technology make 3x4 keypads perfect for both prototyping and finished products!

Hardware Preparation
| 1 | × | Arduino Mega | |
| 1 | × | USB 2.0 cable type A/B (for USB-A PC) | |
| 1 | × | USB 2.0 cable type C/B (for USB-C PC) | |
| 1 | × | Keypad 3x4 | |
| 1 | × | Jumper Wires |
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (30 sensors/displays) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of 3x4 Keypad
The 3x4 keypad (also called a matrix keypad) is a widely-used input device featuring 12 membrane push buttons arranged in a grid format with 3 columns and 4 rows. This configuration creates a standard numeric keypad layout familiar to anyone who has used a telephone, calculator, or ATM machine.
Each button on the keypad is positioned at the intersection of a row and column line. The clever matrix design reduces the number of required pins from 12 (one per button) down to just 7 (4 rows + 3 columns). This efficient wiring scheme works through a scanning process: the Arduino systematically sets each column HIGH or LOW while reading the state of each row. When a button is pressed, it creates an electrical connection between its specific row and column, allowing the Arduino to determine which key was activated.
The typical 3x4 keypad layout includes:
- Numbers 1-9 arranged in a 3x3 grid (like a phone)
- Bottom row featuring *, 0, and # symbols
- Durable membrane switches providing tactile feedback and long-lasting operation
- Flexible ribbon cable with 7-pin connector for easy integration
This keypad configuration is ideal for applications requiring numeric entry, simple command input, or password authentication where the * and # keys serve as special function buttons (typically "clear" and "enter" respectively).
Pinout
Understanding the 3x4 keypad pinout is essential for proper wiring. The keypad uses a matrix configuration with seven pins organized into two functional groups:
Row Pins (4 pins):
- R1 (Row 1): Connects to keys 1, 2, 3
- R2 (Row 2): Connects to keys 4, 5, 6
- R3 (Row 3): Connects to keys 7, 8, 9
- R4 (Row 4): Connects to keys *, 0, #
Column Pins (3 pins):
- C1 (Column 1): Connects to keys 1, 4, 7, *
- C2 (Column 2): Connects to keys 2, 5, 8, 0
- C3 (Column 3): Connects to keys 3, 6, 9, #

How Matrix Scanning Works: Each key sits at the intersection of one row and one column. When you press a key, it physically connects that row to that column. The Arduino detects which key was pressed by cycling through columns (setting each HIGH one at a time) while monitoring all rows. When a key is pressed, the HIGH signal from the active column appears on the corresponding row, revealing the key's position in the matrix. This scanning happens so quickly (thousands of times per second) that it appears instantaneous to users.
Important Note: The pin labels may vary slightly between manufacturers. Some keypads label pins from left to right, others from right to left. Always verify your specific keypad's pinout using the datasheet or by testing with a multimeter.
Wiring Diagram
Now let's examine how to properly connect your 3x4 keypad to the Arduino Mega. This wiring uses seven digital pins to interface with the keypad's row and column matrix:

This image is created using Fritzing. Click to enlarge image
Connection Summary:
- Keypad R1 (Row 1) → Arduino Mega Digital Pin (configured in code)
- Keypad R2 (Row 2) → Arduino Mega Digital Pin (configured in code)
- Keypad R3 (Row 3) → Arduino Mega Digital Pin (configured in code)
- Keypad R4 (Row 4) → Arduino Mega Digital Pin (configured in code)
- Keypad C1 (Column 1) → Arduino Mega Digital Pin (configured in code)
- Keypad C2 (Column 2) → Arduino Mega Digital Pin (configured in code)
- Keypad C3 (Column 3) → Arduino Mega Digital Pin (configured in code)
No Power Required: Unlike many sensors, the keypad doesn't need separate VCC or GND connections—it operates entirely through the digital I/O pins. The Arduino Mega has plenty of digital pins available, so you can choose any convenient pin assignments and define them in your code.
Arduino Mega Code
Detailed Instructions
Follow these detailed steps carefully to get your keypad working:
Step 1 - Physical Connection: Connect the Arduino Mega to the 3x4 keypad using the wiring diagram shown above. Ensure each of the 7 keypad pins connects to the correct Arduino digital pin.
Step 2 - USB Connection: Connect the Arduino Mega board to your computer using a USB cable. Wait for your operating system to recognize the device.
Step 3 - Launch Arduino IDE: Open the Arduino IDE software on your computer. If you haven't installed it yet, download it from the official Arduino website.
Step 4 - Board Configuration: Navigate to Tools > Board and select "Arduino Mega or Mega 2560". Then go to Tools > Port and select the appropriate COM port.
Step 5 - Open Library Manager: Click the Libraries icon (book icon) on the left sidebar of the Arduino IDE to access the Library Manager.
Step 6 - Search for Library: In the search box at the top, type "DIYables_Keypad" and locate the keypad library created by DIYables.io. This library simplifies keypad interfacing and handles all the matrix scanning automatically.
Step 7 - Install Library: Click the Install button to add the DIYables_Keypad library to your Arduino IDE. Wait for installation to complete.

Step 8 - Load Code: Copy the code example provided above and open it in the Arduino IDE editor.
Step 9 - Upload Program: Click the Upload button (right-arrow icon) in the Arduino IDE to compile and transfer the code to your Arduino Mega. Wait for the "Done uploading" message.
Step 10 - Open Serial Monitor: Open the Serial Monitor (Tools > Serial Monitor or Ctrl+Shift+M) to view the keypad output.
Step 11 - Test Keypad: Press various keys on the keypad to test functionality.
Step 12 - View Results: Check the Serial Monitor to see which keys are being detected.
Pro Tip: If certain keys aren't responding or multiple keys register for a single press, double-check your wiring connections. The most common issue is reversed row/column connections or loose jumper wires. Also verify that your pin definitions in the code match your physical wiring.
Keypad and Password
One of the most popular applications for keypads is secure password entry systems. Think of door locks, safes, security panels, and access control systems—they all rely on keypad-based password authentication. Let's explore how to implement a robust password system using our 3x4 keypad.
Understanding Password Entry Logic
When designing a keypad password system, we need to designate special function keys that control the password entry process. In a standard 3x4 keypad layout, we typically assign:
- The "*" (asterisk) key: Functions as the CLEAR or RESET button. Pressing this key clears the current password entry, allowing the user to start over if they make a mistake. This is crucial for user-friendly operation.
- The "#" (hash/pound) key: Functions as the ENTER or SUBMIT button. Pressing this key tells the system "I'm done typing, please verify my password." This triggers the password comparison logic.
- All other keys (0-9): These form the actual password characters. Each press adds that digit to the password buffer being built in memory.
Password Entry Flow
Here's how the password entry process works step-by-step:
When any key is pressed:
- If the key is a digit (0-9): Append that character to the current password string being entered. This builds the password one character at a time as the user types.
- If the key is "#" (ENTER):
- Compare the entered password string against the predefined correct password stored in the program
- If they match exactly, grant access (display "password is correct" or trigger a relay/door lock)
- If they don't match, deny access (display "password is incorrect, try again")
- Clear the entered password buffer to prepare for the next attempt
- Immediately clear the current password buffer
- Reset the entry process so the user can start typing fresh
- This is useful when the user realizes they made an error mid-entry
Security Considerations
For enhanced security, consider these best practices:
- Don't echo the actual password characters to Serial Monitor in production code
- Implement a lockout mechanism after multiple failed attempts
- Use timeouts to auto-clear passwords if entry takes too long
- Store passwords in EEPROM or use hashing for better security
Keypad - Password Code
This example demonstrates a complete password verification system. The code defines a secret password and compares user input against it, providing feedback on whether access is granted or denied.
Testing the Password System
Step 1 - Upload Code: Upload the password verification code shown above to your Arduino Mega.
Step 2 - Open Serial Monitor: Open the Serial Monitor to view password verification results.
Step 3 - Test Incorrect Password: Type the keys "123456" on the keypad (an incorrect password), then press the "#" key to submit.
Step 4 - Test Correct Password: Type the keys "1234" on the keypad (the correct password defined in the code), then press the "#" key to submit.
Step 5 - Test Clear Function: Type some digits, press "*" to clear, then type a password and press "#" to verify the clear function works.
Step 6 - View Results: Check the Serial Monitor to see password verification messages.
Understanding the Code: In this example, the correct password is typically set as "1234" (check the code for the exact password). When you type "123456" and press #, the system detects that "123456" ≠ "1234" and displays "password is incorrect." When you type "1234" and press #, the strings match exactly, and the system displays "password is correct."
Application Ideas: This password verification system can be adapted for:
- Electronic door locks with relay control
- Safe or cabinet access systems
- Device activation/deactivation controls
- Multi-user systems with different password levels
- PIN-based security panels for home automation
Additional Knowledge
Ready to take your keypad projects to the next level? Explore these advanced tutorials that build upon the fundamentals you've learned:
- How to use the multiple passwords for keypad - Learn to implement systems with different user passwords, perfect for multi-user access control or privilege levels.
- How to input a multiple digits number using the keypad - Master the technique of collecting numeric input for calculators, data entry, or parameter configuration.
These resources provide complete code examples and detailed explanations to help you create more sophisticated keypad-based applications!