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:
  • 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:
  1. Initialize: Set all row pins as OUTPUT (HIGH), all column pins as INPUT with pull-up resistors (HIGH by default)
  2. 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
  3. Repeat: Continuously scan all rows (typically 10-100 times per second)
  4. Debounce: Ignore rapid changes to filter mechanical bounce
Example Detection:
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

ParameterTypical Value
Contact Resistance (Closed)< 100Ω
Insulation Resistance (Open)> 100MΩ
Operating Force100-200 gf (gram-force)
Operating Travel0.5-1.0 mm
Life Expectancy1-10 million operations
Bounce Time5-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:
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.

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:
  • 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.

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)

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
Key States:
  • 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

ApplicationKeypad TypeExample
Security Systems3×4, 4×4PIN entry, alarm codes
Telephones3×4Dialers, intercoms
Calculators4×4, 4×5Scientific, basic calculators
Point-of-Sale4×4, 5×4Payment terminals, registers
Industrial Control4×4, customMachine interfaces, HMI
GamingCustom matricesArcade buttons, controllers
MusicCustom sizesMIDI 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
Reliability:
  • 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)
Security:
  • 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

ProtocolPinsComplexityUse Case
Direct MatrixM + NLowSimple projects, learning
I²C Expander2 (SDA, SCL)MediumPin conservation
SPI Shift Register3-4MediumFast scanning needed
Serial UART1-2LowRemote keypads

Project Ideas

Beginner Projects

Intermediate Projects

Advanced Projects