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:
- Set
R0
to output high, all others low - Read all
Cn
pins to find out whichR0
keys are pressed - 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:
- Enable internal pull-ups on all
Rn
- Set
C0
to output low, all others high (or hi-z?) - Read all
Rn
pins to find out whichC0
keys are pressed - 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?