Using the CD74HC4067 multiplexer with push buttons

Hello, Im trying to use the CD74HC4067 multiplexer with push buttons. My goal is simple, to connect push buttons to the CD74HC4067 and have it print each channels state, if the button on that specific channel is "pressed" or "not pressed". I have tried several different codes and I have been researching for about a month now with no success. I have tried a ton of different librarys and even tried to do the code for myself. Im in desperate need of some help.

Here is the code im trying to get to work:

//Mux control pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;

//Mux in “SIG” pin
int SIG_pin = 0;

void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);

digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);

Serial.begin(9600);
}

void loop(){

//Loop through and read all 16 values
//Reports back Value at channel 6 is: 346
for(int i = 0; i < 16; i ++){ 
  Serial.print("Value at channel "); 
  Serial.print(i); Serial.print("is : "); 
  Serial.println(readMux(i)); 
  delay(1000); 
  } 
} 
  int readMux(int channel){ 
    int controlPin[] = {s0, s1, s2, s3}; 
    int muxChannel[16][4]={ {0,0,0,0}, 
    {1,0,0,0}, 
    {0,1,0,0}, 
    {1,1,0,0}, 
    {0,0,1,0}, 
    {1,0,1,0}, 
    {0,1,1,0}, 
    {1,1,1,0}, 
    {0,0,0,1}, 
    {1,0,0,1}, 
    {0,1,0,1}, 
    {1,1,0,1}, 
    {0,0,1,1}, 
    {1,0,1,1}, 
    {0,1,1,1}, 
    {1,1,1,1} }; 
    //loop through the 4 sig 
    for(int i = 0; i < 4; i ++){ 
      digitalWrite(controlPin[i], muxChannel[channel][i]); 
      } 
      //read the value at the SIG pin 
      int val = analogRead(SIG_pin); //return the value 
      return val; 
      }

Here is the schematic:

there are several errors in your code

for(int readMux(int channel)){

i think you want

for(int channel = 0; channel < 16; channel++){

aren't there only 4, not 6 select pins

for(int i = 0; i < 6; i ++){ //loop through the 4 sig

and what are you returning the value to from loop()? did you intend the above code to be a sub-function?

  return muxVal; //return the value

Anton-H:
Hello, Im trying to use the CD74HC4067 multiplexer with push buttons. My goal is simple, to connect push buttons to the CD74HC4067 and have it print each channels state, if the button on that specific channel is "pressed" or "not pressed". I have tried several different codes and I have been researching for about a month now with no success. I have tried a ton of different librarys and even tried to do the code for myself. Im in desperate need of some help.

Here is the code im trying to get to work:

//Mux control pins

int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;

//Mux in “SIG” pin
int SIG_pin = 0;

void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);

digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);

Serial.begin(9600);
}

void loop(){

//Loop through and read all 16 values
//Reports back Value at channel 6 is: 346
for(int i = 0; i < 16; i ++){
 Serial.print("Value at channel ");
 Serial.print(i); Serial.print("is : ");
 Serial.println(readMux(i));
 delay(1000);
 }
}
 int readMux(int channel){
   int controlPin[] = {s0, s1, s2, s3};
   int muxChannel[16][4]={ {0,0,0,0},
   {1,0,0,0},
   {0,1,0,0},
   {1,1,0,0},
   {0,0,1,0},
   {1,0,1,0},
   {0,1,1,0},
   {1,1,1,0},
   {0,0,0,1},
   {1,0,0,1},
   {0,1,0,1},
   {1,1,0,1},
   {0,0,1,1},
   {1,0,1,1},
   {0,1,1,1},
   {1,1,1,1} };
   //loop through the 4 sig
   for(int i = 0; i < 4; i ++){
     digitalWrite(controlPin[i], muxChannel[channel][i]);
     }
     //read the value at the SIG pin
     int val = analogRead(SIG_pin); //return the value
     return val;
     }

there are no errors in your code.

However...

According to your schematic you also have EN = 7

so you need to initialise that pin as well ie

pinMode(en, OUTPUT);
digitalWrite(en, LOW); //if in setup, should permanently enable the board.

then you have SIG_PIN wired to pin 3 which is NOT an analog pin. so again you need to initialise it

pinMode(SIG_PIN, INPUT);

then in 'int readMux(int channel)'

change

int val = analogRead(SIG_pin); //return the value

to

int val = digitalRead(SIG_pin); //return the value

so something like this in code (compiles NOT tested!)

//Mux control pins
int en = 7;
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;


//Mux in "SIG" pin
int SIG_pin = 3;

void setup() {
  pinMode(en, OUTPUT);
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  
  pinMode(SIG_pin, INPUT);

  digitalWrite(en, LOW);
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  Serial.begin(9600);
}

void loop() {

  //Loop through and read all 16 values
  for (int i = 0; i < 16; i ++) {
    Serial.print("Value at channel ");
    Serial.print(i); Serial.print("is : ");
    Serial.println(readMux(i));
    delay(1000);
  }
}

int readMux(int channel) {
  int controlPin[] = {s0, s1, s2, s3};
  int muxChannel[16][4] = { {0, 0, 0, 0},
                            {1, 0, 0, 0},
                            {0, 1, 0, 0},
                            {1, 1, 0, 0},
                            {0, 0, 1, 0},
                            {1, 0, 1, 0},
                            {0, 1, 1, 0},
                            {1, 1, 1, 0},
                            {0, 0, 0, 1},
                            {1, 0, 0, 1},
                            {0, 1, 0, 1},
                            {1, 1, 0, 1},
                            {0, 0, 1, 1},
                            {1, 0, 1, 1},
                            {0, 1, 1, 1},
                            {1, 1, 1, 1}
                          };
  //loop through the 4 sig
  for (int i = 0; i < 4; i ++) {
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }
  //read the value at the SIG pin
  int val = digitalRead(SIG_pin); //return the value
  return val;
}

hope that helps

hc4067.cpp: In function 'void loop()':
hc4067.cpp:29:30: warning: declaration of 'int readMux(int)' has 'extern' and is initialized
for(int readMux(int channel)){
^
hc4067.cpp:29:31: error: expected ';' before ')' token
for(int readMux(int channel)){
^
hc4067.cpp:29:31: error: expected primary-expression before ')' token
hc4067.cpp:29:31: error: expected ';' before ')' token

I have solved the issue by switching the multiplexer, aparently i had managed to burn the other one. Thanks för all the replies.