# Matrix keyboard & shifting-zero algo: possible short?

I need to read a very basic 3x4 numeric keypad connected directly to my pi. I've read some and decided to go with the so-called shifting-zero algorithm: have 4 pins connected to the keyboard's rows and configured as outputs and another 3 pins connected to the columns configured as inputs, with the internal pull-up resistors enabled. The reading loop would then look something like:

foreach (outputPins as row => outPin) {
set outPin low
foreach (inputPins as column => inPin) {
if (inPin is low) // key at [row][column] is pressed
}
set outPin high
}

(sorry for the pseudo-code ;-) )

There's more logic inside the inner loop related to debouncing the keys etc, but that doesn't directly relate to the question. Because it seems to me that if I press two keys in the same column at the same time, then setting one of the key's corresponding row pin low would effectively short the other key's corresponding row pin (which, in the meantime, is high) to ground. That, I believe, could damage my pi. Or could it? I can't really tell :-) I'm not terribly experienced at this stuff.

I was thinking about connecting a small resistor, say 470R, in series between each output pin and it's corresponding keyboard row. But that wouldn't solve the problem, because if I then press two keys at the same time, the two resistors (along with the internal pull-up) would then form a voltage divider which would cause the input to see something around 1.65V (3.3V / 2) which I don't believe it would really like. Even if it did, it wouldn't read anything useful, which would kind of defeat the whole purpose.

I figure maybe it could be done with diodes then: instead of a resistor, I'd connect a 4148 or the like between each output pin and it's row, it's cathode to the pi, anode to the keyboard. That way the diode disables any current from flowing from the output pins, but still would permit those pins to sink current. But a diode needs some voltage across it to open (typically 0.7V I believe). I think that that's ok as the input pins would have their internal pull-ups enabled thus providing the required voltage, but then the inputs would still see 0.7V instead of 0.. or would they? And if they do, would that still read as a "low"?

Or is there another solution which I'm completely unaware of? O.o

Thanks for any tips!

• there are separate USB numeric pads available, why not use one of these? Jan 25, 2014 at 6:36
• Can't. I need the USB ports for other stuff, plus I need the keypad to be wall mounted & vandalism-tolerant, so a regular PC numpad won't cut it for me. Plus it would mean effectively exposing an USB port to the outside world which makes it a security risk. Jan 25, 2014 at 10:24
• You only mention 2 simultaneous keys, but to properly prevent all ghosting you really need a diode inline with every key. Many mechanical keys include a holder for one. Jan 25, 2014 at 22:35

Your current situation could indeed create a short between two row pins. (the keyboard might have some resistors inside, but I think that is unlikely).

A software only solution might be to set the GPIO pins back to inputs, when you don't use them, instead of setting them to 3.3 volt.

The diode solution will also work. The GPIO pins need at least around 2.6V to register a HIGH. So 0.6V is no problem.

Resistors will also work. Note that you calculate the voltage incorrectly on the voltage divider. Since the internal pull-up is something like 10kOhm, the resulting voltage will be more like 0.2V. Downside to the resistor solution is that if you press multiple button in the same column, none will register.

• Thanks Gerben! I'll go with the diodes then. As to the resistors, I believe that the wiki quotes the internal pull-ups as being somewhere in the 50-65kOhm range. Even if they were around 10kOhms as you suggested, the divider would yield some 1.68V as the pull-up would be parallel to one of the series resistors - and if those were set at around 470 Ohm, the 10K parallel resistance would have little effect. Just to elaborate ;-) Jan 25, 2014 at 16:04