I'm writing some microcontroller software to read a mechanical keyboard, which has diodes to prevent keys from interfering with each other.

The keyboard has the keys set up with row outputs and column inputs that can be scanned; basically the boxed area in this schematic, but with many more keys:


simulate this circuit – Schematic created using CircuitLab

Rows R0, R1, and columns C0, C1 are GPIO pins on my microcontroller, AT90USB1286 (8-bit AVR).

I have limited understanding of actual electronics and don't really know how to use diodes effectively. I believe I could scan the keys using the following (naive) process:

  1. Set R0 to output high, all others low
  2. Read all Cn pins to find out which R0 keys are pressed
  3. Repeat for R1, R2, etc.

For that to work, my understanding is that each column would need to have a pull-down resistor to prevent floating signals when no column keys are pressed. My microcontroller has optional internal pull-ups on every GPIO pin but no pull-downs, so I would need to add them myself. I don't believe I could use the pull-ups and change the scanning row output to low because the diodes will prevent the low signal from reaching the column input.

However, I am wondering if I could swap the row and column roles:

  1. Enable internal pull-ups on all Rn
  2. Set C0 to output low, all others high (or hi-z?)
  3. Read all Rn pins to find out which C0 keys are pressed
  4. Repeat from 2 for C1, C2, etc.

This would mean that my schematic would use R0 and R1 as inputs with pullups enabled, and C0/C1 as outputs; each one would output low when scanning it.

My primary concern is that the diodes will somehow prevent the low output from overriding the pull-ups, or something like that.

Does this approach work? Should I output high on the column pins that I'm not scanning, or is it okay (or better) to make them hi-z/inputs?


Yes, it is possible. If Cn is 0V, the diode will conduct, and at the switched point Rm there will be the diode's forward voltage. make sure it is bellow the MCU's Vin low threshold.

  • \$\begingroup\$ Which means a minimum supply of 2.4V given CMOS thresholds (Vdd * 0.3). \$\endgroup\$ – Ignacio Vazquez-Abrams Sep 21 '13 at 18:13

protected by Kortuk Sep 21 '13 at 16:12

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.