[Solved] Matrix keypad emulation with CD4051BE

Hi, I’m not really sure how to do what I need to do. I have a pretty big keypad/keyboard on a device, which requires specific key combinations and codes in order to work, which are easy to forget. Instead of writing everything on a piece of paper and inputting it manually, I want to emulate the key presses with an Arduino. I have seen, I think, two other topics related to this, one in which the OP makes this happen using optocouplers, without sharing any code, and in the other topic, the OP is advised to use the CD4051BE IC, but there is no follow-up.

I should mention that I have no idea how to use this IC for anything, let alone what I am trying to use it for. I have a 33 key, 12 pin keypad, where pins #1-#7 are the columns and #8-#12 are the rows (they might me the other way around, to be sure which are which, can you let me know what measurements to take?). If it helps anyone, I will attach my list of key combinations and their resulting key.

I would like any valuable resources that you have to offer, or even complete code which I can modify to my needs, if you have done something similar to this before.

Thanks in advance for helping a noob out!

MP55 KEYBOARD.txt (646 Bytes)

That is quite an interesting problem. It looks like the keys are arranged in a 7 x 5 matrix.

The scanning of the existing keyboard may work something like this:

The rows are set LOW and the columns pulled (say via a 10k resistor) towards HIGH. The colums are tested to see if any are low. If so, a button has been pressed in that column.
Now exactly the same is done again, but with rows and columns interchanged.
At the end the row and column of the pressed button has been identified.

You may be lucky, if this is the case, and simply hold the row and column pins for a specific key (which you have already identified in your table) LOW for a couple of scanning cycles.

In the worst case, you will, somehow, have to monitor the scanning cycle and set the appropriate pin (on that 12 pin connector) to the correct value at the correct time.

I'd suggest a 16 way port expander say MCP23017.

However, you'd have to do some experiments first to check this way will work.

You can always use one optocoupler per key, where the polarity of the voltage on the key has been measured and aligned with the polarity of the output of the opto. In that case, there is no need to decipher the scanning sequence, and it is pretty much guaranteed to work. You just power the opto for the length of time that a key press would take. If you don't need to connect all the keys, it's the easiest way. You get full electrical isolation as a bonus.

Thanks for the answers, I haven't yet made any measurements, but the point was using the CD4051BE which I ordered specifically for this. Let's say I had a more "normal" keypad, like a 4x4 one. How would I be able to emulate it with the IC mentioned above?

Hang on!

The question is - how many different keys do you actually need to emulate? If it is for example, four, then you can use a 74HC4066 which is four switches controlled by four input pins, so each "switch" is connected in the matrix position for a particular key, and you set the control pin for that switch HIGH to "close" the switch. Dead easy. :grinning:

The 4051 connects one line to any of eight, controlled by four lines, three to choose one of eight and the fourth to enable it. This would suit if there were seven (in your case, and if you use only seven of the switches then you do not need to control the enable line) keys on one row that you needed to emulate. If you use two 4051s "back to back" then you can emulate any key in the matrix because one selects a row and the other connects it to a selected column.

It probably does not matter for your application, but the CD4051 is essentially obsolete being of the old "CD" series. The correct part is the 74HC4051 or 74HC4066 as I mentioned. These have much better than half the "on" switch resistance and are considerably faster in response where that matters. This is of course, always a danger in finding "information" on these sort of forums. :cold_sweat:

While you could use optocouplers if you needed to, if the keypad is on a system operating at 5 V as it almost always would be and your system using the 75HC4051 or 4066 has its ground connected to the same (negative) ground as the keypad system, then it should work just fine.

Thanks for the useful info!

There is something I don't really get, which is "selecting" the rows and columns.

So from what I understand, with the IC that I have, you tell it a 3 digit binary number with the 3 input pins (A, B, C) and it connects the corresponding output (for ex., if you give A 1, B 1, C 0, so 110 it selects output #6) to 5V. Please correct me if I'm wrong!

So does this "selecting" you are talking about mean using one IC to give a column pin 5V and another IC to give the corresponding row 5V so they are technically connected? If the Arduino and my device share a ground, wouldn't this damage it?

Sorry for any noob questions, but that's what I am. :slight_smile:

Edit: To answer your first question, I would want to use all 33 keys.

Download the 74HC4051 data sheet and read it. All the answers about how it operates are in it. If you can't understand it, your chances of being able to use it are small.

Thanks all for your answers, but it seems like I'm still on my own here. I guess the project will just have to wait another day (or week, or month, or year).

The best case scenario for you is that you can simply hold 2 pins on that 12 pin connector LOW , simultaneously, for a few few milliseconds to simulate a key press.

For example, to simulate the pressing of the ‘STL’ key (B10), you would hold pins 5 and 9 LOW.
In that case, you would need some sort of port expander. Two 4051 type mux/demux chips may work. It is not inconceivable that 12 spare Arduino pins would do ( maybe with 12 opto couplers or just transistors)

However, all this will work only if:

  1. The keyboard scanning is as I outlined in post #1
  2. The voltage the device the key board is connected to and the Arduino are in the same range (But there may also be work around even if the keyboard is driven at say 12 volts )
  3. You create a common ground between the keyboard’s device and the Arduino.

The first thing is to check is point 1. above. Have you access to an oscilloscope?

I don’t have an oscilloscope, but I’ve made some measurements. The ribbon coming from the keypad is actually 14 pin and the updated wiring is attached, with the first part being ordered by button number and the second part, by pin number.

The “ON” key (pin 13) is its separate pin, which, when connected to GND (pin 14) and held for a few seconds powers on the device.

Between GND and the pins, I have measured as follows:

#1 - #7  : I can't measure a stable voltage, I suspect these pins are powered one after the other in a cycle
#8 - #12 : Completely stable 5V
#13      : Also changing fast like #1 - #7 (as mentioned above, this is the "ON" button)
#14      : GND

Thanks for your help so far! I really, really want to use the ICs that I bought and not “port expanders”.

MP55 KEYBOARD.txt (1.4 KB)

Can you post a picture of this keyboard or can you find some link to it online ?
Can you also link to the article which gave you the idea of using a CD4051 for such a project. I'm reasonably sure it can be used, but it has only 8 ports so you may need a second one (depending on the keyboard scanning mechanism)

OK. On pins #8 to #12 which you say have a stable 5 volts, can you put a resistor (say 10k or higher) between the pin and ground, then measure the voltage across the resistor. Do that for each pin in turn. I'm trying to work out if these pins are on the 5volt rail, or are simply pulled up to 5volts.

I have taken two pictures:

I doubt you could find anything about this device on the internet, as it is very obsolete and undocumented.

Where I've seen the CD4051 used:

  1. Suggested in reply #1, but without follow-up
  2. Suggested in reply #6, but OP used optocouplers for his final circuit

FYI I have 10 CD4051 ICs.

By pulling a pin which shows 5V with a 10K resistor to GND, the voltage on it drops to ~3.3V.

Have you read the data sheet yet?


Ok, folks will not go out to imgur to look at pictures.
Select More:Modify in your post, and Attach the pictures to your post.
You can use the Insert an Image button on the menu, just about the middle of the menu, looks like a TV.

OK. It looks like the pins #8 to #12 are 4.7K away from the 5volt rail.

Without at an oscilloscope, it is not absolutely clear how the scanning is working, however I guess it is similar to what I outlined earlier.

If it was mine, I'd probably take a risk and use two 1K resistors, one between pin 5 and ground. The other between pin 9 and ground. With luck, that will simulate the press of the 'STL' (B10) key.

You could probably go lower with the resistors if that doesn't do anything but not less than 470R at the moment.

Surface mount connector for the ribbon cable?
That's awkward.
What's the other end?
Perhaps a make an adapter board that the other end can plug into, and then back out t to where ever it goes. That will give you access points to wire your circuit to.

A button press will be the same as connecting two wires together.
You will have to do some experimenting to see which pairs correspond to a button press.

When you have that worked out, then you could perhaps have a solution where one CD4051 connects to up to 8 wires, and another CD4051 connects to up to 8 other wires. Then selecting say channel 1 for the first and channel 3 for the second will equate to the 3rd button in row 1. 2 and 4 might be 4th button in row 2. Does that make sense?

Surface mount connector for the ribbon cable?
That's awkward.

CrossRoads, the ribbon cable was removed from the socket in the pictures. The other IDE-style ribbon you are seeing was soldered on my me to have pin headers at the other end and I put some hot glue over them so the small solder joints wouldn't rip off.

You will have to do some experimenting to see which pairs correspond to a button press.

I have already done that, the pin pairs are attached in reply #9.

Does that make sense?

If you could, please, look in the attached file mentioned above and give me examples that apply to my device (for ex., the key "PF" is pin 2 and 8, how would I "virtually press" it?).


If it was mine, I'd probably take a risk and use two 1K resistors, one between pin 5 and ground. The other between pin 9 and ground. With luck, that will simulate the press of the 'STL' (B10) key.

You could probably go lower with the resistors if that doesn't do anything but not less than 470R at the moment.

6v6gt, I don't have 1k resistors handy right now, could I use the 10ks I used earlier?


This is what I was describing, for say a 4x5 matrix.
This assumes you have access to the same Power & Gnd the keyboard runs from so the CD4051 can be run at the same levels.