Issue with detecting button presses using a 16-pin multiplexer (CD74HC4067) with Arduino on Wokwi platform

Hello everyone! I'm new to Arduino world and I'm working on a University project.
I'm trying to simulate an Arduino circuit using a 16-pin multiplexer (CD74HC4067) to expand the number of pin slots on my Arduino Uno. I want to attach push buttons to the multiplexer and detect button presses using the Arduino. However, I'm facing an issue where the Arduino is not detecting any button presses.

I have written the following code to read the button states:

const int s0 = 8;
const int s1 = 9;
const int s2 = 10;
const int s3 = 11;
const int sig = 2; 

boolean buttonActive[] = {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};

int buttonState[] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
int lastButtonState[] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};   // the previous reading from the input pin

long lastDebounceTime[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time;

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

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

  Serial.begin(9600);
}

void loop() {

  for(int i = 0; i < 16; i++) {
    buttonState[i] = cdRead(i);
    Serial.println(buttonState[i]);

    if (buttonState[i] == 1 && lastButtonState[i] == 0) {
      lastDebounceTime[i] = millis();
    } 

    if((millis() - lastDebounceTime[i]) > debounceDelay) {
      if(buttonState[i] == HIGH && buttonActive[i] == false) {
        Serial.print("Pressed: ");
        Serial.println((int) i);
        buttonActive[i] = true;
      } 
      else if(buttonState[i] == LOW) {
        buttonActive[i] = false; 
      }
    }

    lastButtonState[i] = buttonState[i];
  }

  delay(500);
}

void cdSelect(unsigned int ch) {

  switch (ch) {
  case 0: 
    digitalWrite(s0, LOW); 
    digitalWrite(s1, LOW); 
    digitalWrite(s2, LOW); 
    digitalWrite(s3, LOW); 
    break;
  case 1: 
    digitalWrite(s0, HIGH); 
    digitalWrite(s1, LOW); 
    digitalWrite(s2, LOW); 
    digitalWrite(s3, LOW); 
    break;
  case 2: 
    digitalWrite(s0, LOW); 
    digitalWrite(s1, HIGH); 
    digitalWrite(s2, LOW); 
    digitalWrite(s3, LOW); 
    break;
  case 3: 
    digitalWrite(s0, HIGH); 
    digitalWrite(s1, HIGH); 
    digitalWrite(s2, LOW); 
    digitalWrite(s3, LOW); 
    break;
  case 4: 
    digitalWrite(s0, LOW); 
    digitalWrite(s1, LOW); 
    digitalWrite(s2, HIGH); 
    digitalWrite(s3, LOW); 
    break;
  case 5: 
    digitalWrite(s0, HIGH); 
    digitalWrite(s1, LOW); 
    digitalWrite(s2, HIGH); 
    digitalWrite(s3, LOW); 
    break;
  case 6: 
    digitalWrite(s0, LOW); 
    digitalWrite(s1, HIGH); 
    digitalWrite(s2, HIGH); 
    digitalWrite(s3, LOW); 
    break;
  case 7: 
    digitalWrite(s0, HIGH); 
    digitalWrite(s1, HIGH); 
    digitalWrite(s2, HIGH);
    digitalWrite(s3, LOW);
    break;
  case 8: 
    digitalWrite(s0, LOW); 
    digitalWrite(s1, LOW); 
    digitalWrite(s2, LOW);
    digitalWrite(s3, HIGH);
    break;
  case 9: 
    digitalWrite(s0, HIGH); 
    digitalWrite(s1, LOW); 
    digitalWrite(s2, LOW);
    digitalWrite(s3, HIGH);
    break;
  case 10: 
    digitalWrite(s0, LOW); 
    digitalWrite(s1, HIGH); 
    digitalWrite(s2, LOW);
    digitalWrite(s3, HIGH);
    break;
  case 11: 
    digitalWrite(s0, HIGH); 
    digitalWrite(s1, HIGH); 
    digitalWrite(s2, LOW);
    digitalWrite(s3, HIGH);
    break;
  case 12: 
    digitalWrite(s0, LOW); 
    digitalWrite(s1, LOW); 
    digitalWrite(s2, HIGH);
    digitalWrite(s3, HIGH);
    break;
  case 13: 
    digitalWrite(s0, HIGH); 
    digitalWrite(s1, LOW); 
    digitalWrite(s2, HIGH);
    digitalWrite(s3, HIGH);
    break;
  case 14: 
    digitalWrite(s0, LOW); 
    digitalWrite(s1, HIGH); 
    digitalWrite(s2, HIGH);
    digitalWrite(s3, HIGH);
    break;
  case 15: 
    digitalWrite(s0, HIGH); 
    digitalWrite(s1, HIGH); 
    digitalWrite(s2, HIGH);
    digitalWrite(s3, HIGH);
    break;
  default: 
    break;
  } 

  
}

unsigned short cdRead(unsigned int ch) {
  cdSelect(ch);
  return digitalRead(sig); 
}

I am using the Wokwi platform for simulation. Could this be related to the issue? Is there anything specific I need to consider when simulating a multiplexer using the WoWki platform?

I link the circuit image (I was testing the circuit only with 2 buttons connected).

I can share you also the link:
https://wokwi.com/projects/380511599209956353

Any help or suggestions would be greatly appreciated. Thank you!

I have connected the push buttons to the multiplexer and used the cdRead function to read the button states. However, when I press a button, the Arduino never detects it. I have checked the connections and they seem to be correct.

1 Like

Build it in real.

Could be the way you wired those 4-pin buttons.

With that type of button, each pin is connected to another pin when the button is not pressed, and when the button is pressed, all 4 pins are connected. But it's hard to know which pins are connected to which other pins when the button is not pressed. The simple solution is to use 2 diagonally opposite pins, it does not matter which diagonal pair you use. But don't use the other diagonally opposite pair of pins, connect nothing to those.

Question: why is the multiplexer module not plugged into the breadboard?

Please check the datasheet how the pin EN (chip enable) need to be wired.

It needs to pulled high or low.

1 Like

I think a digital port expander such as the PCF8575 or PCA9555 would be a better choice. You can check 8 buttons and debounce in one pass. The code would be much simpler and faster. I would use the buttons to switch to ground. Depending on the part the pull up can be internal.

1 Like

I held off saying it because it could be considered "off-topic", but I agree with @gilshultz about pcf8575. Other options include connecting your 16 buttons in a 4x4 matrix. This needs only 8 pins, which can be arduino pins or can be pcf8574 pins, for example.

1 Like

thank you!
I did not connect to the breadboard because I couldn't rotate it :sweat_smile:.

Yeah, I've tried both, but nothing happened :frowning:

Anyway, thanks!

Hi, thank you! I think I will follow your advice!

1 Like

I tested this circuit with just the Arduino and the CD74HC...
" Select - Wokwi ESP32, STM32, Arduino Simulator

I put all entries in HIGH, I put E in LOW, as indicated in the datasheet page. 2, I mounted an LED on the output and selected an input.
The LED didn't light up, so it seems to me that there is a problem with the simulator with this chip.

Ref Datasheet https://www.ti.com/lit/ds/symlink/cd74hc4067.pdf

Using analogRead() instead digitalRead(), works.
See simulation:
"multiplexer_2 Copy - Wokwi ESP32, STM32, Arduino Simulator

Press any button and see result....

Thank you a lot!

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