# Creating a 5x5 keypad without using micro-controllers

This is my first question ever on stackexchange, so if i am unable to provide enough information or if i do anything wrong, please let me know.

I want to create a 5x5 keypad using 25 simple buttons, i could connect each column to a decoder output and record the decoder input to get the column number and connect the rows to a multiplexer to get the column number ( i think i understand this part), but i want to convert the row and column number to a 5 bit number (0-24), to represent the pressed button. Basically i want to apply this formula using discrete logic: [(5*row)+column)]. For Example coordinate (0,0) will be 0 and coordinate (3,4) will be 19. Also assume that only one button is pressed at a time.

Designing such a circuit through kmapping requires alot of hardware (ICs), is there a way to implement this using less hardware (ICs)?

This is part of a project, only micro-controllers are not allowed, any other TTL hardware can be used.

• The "simpler" you try to do it, the more hardware you will end up using. May 25, 2018 at 18:03
• What happens when two buttons are pressed together - would you like the average value? May 25, 2018 at 18:09
• lets assume that 2 buttons will never be pressed. May 25, 2018 at 18:11
• In that case you can just use a diode matrix. Button 0 : no diodes. Button one to D0, Button 2 to D1, button 3 to D0 and D1 etc. You have a small problem that 0 and no button are the same... May 25, 2018 at 18:14
• @Oldfart i don't completely understand your model May 25, 2018 at 18:25

(5 × row) + column

Designing such a circuit through K-mapping requires a lot of hardware (ICs) ...

That's why more specialized ICs were invented. For example, you can get a 4-bit adder as a single chip (7483). It only takes two of them, plus a 2-input OR gate, to implement your formula.

I realize that this is a homework assignment, but I'm going to give you an answer anyway. Optimizing a design around SSI/MSI logic functions is an obsolete skill that has no place in a world that has cheap MCUs, CPLDs and FPGAs. I have this skill because I was designing large systems using this kind of logic back in the 1980s, but I really have no idea why anyone would still be teaching it today.

Multiplying R by 5 is a simple question of adding R and 4 × R, and the latter is created by simply shifting R by two places to the left. Let's define the bits of R as r2 r1 r0, and the bits of C as c2 c1 c0.

So 5 × R is

 R =  0  0 r2 r1 r0
4R = r2 r1 r0  0  0
--------------
s4 s3 s2 s1 s0


Note that the two LSBs of the sum (s1 s0) are simply copies of r1 r0 — no logic is required to create them. A 4-bit adder chip can be used to create the remaining three columns (s4 s3 s2).

Now we need to add the column bits to this sum. Normally, this would require a 5- bit adder, but we can take advantage of the fact that the row and column numbers only range from 0 to 4 in this application.

Note that when R=4, S=20, which is 10100 in binary, and this is the only time that s4 is set. Regardless of the value of C, there can never be a carry into the s4 column. Similarly, when R=3, S=15, which is 01111 in binary. Now, s4 is not set, but any nonzero value of C will result in a carry into that column. Therefore, we don't need a full adder for that column. We can use another 4-bit adder to process the four low-order columns, and then simply OR together s4 and the carry-out of that adder to create the MSB of the result.

• how can it be done with 2 full adder and an or gate? i can only see this working with 3 full adders [2R+2R+R+C] May 25, 2018 at 22:34
• Hint: What's 2R+2R? May 26, 2018 at 0:01
• 2 times row number, breaking the 5R to 2R+2R+R, 2R can be achieved by simply shifting the number one bit to the right but 4R has to be achived by shifting twice, that cant be done in 4 bits May 26, 2018 at 0:24
• Sure it can, once you realize that not all of the bits need to go through the adder. Note that the two LSBs of 4R are always zero. May 26, 2018 at 3:55

Here is a diode matrix for seven buttons and three bits. (Updated after Andy's correction.) Expand it to get 25 buttons. simulate this circuit – Schematic created using CircuitLab

• this is a very different approach from what i was thinking, still very useful May 25, 2018 at 18:34
• I gave a circuit like this to a clay-pigeon shooting guy. He wanted a set of buttons to launch 1,2..N clay-pigeons simultaneously. May 25, 2018 at 18:44
• That is actually an excellent idea. Isn't there a glitch between D11 and D12, though? Anyway, just for the fun, let me calculate the number of diodes for 25 buttons... 79! Much less than I expected, actually.
– dim
May 25, 2018 at 19:09
• Yep small drawing error between D11 and D12. I'll correct. May 25, 2018 at 19:41
• If you need a "key pressed" output, why not just diode-OR sw0, d0, d1 and d2 together? Saves three diodes. You could also remove the diode between a switch and an output (d0, d1 or d2) whenever said switch connects to just a single output.
– jms
May 25, 2018 at 21:47

What you are looking for is something like the MM74C922 / MM74C923 16 or 20 key encoder. I don't know of a 25-way version. Figure 1. The MM74C923 key matrix encoder.

There is rather a lot going on in this and some study of the datasheet may convince you that it's not worth trying to do it in discrete logic. It should be rather straight forward to emulate in a small micro though.

I first came across it in a Z80 schematic I used to explain micro-computer essentials. Figure 2. The keypad matrix encoder for the Z80 computer is located at the bottom right. (Click to enlarge.) Source: Z80.info.

• this is very useful, might have solved my problem. May 25, 2018 at 18:28
• Z80!!! wow... that is something I haven't heard in a long while. Like 25 years or so. Getting all primordial here, are we? ;) May 25, 2018 at 18:46
• I don't think I ever used one but the schematic is very useful for explaining the essentials of a micro-computer with real address and data buses. I've used it at work to explain the innards of micro-controllers to electrical maintenance personnel who may never have understood them. The operation of U5-A and U7-A to drive the chip selects are rather interesting. May 25, 2018 at 19:17
• Using bit banged serial to avoid a CTC chip that a SIO would have required. But perhaps the spare DIO lines were the system resource so using two for serial was a fair choice. May 26, 2018 at 8:19
• What is CTC, SIO and DIO and by 'serial' are you referring to the RS232 port? May 26, 2018 at 8:23

You say you don't want to use a microcontroller, but what will be receiving these signals? The way you describe would be more complicated than you think as you would still need some way to select the channel for the mux/demux.

The easiest thing I can think of would be a resistive ladder (google R*2R) with 25 inputs. This would output a voltage anywhere from 0 to your input voltage, with each switch outputting a different voltage, and combinations of switches outputting a different voltage still. This would only require 50 resistors (albeit you would want at most 1% resistors). This takes it into the analog realm though.

• i think i want to keep it digital, i don't understand analog circuits at all (software engineering student). May 25, 2018 at 18:36
• You can ignore this; it's a silly suggestion anyway. Stiddily, take another look at the precision of resistors required for a 25-bit DAC! (Not to mention the ADC required to read the result!) May 25, 2018 at 20:41
• @DaveTweed Yes impractical but a modified solution with 2 to 5 analogue outputs (each handling a portion of the switches) using R-2R ladders might be feasible and well within resistor tolerances and surplus microcontroller IO pin resources. It would support n-key roll over as well and probably use less IO lines than a digital implementation. May 26, 2018 at 8:14
• None of which answers th actual question, which is about producing a binary number in the range 0 to 24 using only TTL from a matrix-connected keypad. May 26, 2018 at 13:12