Help with CD74HC4067 on matrix keyboard

Hello, sorry for my bad English, but it is not my native language and I do not speak it well.
I come to the forum because I need help, I can't get a matrix keyboard to work (8 columns x 2 rows, in the future it will be 16 columns and 3 rows) in conjunction with a CD74HC4067 multiplexer.

I am using the CD74HC4067 with the following distribution to Arduino Pro Micro:

next = 5
s3 = 9
s2 = 8
s1 = 7
s0 = 6
in = gnd
vdc = 5v
gnd = gnd

the matrix keyboard has the columns connected to 8 pins from c15 to c8 (on the multiplexer) and the 2 rows are connected to pins 15 and 14 directly on the arduino pro micro.

the keyboard matrix works perfectly connected directly to the arduino, but when trying to modify the code to use it with the already mentioned distribution, I couldn't do it, logically because there is some error in the logic that I am applying to elaborate the code.

please if someone can help me or give me an idea to use the multiplexer as matrix keyboard columns.

Now I leave the code that I have not been able to carry out correctly.

#include <Keypad.h>

const int muxSIG = 5;
const int muxS0 = 6;
const int muxS1 = 7;
const int muxS2 = 8;
const int muxS3 = 9;

int SetMuxChannel(byte channel){
   digitalWrite(muxS0, bitRead(channel, 0));
   digitalWrite(muxS1, bitRead(channel, 1));
   digitalWrite(muxS2, bitRead(channel, 2));
   digitalWrite(muxS3, bitRead(channel, 3));
}


byte c8 = SetMuxChannel(8);
byte c9 = SetMuxChannel(9);
byte c10 = SetMuxChannel(10);
byte c11 = SetMuxChannel(11);
byte c12 = SetMuxChannel(12);
byte c13 = SetMuxChannel(13);
byte c14 = SetMuxChannel(14);
byte c15 = SetMuxChannel(15);
//uint8_t c15 = SetMuxChannel(15);

const byte ROWS = 2; // rows
const byte COLS = 8; // columns

byte rowPins[ROWS] = {15,14}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {c8,c9,c10,c11,c12,c13,c14,c15}; //connect to the column pinouts of the kpd


byte keys[ROWS][COLS] = {
{1,2,3,4,5,6,7,8},
{9,10,11,12,13,14,15,16}
};


Keypad kpdA = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

String msg;
int data;


void setup() {
   Serial.begin(115200);
   pinMode(muxS0, OUTPUT);
   pinMode(muxS1, OUTPUT);
   pinMode(muxS2, OUTPUT);
   pinMode(muxS3, OUTPUT);
   //pinMode(muxSIG, INPUT_PULLUP);
   
    
    msg = "";
}


void loop() {

    // Fills kpd.key[ ] array with up-to 10 active keys.
    // Returns true if there are ANY active keys.
    if (kpdA.getKeys())
    {
        for (int i=0; i<LIST_MAX; i++)   // Scan the whole key list.
        {
            if ( kpdA.key[i].stateChanged )   // Only find keys that have changed state.
            {
                if ( (kpdA.key[i].kstate) == PRESSED) {  // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
                data = byte(kpdA.key[i].kchar);
                msg = " PRESSED.";
                Serial.print("Key ");
                Serial.print(data);
                Serial.println(msg);
                }
                
                if ( (kpdA.key[i].kstate) == RELEASED) {
                data = byte(kpdA.key[i].kchar);
                msg = " RELEASED.";
                Serial.print("Key ");
                Serial.print(data);
                Serial.println(msg);
                
                }
            }
        }
    }



}  // End loop

Post a schematic.

Sorry, I don't know how to make electrical diagrams, with their symbols and technicalities. That's why I attach a drawing (pretty bad really, I apologize for that too), but I hope it can be useful.

You have 16 buttons and a 16:1 multiplexer. Why add the complexity of a diode matrix circuit?

'SetMuxChannel()' does not return any value. If you have warnings enabled in the compiler, it will tell you.

hello, first of all thank you for your help and comments :).

answering your question.

I really need a number of buttons of 16x3, but I am using 8x2, to understand the programming and circuit in an easier way and when I understand it, I will expand the number of buttons to what I really need, that is the reason.

I have no warnings in the compiler.
How could I indicate in the code the distribution of the pins?
my logic has not been successful therefore I am stuck 100%.
Maybe it is not possible to do what I am trying to do?

I really appreciate your time and help.

Your code is part traditional matrix and has some guesses it looks like about how to do a matrix keyboard with multiplexers.

Fortunately your friend google can help out, as this problem has been so,fed many times.

For now, google

multiplexer matrix keyboard

and poke around. When I'm back at the big rig, I will find a recent thread in these fora I recall discussing doing exactly what you are up to.

In the IDE, use the Preferences panel to find and crank up the level of warnings and verbosity of the compiler to the max, it can help catch out errors and questionable code.

HTH/L8R

a7

OK this

might not be too much help and the link below will go over some aproaches of interest, including just using the control surface library, which TBH looks like it can do anything. If you aren't interested in writing the scanning software yourself or (I think it would be necessary) to modify the keypad library to use the mux:

HTH

a7

What is that number?

16 x 3 = 48, additionally I need some pins for some encoders, screens and 2 joysticks, so it is almost mandatory to learn how to use a multiplexer and matrices.

Why? Most key switch arrays like keyboards don't use a multiplexer. If you want to minimize Arduino pins, the standard way is to use shift register IC's, or dedicated I/O expander IC's.

thanks for the data, I will take the day to try to find information, since I have seen several posts in the forum, in other forums and youtube videos and I only find unfinished projects (adding the difficulty of the language that many times google does not help enough)

Do you require "n-key rollover"? In other words, must you detect simultaneous key presses? Are any of the switches toggles, or are they all momentary contact?

If you must use multiplexers, why not use two? One for rows, one for columns. That would give you 256 keys. :grin:

Or, use a 1-n data selector IC like a HC138 to drive one side of the matrix, 8 rows with that, 16 columns with the multiplexer, 128 keys.

Using 2 multiplexers in the matrix sounds great, but I can't even use 1 in a matrix so it's impossible for me to do it. :grin: :grin: :grin:

For the rest, a "1-n data selector IC" I couldn't understand due to translation issues, what it is? It would be similar to a "TCA9548A"...

thanks for helping.

I think 74HC138 translates very well... :slight_smile: In fairness, I abbreviated it by habit.

You left some of my questions without an answer...

I'm sorry I get a little confused between trying to learn, translating what I can't understand and putting together the code, etc...

I'm trying to make a digital piano, midi type, and I also want to do a couple of things (which also use a large matrix later), so I need to make a keyboard in which several keys are pressed simultaneously and avoid ghosting.

If I forget to answer something, I'm sorry, please ask again, English is difficult for me :tired_face: :tired_face: :tired_face:

Make your life easier, and find the easiest design to implement your matrix before you invest huge amounts of time in building it. That means, research, research, research. Look at how other systems have done it.

Real life systems have not used a CMOS multiplexer like that one for the purposes of keyboard scanning, in several decades.

okay, thanks for letting me know so I don't keep wasting my time trying something almost impossible to do.

The problem is that I can't find information or finished projects from other users, especially the elements they use to make large arrays.

but hey, at least it served to rule out this option.

Bypass the amateur hacky stuff. Download some service manuals of synth keyboards. Check out this recent thread
https://forum.arduino.cc/t/keyboard-matrix-74hc138/1018597

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.