Matrix Scanning Principle
Why Matrix Configuration?
A 4×4 keypad has 16 keys. Without matrix scanning, we'd need 16 individual input pins. With matrix scanning, we only need 8 pins (4 rows + 4 columns), saving 50% of I/O resources.
General Formula:
For an M×N keypad:
- Direct wiring: M × N pins required
- Matrix scanning: M + N pins required
- Pin savings increase with larger keypads
Example Savings:
A 4×4 keypad has 16 keys. Without matrix scanning, we'd need 16 individual input pins. With matrix scanning, we only need 8 pins (4 rows + 4 columns), saving 50% of I/O resources.
General Formula:
For an M×N keypad:
- Direct wiring: M × N pins required
- Matrix scanning: M + N pins required
- Pin savings increase with larger keypads
Example Savings:
- 3×4 keypad: 12 keys → 7 pins (vs 12 pins)
- 4×4 keypad: 16 keys → 8 pins (vs 16 pins)
- 8×8 keypad: 64 keys → 16 pins (vs 64 pins)
Standard 4×4 Keypad Layout
1
2
3
A
4
5
6
B
7
8
9
C
*
0
#
D
Matrix Structure
Internal Keypad Structure (Simplified):
COL1 COL2 COL3 COL4
| | | |
ROW1 -----+--●--+--●--+--●--+--●
| [1] | [2] | [3] | [A]
| | | |
ROW2 -----+--●--+--●--+--●--+--●
| [4] | [5] | [6] | [B]
| | | |
ROW3 -----+--●--+--●--+--●--+--●
| [7] | [8] | [9] | [C]
| | | |
ROW4 -----+--●--+--●--+--●--+--●
| [*] | [0] | [#] | [D]
Each ● represents a momentary switch (key)
When pressed, it connects the row to the column
Scanning Algorithm
Step-by-Step Scanning Process:
If Row 2 is set LOW and Column 3 reads LOW → Key "6" is pressed
- Initialize: Set all row pins as OUTPUT (HIGH), all column pins as INPUT with pull-up resistors (HIGH by default)
- Scan: For each row:
- Set that row LOW, keep others HIGH
- Read all column pins
- If any column reads LOW, key at (row, col) is pressed
- Set row back to HIGH
- Repeat: Continuously scan all rows (typically 10-100 times per second)
- Debounce: Ignore rapid changes to filter mechanical bounce
If Row 2 is set LOW and Column 3 reads LOW → Key "6" is pressed
Timing Diagram
When Key "5" is pressed (Row 2, Column 2):
Time: t0 t1 t2 t3 t4
| | | | |
Row 1: ──┐ ┌───────────────────
└───┘
Row 2: ──────┐ ┌───────────────
└───┘ ↑ Set LOW
Row 3: ──────────────┐ ┌───────
└───┘
Row 4: ────────────────────┐ ┌─
└───┘
Col 2: ──────┐ ┌───────────
└───────┘ ↓ Reads LOW
Key "5" detected!
Electrical Characteristics
Key Switch Specifications
| Parameter | Typical Value |
|---|---|
| Contact Resistance (Closed) | < 100Ω |
| Insulation Resistance (Open) | > 100MΩ |
| Operating Force | 100-200 gf (gram-force) |
| Operating Travel | 0.5-1.0 mm |
| Life Expectancy | 1-10 million operations |
| Bounce Time | 5-20 ms |
Debouncing Requirements
Contact Bounce:
When a key is pressed, mechanical contacts don't close cleanly. They "bounce" (make and break contact rapidly) for 5-20ms before settling.
Software Debounce Implementation:
Hardware Debounce (RC Filter):
R = 10kΩ, C = 0.1µF
Time constant τ = R × C = 1ms
Settling time ≈ 5τ = 5ms
However, software debouncing is preferred for matrix keypads as the Keypad library handles it automatically.
When a key is pressed, mechanical contacts don't close cleanly. They "bounce" (make and break contact rapidly) for 5-20ms before settling.
Software Debounce Implementation:
1. Detect key press
2. Wait 10-20ms
3. Read key again
4. If still pressed → valid
5. If not pressed → ignore (was noise)
Hardware Debounce (RC Filter):
R = 10kΩ, C = 0.1µF
Time constant τ = R × C = 1ms
Settling time ≈ 5τ = 5ms
However, software debouncing is preferred for matrix keypads as the Keypad library handles it automatically.
Pull-up Resistor Analysis
Why Pull-up Resistors?
Column pins need a defined HIGH state when no key is pressed. Pull-up resistors (10kΩ typical) ensure pins don't "float" at undefined voltages.
Current Calculation:
When key is pressed (column connected to LOW row):
I = Vcc / Rpullup = 5V / 10kΩ = 0.5mA
This is well within Arduino's 40mA pin limit and provides reliable detection.
Internal vs External Pull-ups:
Arduino has internal pull-up resistors (~20-50kΩ) that can be enabled with INPUT_PULLUP mode. These are sufficient for keypad use and eliminate the need for external resistors.
Column pins need a defined HIGH state when no key is pressed. Pull-up resistors (10kΩ typical) ensure pins don't "float" at undefined voltages.
Current Calculation:
When key is pressed (column connected to LOW row):
I = Vcc / Rpullup = 5V / 10kΩ = 0.5mA
This is well within Arduino's 40mA pin limit and provides reliable detection.
Internal vs External Pull-ups:
Arduino has internal pull-up resistors (~20-50kΩ) that can be enabled with INPUT_PULLUP mode. These are sufficient for keypad use and eliminate the need for external resistors.
Advanced Topics
Multi-Key Detection
Limitation: Simple matrix scanning cannot reliably detect more than 2 simultaneous key presses due to "ghosting."
Ghosting Phenomenon:
If keys (R1,C1), (R1,C2), and (R2,C1) are pressed, the scanner may falsely detect (R2,C2) as pressed due to current paths through the matrix.
Solutions:
Ghosting Phenomenon:
If keys (R1,C1), (R1,C2), and (R2,C1) are pressed, the scanner may falsely detect (R2,C2) as pressed due to current paths through the matrix.
Solutions:
- Blocking Diodes: Add diode in series with each switch (1N4148) to prevent reverse current. Increases cost and complexity.
- Software: Ignore simultaneous presses or implement rollover algorithms
- Design: For most applications (calculators, PIN entry, phone dialers), single-key operation is sufficient
Scan Rate Optimization
Scan Rate Requirements:
Human typing speed: ~5-10 keys/second
Required detection rate: >20 Hz (Nyquist theorem)
Typical implementation: 50-100 Hz
Timing Analysis:
Per-row scan time: ~1ms (including debounce)
Total scan cycle (4 rows): 4ms
Scan frequency: 1000ms / 4ms = 250 Hz ✓
This provides plenty of margin for reliable detection.
Human typing speed: ~5-10 keys/second
Required detection rate: >20 Hz (Nyquist theorem)
Typical implementation: 50-100 Hz
Timing Analysis:
Per-row scan time: ~1ms (including debounce)
Total scan cycle (4 rows): 4ms
Scan frequency: 1000ms / 4ms = 250 Hz ✓
This provides plenty of margin for reliable detection.
Power Consumption
Idle State (no keys pressed):
Pull-up current per column: I = 5V / 50kΩ = 0.1mA (internal pullup)
4 columns × 0.1mA = 0.4mA total
Active State (one key pressed):
Additional current: ~0.5mA (through closed switch)
Total: ~0.9mA
Battery Life Estimate:
CR2032 coin cell: 220mAh
Continuous scanning: 220mAh / 0.9mA = 244 hours (10 days)
With sleep mode (1% duty cycle): 24,400 hours (3 years)
Pull-up current per column: I = 5V / 50kΩ = 0.1mA (internal pullup)
4 columns × 0.1mA = 0.4mA total
Active State (one key pressed):
Additional current: ~0.5mA (through closed switch)
Total: ~0.9mA
Battery Life Estimate:
CR2032 coin cell: 220mAh
Continuous scanning: 220mAh / 0.9mA = 244 hours (10 days)
With sleep mode (1% duty cycle): 24,400 hours (3 years)
Keypad Library Internals
Keypad Library Features:
- Automatic matrix scanning
- Built-in debouncing (10ms default)
- Event detection (PRESSED, RELEASED, HOLD)
- Key state management
- Multiple keypad support
- IDLE: No key activity
- PRESSED: Key just pressed
- HOLD: Key held down (after hold time threshold)
- RELEASED: Key just released
Practical Applications
Common Use Cases
| Application | Keypad Type | Example |
|---|---|---|
| Security Systems | 3×4, 4×4 | PIN entry, alarm codes |
| Telephones | 3×4 | Dialers, intercoms |
| Calculators | 4×4, 4×5 | Scientific, basic calculators |
| Point-of-Sale | 4×4, 5×4 | Payment terminals, registers |
| Industrial Control | 4×4, custom | Machine interfaces, HMI |
| Gaming | Custom matrices | Arcade buttons, controllers |
| Music | Custom sizes | MIDI controllers, drum pads |
Design Considerations
Layout Design:
- Standard layouts improve usability (phone, calculator familiar to users)
- Key spacing: 15-20mm for comfortable pressing
- Tactile feedback improves user experience
- Backlit keys for low-light environments
- Choose switches with appropriate life rating for application
- Membrane keypads for sealed, waterproof designs
- Mechanical switches for tactile feedback and durability
- Consider environmental factors (dust, moisture, temperature)
- Never display entered passwords in plain text
- Implement timeout mechanisms for sensitive applications
- Rate-limit attempts to prevent brute-force attacks
- Clear buffers after use to prevent data retention
Interface Protocols
| Protocol | Pins | Complexity | Use Case |
|---|---|---|---|
| Direct Matrix | M + N | Low | Simple projects, learning |
| I²C Expander | 2 (SDA, SCL) | Medium | Pin conservation |
| SPI Shift Register | 3-4 | Medium | Fast scanning needed |
| Serial UART | 1-2 | Low | Remote keypads |
Project Ideas
Beginner Projects
- Calculator: Basic arithmetic with 7-segment or OLED display
- PIN Lock: Electronic lock with correct code entry
- Music Player: Each key plays a different note
- Counter: Increment/decrement display with keypad input
Intermediate Projects
- Menu System: Navigate nested menus on LCD/OLED
- Data Logger: Enter values and timestamps, save to SD card
- Morse Code Trainer: Practice sending/receiving Morse code
- Game Controller: Custom input device for Arduino games
Advanced Projects
- Safe Controller: Multi-combination electronic safe with timeout and lockout
- MIDI Controller: Musical instrument with velocity-sensitive keys
- Two-Factor Authentication: Keypad + RFID card reader security system
- Scientific Calculator: Full-featured calculator with function library