# Creating a keyboard with a microcontroller, do I need an input pin for each key?

I am planning to implement V-USB with an ATMega8 microcontroller. I am interested in implementing an HID device; more specifically, I want to try and make my own simple 26 key keyboard (each corresponding to a letter of the English alphabet).

This may be a stupid question, but do I need an input pin for each key I implement? In other words, will I need to use a microcontroller that has at least 26 I/O pins? Or is there another way to do this?

• Pretty much like this question: electronics.stackexchange.com/questions/37355/… Also check @stevenvh his elaboration on Charlieplexing If you have 6 inputs, then Charlieplexing allows you $6 × ( 6 - 1 ) = 30$ keys. Nov 9, 2012 at 22:55

The idea to use a matrix is certainly the way to go. Your matrix could be organized in another way with eight input rows and four output lines for support of up to 32 switches. Since you only need to excite one input row at a time you could use a simple common 3-8 decoder to drive the input rows like shown below. This reduces the total microcontroller pin count to 7 lines!!

Note that with use of a 74HC138 type of 3-8 decoder it is necessary to flip the logic so that the output columns have pullup resistors, the diode direction is inverted and the software will see lows (0's) on the output lines where detected switches are seen.

Many thanks to tcrosley for borrowing some of his key matrix graphics and to Philips Semiconductors for the logic symbol image of their 74HC138 part.

• Nice addition to my circuit -- well, not really mine, I "borrowed" the image from somewhere on the web after searching for keyboard matrix. Nov 9, 2012 at 20:23
• Cool, thanks! The actual concept I had was to make each key a capacitive touch sensor. Would that still be possible with the keyboard matrix method? Nov 9, 2012 at 23:59
• @capcom - The scheme shown is designed for switches that have low impedance contact closures. Capacitive type switches are a whole different type of technology. Contact closure switches including the extremely low cost TACT type push buttons are the type for this matrix solution. Nov 10, 2012 at 3:03
• I see. Well, in terms of low impedance contact closures, do you know what I would be looking for if I was trying to create the thinnest switch possible? That is why I was considering capacitive touch as a solution. Thanks again! Nov 10, 2012 at 3:12
• @capcom - To get an idea of how thin you can get a keyboard to be you may want to take apart a computer keyboard to see how they work inside. In keyboards from the recent years the they have three sheets of mylar material. Two sheets in the stack have conductive ink printed on their surfaces facing each other. The third sheet is in between these other two with holes in it where each key site is located. Pressing the two outer sheets together in the area of the hole allows the conductive ink spots that are on the outer sheets to contact through the hole! Nov 10, 2012 at 6:42

One of the easiest ways is to use a keyboard matrix, like this one:

In your case, for 26 keys, you could arrange it in a 5 x 6 matrix (requiring 11 I/O pins). This will actually allow for 30 keys without any additional wires. A 6x6 matrix (just one more wire) would permit 36 keys, enough for A-Z and 0-9.

Because of the squaring effect, the larger the keyboard, the more wires you save. 100 keys takes only 20 wires, even less than your running one wire from each of your original 26 keys.

The matrix idea works by scanning: You hold one of the I lines high, one after another, and then scan all of the O lines for each I line held high. You will get a input high on only one one of the O lines for only one of the I lines held high. The combination will isolate one of the keys.

• Don't forget pulldown resistors on the "O" lines. Nov 9, 2012 at 17:32