[{"data":1,"prerenderedAt":47},["ShallowReactive",2],{"blog-keyboard-matrix-design-cn":3},{"title":4,"description":5,"date":6,"lastUpdated":6,"category":7,"image":8,"readingTime":9,"wordCount":10,"timeRequired":11,"htmlContent":12,"faq":13,"tags":14,"slug":21,"sourceLocale":22,"jsonld":23},"Keyboard Matrix Design: Key Scanning Circuit Fundamentals","Learn keyboard matrix design including row-column scanning principles, ghosting prevention, N-key rollover implementation, and matrix optimization for QMK firmware.","2025-09-06","design","/assets/img/blogs/2025/09/keyboard-matrix-design.webp",9,1779,"PT9M","\u003Cp>The matrix circuit forms the electrical foundation of every keyboard, enabling a limited number of microcontroller pins to detect keypresses across dozens or hundreds of switches. Understanding matrix design is essential for \u003Ca href=\"/cn/blog/keyboard-pcb-design/\">keyboard PCB design\u003C/a>, as the matrix determines firmware configuration, rollover capability, and overall keyboard functionality.\u003C/p>\n\u003Cp>HILPCB manufactures keyboard PCBs with the consistent quality required for reliable matrix operation—precise hole placement for switches and proper trace connectivity for accurate key detection.\u003C/p>\n\u003Ch2>In This Guide\u003C/h2>\n\u003Col>\n\u003Cli>\u003Ca href=\"#why-matrix\">Why Keyboards Use Matrix Circuits\u003C/a>\u003C/li>\n\u003Cli>\u003Ca href=\"#scanning-process\">Matrix Scanning Process\u003C/a>\u003C/li>\n\u003Cli>\u003Ca href=\"#ghosting\">The Ghosting Problem\u003C/a>\u003C/li>\n\u003Cli>\u003Ca href=\"#diode-solution\">Diode Solution for N-Key Rollover\u003C/a>\u003C/li>\n\u003Cli>\u003Ca href=\"#matrix-config\">COL2ROW vs ROW2COL Configurations\u003C/a>\u003C/li>\n\u003Cli>\u003Ca href=\"#optimization\">Matrix Optimization Strategies\u003C/a>\u003C/li>\n\u003Cli>\u003Ca href=\"#firmware-config\">QMK Firmware Configuration\u003C/a>\u003C/li>\n\u003C/ol>\n\u003Chr>\n\u003Ch2 id=\"why-matrix\">Why Keyboards Use Matrix Circuits\u003C/h2>\n\n\u003Cp>Direct wiring—connecting each switch to a dedicated MCU pin—would be impractical for any keyboard beyond a numeric keypad. A full-size 104-key keyboard would require 104 GPIO pins, far exceeding the capabilities of affordable microcontrollers like the ATmega32U4 (26 I/O pins) or even the RP2040 (30 GPIO).\u003C/p>\n\u003Cp>Matrix circuits solve this constraint by arranging switches at the intersections of row and column lines. The mathematical efficiency is compelling:\u003C/p>\n\u003Cp>\u003Cstrong>Direct wiring (104 keys):\u003C/strong> 104 pins required\n\u003Cstrong>8×14 matrix (104 keys):\u003C/strong> 22 pins required (8 rows + 14 columns)\n\u003Cstrong>Pin reduction:\u003C/strong> 79%\u003C/p>\n\u003Cp>For a 60% keyboard (61 keys), an 8×8 matrix requires only 16 pins—leaving ample I/O for LEDs, rotary encoders, OLED displays, and other features.\u003C/p>\n\u003Cp>The trade-off is complexity: matrix scanning introduces timing considerations, and without additional components, matrices are susceptible to &quot;ghosting&quot; that limits simultaneous key detection. These challenges are well-understood and solved through proper design.\u003C/p>\n\u003Chr>\n\u003Ch2 id=\"scanning-process\">Matrix Scanning Process\u003C/h2>\n\n\u003Cp>Matrix scanning is a sequential process where the microcontroller activates one row (or column) at a time while reading all positions on the opposite axis. By repeating rapidly across all rows, the MCU determines which switches are closed.\u003C/p>\n\u003Ch3>Scanning Sequence\u003C/h3>\n\u003Col>\n\u003Cli>MCU sets Row 0 to LOW, all other rows HIGH (or floating with internal pull-ups)\u003C/li>\n\u003Cli>MCU reads all column pins simultaneously\u003C/li>\n\u003Cli>Any column reading LOW indicates a pressed switch at Row 0, that column\u003C/li>\n\u003Cli>MCU sets Row 1 to LOW (Row 0 returns HIGH), repeats column reading\u003C/li>\n\u003Cli>Process continues through all rows, completing one full scan\u003C/li>\n\u003Cli>Entire sequence repeats hundreds or thousands of times per second\u003C/li>\n\u003C/ol>\n\u003Ch3>Scan Rate and Latency\u003C/h3>\n\u003Cp>Scan rate directly affects perceived keyboard responsiveness:\u003C/p>\n\u003Cul>\n\u003Cli>\u003Cstrong>125Hz (8ms per scan):\u003C/strong> Perceptible lag during fast typing or gaming\u003C/li>\n\u003Cli>\u003Cstrong>500Hz (2ms):\u003C/strong> Acceptable for most users\u003C/li>\n\u003Cli>\u003Cstrong>1000Hz (&lt;1ms):\u003C/strong> Standard for quality keyboards, effectively instantaneous\u003C/li>\n\u003Cli>\u003Cstrong>8000Hz (0.125ms):\u003C/strong> Marketing-driven for competitive gaming, minimal practical benefit over 1000Hz\u003C/li>\n\u003C/ul>\n\u003Cp>QMK firmware achieves 1000Hz on typical ATmega32U4 or RP2040 hardware with basic matrix configurations. Complex configurations (large matrices, extensive RGB, OLED displays) may reduce achievable rates, though 500Hz+ remains typical.\u003C/p>\n\u003Chr>\n\u003Cdiv style=\"background: linear-gradient(135deg, #7c2d12 0%, #c2410c 100%); border-radius: 20px; padding: 32px 24px; margin: 32px 0; box-shadow: 0 12px 32px rgba(0,0,0,0.3);\">\n\u003Ch3 style=\"color: #ffedd5; font-size: 17px; font-weight: 700; margin: 0 0 24px 0; text-align: center;\">Matrix Efficiency Comparison\u003C/h3>\n\u003Cdiv style=\"display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px;\">\n\u003Cdiv style=\"background: rgba(255,255,255,0.1); border-radius: 10px; padding: 20px 14px; text-align: center;\">\n\u003Cdiv style=\"font-size: 26px; font-weight: 800; color: #fed7aa;\">104\u003C/div>\n\u003Cdiv style=\"font-size: 12px; color: #fdba74; margin-top: 6px;\">Direct Wire Pins (Full-Size)\u003C/div>\n\u003C/div>\n\u003Cdiv style=\"background: rgba(255,255,255,0.1); border-radius: 10px; padding: 20px 14px; text-align: center;\">\n\u003Cdiv style=\"font-size: 26px; font-weight: 800; color: #fed7aa;\">22\u003C/div>\n\u003Cdiv style=\"font-size: 12px; color: #fdba74; margin-top: 6px;\">8×14 Matrix Pins\u003C/div>\n\u003C/div>\n\u003Cdiv style=\"background: rgba(255,255,255,0.1); border-radius: 10px; padding: 20px 14px; text-align: center;\">\n\u003Cdiv style=\"font-size: 26px; font-weight: 800; color: #fed7aa;\">79%\u003C/div>\n\u003Cdiv style=\"font-size: 12px; color: #fdba74; margin-top: 6px;\">Pin Reduction\u003C/div>\n\u003C/div>\n\u003Cdiv style=\"background: rgba(255,255,255,0.1); border-radius: 10px; padding: 20px 14px; text-align: center;\">\n\u003Cdiv style=\"font-size: 26px; font-weight: 800; color: #fed7aa;\">1\u003C/div>\n\u003Cdiv style=\"font-size: 12px; color: #fdba74; margin-top: 6px;\">Diode Per Switch\u003C/div>\n\u003C/div>\n\u003C/div>\n\u003C/div>\n\n\u003Ch2 id=\"ghosting\">The Ghosting Problem\u003C/h2>\n\n\u003Cp>Ghosting occurs when matrix circuits create unintended electrical paths, causing the MCU to detect keypresses that didn&#39;t happen. Understanding the mechanism helps appreciate why diodes are essential.\u003C/p>\n\u003Ch3>How Ghosting Occurs\u003C/h3>\n\u003Cp>Consider three keys pressed simultaneously that form three corners of a rectangle in the matrix—positions (Row0, Col0), (Row0, Col1), and (Row1, Col0).\u003C/p>\n\u003Cp>When scanning Row0: both Col0 and Col1 read LOW correctly (those switches are pressed).\u003C/p>\n\u003Cp>When scanning Row1: Col0 reads LOW correctly. But Col1 also reads LOW even though the switch at (Row1, Col1) is NOT pressed. Current flows backward: from the LOW Row1 line, through the pressed switch at (Row1, Col0), into Col0, through the pressed switch at (Row0, Col0), into Row0, through the pressed switch at (Row0, Col1), and finally to Col1—creating a &quot;sneak path&quot; that makes an unpressed key appear pressed.\u003C/p>\n\u003Ch3>Ghosting Characteristics\u003C/h3>\n\u003Cul>\n\u003Cli>Requires at least 3 simultaneous keypresses\u003C/li>\n\u003Cli>Those keys must form 3 corners of a rectangle in the matrix\u003C/li>\n\u003Cli>The 4th corner falsely registers as pressed\u003C/li>\n\u003Cli>More simultaneous keys create more potential ghosting patterns\u003C/li>\n\u003C/ul>\n\u003Cp>Some budget keyboards implement &quot;blocking&quot;—preventing certain combinations from registering rather than producing ghosts. This is unacceptable for gaming and fast typing where complex key combinations are routine.\u003C/p>\n\u003Chr>\n\u003Ch2 id=\"diode-solution\">Diode Solution for N-Key Rollover\u003C/h2>\n\n\u003Cp>Adding a diode in series with each switch eliminates ghosting by preventing current from flowing backward through the matrix. Diodes conduct in one direction only—from anode to cathode—blocking the sneak paths that cause ghost detection.\u003C/p>\n\u003Ch3>How Diodes Prevent Ghosting\u003C/h3>\n\u003Cp>With properly oriented diodes, current can flow from a switch to its row (or column) line but cannot flow backward from that line through another switch. Each switch position becomes electrically isolated from creating ghost circuits. The MCU reads only genuine keypresses regardless of how many keys are pressed.\u003C/p>\n\u003Ch3>Result: N-Key Rollover (NKRO)\u003C/h3>\n\u003Cp>With per-switch diodes:\u003C/p>\n\u003Cul>\n\u003Cli>Any combination of keys detected accurately\u003C/li>\n\u003Cli>No blocking combinations\u003C/li>\n\u003Cli>Consistent behavior regardless of simultaneous keypresses\u003C/li>\n\u003Cli>Essential for gaming and fast typing\u003C/li>\n\u003C/ul>\n\u003Cp>This is why all quality mechanical keyboards include one diode per switch. The component cost and assembly complexity are justified by the dramatic improvement in functionality.\u003C/p>\n\u003Ch3>Common Diode Options\u003C/h3>\n\u003Cul>\n\u003Cli>\u003Cstrong>1N4148 (through-hole DO-35):\u003C/strong> Classic choice, easy hand soldering, larger footprint\u003C/li>\n\u003Cli>\u003Cstrong>1N4148W (SOD-123):\u003C/strong> Standard SMT package, good balance of size and handling\u003C/li>\n\u003Cli>\u003Cstrong>1N4148WS (SOD-323):\u003C/strong> Smaller SMT for space-constrained designs\u003C/li>\n\u003Cli>\u003Cstrong>BAV70 (SOT-23):\u003C/strong> Dual-diode package containing two diodes with common cathode—serves two switches, reduces component count\u003C/li>\n\u003C/ul>\n\u003Cp>Orientation details in \u003Ca href=\"/cn/blog/keyboard-diode-direction/\">keyboard diode direction\u003C/a>.\u003C/p>\n\u003Cp>\u003Cimg src=\"/assets/img/blogs/2025/09/keyboard-matrix-design-1.webp\" alt=\"Keyboard Matrix Design\">\u003C/p>\n\u003Ch2 id=\"matrix-config\">COL2ROW vs ROW2COL Configurations\u003C/h2>\n\n\u003Cp>Two standard configurations exist for keyboard matrices, differing in which axis drives current versus which reads results. Both work equally well—the choice typically depends on routing convenience.\u003C/p>\n\u003Ch3>COL2ROW (Most Common)\u003C/h3>\n\u003Cul>\n\u003Cli>\u003Cstrong>Diode cathode (marked end) connects to the ROW line\u003C/strong>\u003C/li>\n\u003Cli>Column pins configured as outputs, driving LOW during scan\u003C/li>\n\u003Cli>Row pins configured as inputs with pull-up resistors\u003C/li>\n\u003Cli>Memory aid: &quot;COL2ROW = Cathode to Row&quot;—the &quot;C&quot; in both words\u003C/li>\n\u003C/ul>\n\u003Cp>\u003Cstrong>QMK configuration:\u003C/strong> \u003Ccode>#define DIODE_DIRECTION COL2ROW\u003C/code>\u003C/p>\n\u003Ch3>ROW2COL (Alternative)\u003C/h3>\n\u003Cul>\n\u003Cli>\u003Cstrong>Diode cathode connects to the COLUMN line\u003C/strong>\u003C/li>\n\u003Cli>Row pins configured as outputs, driving LOW during scan\u003C/li>\n\u003Cli>Column pins configured as inputs with pull-ups\u003C/li>\n\u003Cli>Signal flows from Row, through switch and diode, TO COLumn\u003C/li>\n\u003C/ul>\n\u003Cp>\u003Cstrong>QMK configuration:\u003C/strong> \u003Ccode>#define DIODE_DIRECTION ROW2COL\u003C/code>\u003C/p>\n\u003Ch3>Critical Requirement\u003C/h3>\n\u003Cp>\u003Cstrong>Firmware must match hardware exactly.\u003C/strong> If the PCB uses COL2ROW orientation but firmware specifies ROW2COL, the entire matrix will be non-functional. This mismatch is a common source of &quot;dead keyboard&quot; issues during initial bring-up.\u003C/p>\n\u003Chr>\n\u003Cdiv style=\"background: linear-gradient(135deg, #1e3a5f 0%, #2563eb 100%); border-radius: 20px; padding: 32px 24px; margin: 32px 0; box-shadow: 0 12px 32px rgba(0,0,0,0.3);\">\n\u003Ch3 style=\"color: #dbeafe; font-size: 17px; font-weight: 700; margin: 0 0 24px 0; text-align: center;\">Common Keyboard Matrix Dimensions\u003C/h3>\n\u003Cdiv style=\"display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px;\">\n\u003Cdiv style=\"background: rgba(255,255,255,0.1); border-radius: 10px; padding: 20px 14px; text-align: center;\">\n\u003Cdiv style=\"font-size: 26px; font-weight: 800; color: #93c5fd;\">8×8\u003C/div>\n\u003Cdiv style=\"font-size: 12px; color: #bfdbfe; margin-top: 6px;\">60% Keyboard (16 Pins)\u003C/div>\n\u003C/div>\n\u003Cdiv style=\"background: rgba(255,255,255,0.1); border-radius: 10px; padding: 20px 14px; text-align: center;\">\n\u003Cdiv style=\"font-size: 26px; font-weight: 800; color: #93c5fd;\">6×17\u003C/div>\n\u003Cdiv style=\"font-size: 12px; color: #bfdbfe; margin-top: 6px;\">TKL Keyboard (23 Pins)\u003C/div>\n\u003C/div>\n\u003Cdiv style=\"background: rgba(255,255,255,0.1); border-radius: 10px; padding: 20px 14px; text-align: center;\">\n\u003Cdiv style=\"font-size: 26px; font-weight: 800; color: #93c5fd;\">8×14\u003C/div>\n\u003Cdiv style=\"font-size: 12px; color: #bfdbfe; margin-top: 6px;\">Full-Size (22 Pins)\u003C/div>\n\u003C/div>\n\u003Cdiv style=\"background: rgba(255,255,255,0.1); border-radius: 10px; padding: 20px 14px; text-align: center;\">\n\u003Cdiv style=\"font-size: 26px; font-weight: 800; color: #93c5fd;\">4×12\u003C/div>\n\u003Cdiv style=\"font-size: 12px; color: #bfdbfe; margin-top: 6px;\">40% Keyboard (16 Pins)\u003C/div>\n\u003C/div>\n\u003C/div>\n\u003C/div>\n\n\u003Ch2 id=\"optimization\">Matrix Optimization Strategies\u003C/h2>\n\n\u003Cp>Thoughtful matrix organization simplifies PCB routing, eases firmware development, and minimizes GPIO requirements.\u003C/p>\n\u003Ch3>Balancing Rows and Columns\u003C/h3>\n\u003Cp>For a given key count, multiple dimensions work mathematically. A 60-key board could use 5×12 (17 pins), 6×10 (16 pins), or 8×8 (16 pins with 4 unused positions).\u003C/p>\n\u003Cp>\u003Cstrong>Square or near-square matrices\u003C/strong> typically minimize total pin count while balancing routing complexity between layers. Approximately equal horizontal and vertical line counts distribute routing work evenly.\u003C/p>\n\u003Ch3>Physical Alignment\u003C/h3>\n\u003Cp>Aligning matrix rows with physical keyboard rows dramatically simplifies both routing and firmware:\u003C/p>\n\u003Cul>\n\u003Cli>Row traces naturally run horizontally (matching physical layout)\u003C/li>\n\u003Cli>Column traces run vertically\u003C/li>\n\u003Cli>Keymap definition becomes intuitive (visual match to physical keyboard)\u003C/li>\n\u003Cli>Reduced crossing points and via count\u003C/li>\n\u003C/ul>\n\u003Ch3>Spare Positions\u003C/h3>\n\u003Cp>Leave unused matrix positions for layout variants:\u003C/p>\n\u003Cul>\n\u003Cli>Split Backspace (two 1u keys vs one 2u)\u003C/li>\n\u003Cli>ISO Enter vs ANSI Enter\u003C/li>\n\u003Cli>Split Right Shift\u003C/li>\n\u003Cli>Alternative bottom row configurations\u003C/li>\n\u003C/ul>\n\u003Cp>An 8×8 matrix with 61 keys leaves 3 positions for alternatives without additional pins.\u003C/p>\n\u003Chr>\n\u003Ch2 id=\"firmware-config\">QMK Firmware Configuration\u003C/h2>\n\n\u003Cp>QMK requires exact matrix specification matching hardware. The firmware needs matrix dimensions, pin assignments, and diode orientation.\u003C/p>\n\u003Ch3>Configuration in config.h\u003C/h3>\n\u003Cpre>\u003Ccode class=\"language-c\">#define MATRIX_ROWS 8\n#define MATRIX_COLS 8\n#define MATRIX_ROW_PINS { B0, B1, B2, B3, B4, B5, B6, B7 }\n#define MATRIX_COL_PINS { D0, D1, D2, D3, D4, D5, D6, D7 }\n#define DIODE_DIRECTION COL2ROW\n\u003C/code>\u003C/pre>\n\u003Cp>Pin naming varies by MCU: ATmega uses B0, D2, F4; RP2040 uses GP0, GP1, etc. Verify against the specific MCU datasheet.\u003C/p>\n\u003Ch3>Testing and Verification\u003C/h3>\n\u003Cp>Before finalizing firmware:\u003C/p>\n\u003Col>\n\u003Cli>Flash basic firmware with matrix debug enabled\u003C/li>\n\u003Cli>Short each switch position with tweezers\u003C/li>\n\u003Cli>Monitor debug output for correct row/column detection\u003C/li>\n\u003Cli>Test simultaneous keypresses to confirm no ghosting\u003C/li>\n\u003Cli>Verify all expected positions register correctly\u003C/li>\n\u003C/ol>\n\u003Cp>\u003Cstrong>Troubleshooting patterns:\u003C/strong>\u003C/p>\n\u003Cul>\n\u003Cli>Entire row/column dead → Trace routing error or wrong pin assignment\u003C/li>\n\u003Cli>Scattered dead keys → Individual diode issues\u003C/li>\n\u003Cli>Complete matrix dead → DIODE_DIRECTION mismatch\u003C/li>\n\u003C/ul>\n\u003Chr>\n\u003Cdiv data-component=\"BlogQuickQuoteInline\">\u003C/div>\n\n\u003Chr>\n\u003Ch2>HILPCB Matrix PCB Fabrication\u003C/h2>\n\u003Cp>HILPCB manufactures keyboard PCBs with the precision required for reliable matrix circuits:\u003C/p>\n\u003Cul>\n\u003Cli>\u003Cstrong>Hole Placement:\u003C/strong> Consistent switch alignment across the entire board\u003C/li>\n\u003Cli>\u003Cstrong>Via Plating:\u003C/strong> 20-25μm barrel copper for reliable through-connections\u003C/li>\n\u003Cli>\u003Cstrong>Trace Uniformity:\u003C/strong> ±10% across panels for consistent electrical characteristics\u003C/li>\n\u003Cli>\u003Cstrong>Diode Assembly:\u003C/strong> \u003Ca href=\"/cn/products/smt-assembly/\">SMT assembly\u003C/a> with verified orientation for all common packages\u003C/li>\n\u003C/ul>\n\u003Cp>We support keyboard projects from \u003Ca href=\"/cn/products/small-batch-assembly/\">prototype quantities\u003C/a> through \u003Ca href=\"/cn/products/large-volume-assembly/\">group buy volumes\u003C/a> with consistent quality at every scale.\u003C/p>\n\u003Cp>Contact HILPCB for keyboard matrix design review and manufacturing quotation.\u003C/p>\n\n\u003Csection class=\"related-links\" aria-label=\"Related\">\u003Ch3>Related links\u003C/h3>\u003Cul>\u003Cli>\u003Ca href=\"/cn/blog/keyboard-pcb-design/\">Keyboard PCB Design: Complete Guide to Custom Mechanical Keyboard\u003C/a>\u003C/li>\u003Cli>\u003Ca href=\"/cn/blog/keyboard-diode-direction/\">键盘二极管方向：COL2ROW 与 ROW2COL 极性指南\u003C/a>\u003C/li>\u003Cli>\u003Ca href=\"/cn/products/smt-assembly/\">SMT assembly\u003C/a>\u003C/li>\u003Cli>\u003Ca href=\"/cn/products/small-batch-assembly/\">prototype quantities\u003C/a>\u003C/li>\u003Cli>\u003Ca href=\"/cn/products/large-volume-assembly/\">group buy volumes\u003C/a>\u003C/li>\u003C/ul>\u003C/section>",[],[15,16,17,18,19,20],"Keyboard Matrix","N-Key Rollover","Ghosting Prevention","Matrix Scanning","QMK Matrix","NKRO Keyboard","keyboard-matrix-design","en",{"blog":24,"breadcrumb":33},{"@context":25,"@type":26,"headline":4,"description":5,"image":8,"url":27,"datePublished":6,"dateModified":6,"timeRequired":11,"keywords":28,"articleSection":7,"author":29,"publisher":32},"https://schema.org","BlogPosting","https://hilpcb.com/cn/blog/keyboard-matrix-design/","Keyboard Matrix, N-Key Rollover, Ghosting Prevention, Matrix Scanning, QMK Matrix, NKRO Keyboard",{"@type":30,"name":31},"Organization","HILPCB",{"@type":30,"name":31},{"@context":25,"@type":34,"itemListElement":35},"BreadcrumbList",[36,41,45],{"@type":37,"position":38,"name":39,"item":40},"ListItem",1,"Home","https://hilpcb.com/",{"@type":37,"position":42,"name":43,"item":44},2,"Blog","https://hilpcb.com/cn/blog/",{"@type":37,"position":46,"name":21,"item":27},3,1791623286007]