# How does this N-key-rollover work?

I'm designing a keyboard. I found some information online about how to make the keyboard be able to detect multiple key presses at the same time (N-key-rollover).

I found this schematic, from this website.

The website says that A and B are the two inputs, and 1 and 2 are the two outputs. I'd select A, then read the values at 1 and 2, and hence know which keys are currently pressed, then do the same with B.

The diodes are to prevent false-positive keypresses. The website explains this in more detail.

What I don't understand, is how 1 or 2 would ever be high? Let's assume A is high, and the bottom left switch is closed, surely its diode is still opposing the current flow, right? How would any current flow to 2?

Is this a matter of conventional vs. electron current flow? Because that always confuses me, but I just can't figure this out.

EDIT, this is active low.

• Is this for active-high or active-low logic? – Hearth Oct 11 '18 at 21:36
• @Felthry whoops - post edited, sorry for leaving that out. – Jacob Garby Oct 11 '18 at 21:38
• (and where I said "let's assume A is high", I mean when it is selected, or active) – Jacob Garby Oct 11 '18 at 21:38

A pull-up resistor is required on both pins 1 and 2. These will weakly pull the output pins high. Additionally A and B are either driven high or low depending on which column you are wanting to scan - driving low enables a column, driving high disabled a column.

If input A say is driven low, and outputs 1 and 2 are pulled weakly high by a resistor, then you basically have a circuit that is nothing more than two push buttons. You can read the values of 1 and 2 to see if the button is pressed or not.

If input B is driven low, you again have two push buttons which can be checked by reading 1 and 2. Again the outputs are weakly driven high by resistors.

By constantly swapping between B and A being driven low, you can read all four buttons using two pins.

The diodes prevent the buttons in column B from being registered if you were to push multiple buttons simultaneously (e.g. pressing all of A1, B1, A2 simultaneously would otherwise pull B2 low).

The reason the pull-up resistors are not shown is that these are typically present internally in the microcontroller used to read the array.

• That makes sense. Thanks for the quick answer! I'll accept it as soon as I can. – Jacob Garby Oct 11 '18 at 21:43
• @Jacob: Tom won't mind me saying this but wait a day or two to see what other answers come in. You will get other points of view and maybe some other insights. When you accept too quickly it discourages others from answering. You can usually upvote as well. – Transistor Oct 11 '18 at 22:01
• @TomCarpenter I'm sorry for bringing this up again, but I can't get my head around why the diodes appear to be the wrong way around, to me? – Jacob Garby Oct 14 '18 at 12:52
• Or in other words, how would any current at all flow from the columns to the rows? – Jacob Garby Oct 14 '18 at 13:09
• The diodes are the way round they are because you are driving A or B low. 1 and 2 are pulled high, so current needs to flow from number to letter. – Tom Carpenter Oct 14 '18 at 13:42