Strange digital inputs with Arduino and CD74HC4067

So I'm trying to have a Joystick like input for my PC with an Arduino Micro with around 20 inputs so I had to add a CD74HC4067 mux.
I connected the mux as shown below:
arduino-multiplexor-cd74hc4067-esquema
And my buttons directly to arduino's VCC and to each one of the 16 mux inputs (Many of my bottons just have two connections so I can't wire it to the ground). This is the code I'm using:

const int muxSIG = A0;
const int muxS0 = 8;
const int muxS1 = 9;
const int muxS2 = 10;
const int muxS3 = 11;

int SetMuxChannel(byte channel)
{
   digitalWrite(muxS0, bitRead(channel, 0));
   digitalWrite(muxS1, bitRead(channel, 1));
   digitalWrite(muxS2, bitRead(channel, 2));
   digitalWrite(muxS3, bitRead(channel, 3));
}
 
void setup()
{
   pinMode(muxS0, OUTPUT);
   pinMode(muxS1, OUTPUT);
   pinMode(muxS2, OUTPUT);
   pinMode(muxS3, OUTPUT);
   Serial.begin(9600);
   delay(1000);
}
 
void loop()
{
   for (byte i = 0; i < 16; i++)
   {
      SetMuxChannel(i);
      byte muxValue = digitalRead(muxSIG);
      Serial.print(i);  
      Serial.print(": ");
      Serial.print(muxValue);
      Serial.print(" | ");
   }   
   delay(500);
      Serial.println();
}

When I play it though, the iputs change constantly without my pushing any button Like this in a totally random way:

0: 1 | 1: 1 | 2: 1 | 3: 1 | 4: 1 | 5: 1 | 6: 1 | 7: 1 | 8: 1 | 9: 1 | 10: 1 | 11: 1 | 12: 1 | 13: 1 | 14: 1 | 15: 1 |
0: 1 | 1: 1 | 2: 1 | 3: 1 | 4: 1 | 5: 1 | 6: 1 | 7: 1 | 8: 1 | 9: 1 | 10: 1 | 11: 1 | 12: 1 | 13: 1 | 14: 1 | 15: 0 |
0: 1 | 1: 1 | 2: 1 | 3: 0 | 4: 1 | 5: 0 | 6: 0 | 7: 0 | 8: 1 | 9: 1 | 10: 1 | 11: 0 | 12: 1 | 13: 0 | 14: 0 | 15: 0 |

If I push a button I can see things happening but not anything that makes sense.
What am I doing wrong here?

From your description there appears to be nothing connected to the input when the switch is open. If so you are reading noise on the input, which could be anything. You need pullup or pulldown (depending on how the switches are connected) resistors so that there is a defined level on the input.

pls show graphical what you mean. i (and person above too) think you have no voltage on the pin if button not pressed so its state is undefined. i recomment using pull up resistor and button should connect pin to GND.

@axteroide
Just connect a single 5.1K resistor between A0 and GND

2 Likes

Clever, the CD74HC4067 is an analog bidirectional switch so the resistor can be on the common side. How about just making A0 (or any digital pin) INPUT_PULLUP and use the internal pullup resistor with no additional parts? You have to invert the sense of the switch (closed is LOW).

Edit: To clarify the switch is connected between the input pin and ground.

@axteroide has at least some switches that cannot be connected to ground. All are connected to Vcc.

a7

Thank you!
I'll try this as it seems the simplest solution. As soon as I buy the resistor as I don't have many and check it works I'll mark the answer as the solution.

It does not have to be exacly 5.1K Anything between 3.3K and 10K would work.
If you have two 10K resistors, put them in parallel to make 5K

1 Like

I could find the 2 10k resistors and tried your solution. It was stable but there seemed to be son kind of electrical short between some of the buttons. If I pressed 8 for example this would be the exit:
0: 1 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 | 8: 0 | 9: 0 | 10: 0 | 11: 0 | 12: 0 | 13: 0 | 14: 0 | 15: 0 |
0: 1 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 | 8: 0 | 9: 0 | 10: 0 | 11: 0 | 12: 0 | 13: 0 | 14: 0 | 15: 0 |
0: 1 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 | 8: 0 | 9: 0 | 10: 0 | 11: 0 | 12: 0 | 13: 0 | 14: 0 | 15: 0 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 | 8: 0 | 9: 0 | 10: 0 | 11: 0 | 12: 0 | 13: 0 | 14: 0 | 15: 0 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 | 8: 0 | 9: 0 | 10: 0 | 11: 0 | 12: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 | 15: 0 |
So the HIGH value changes between input 1 and 8, if I disconnected the arduino and connected it again it would maybe change and do the same thing with 7 and 14 and so on, some inputs would look stable, others wouldn't. I checked and couldn't find a short on those pins.

Any ideas of something I could try?
Otherwise I'll probably try the pull up or down resistor.

Thanks for your answers!

Use the Arduino board and multiplexer, but supply your own input using a jumper wire to play the roll of the switches you are planning to use later.

Here since you have been forced to use a pull down configuration, the jumper could be used like a switch by connecting Vcc to the mux inputs one by one.

To rule out something wonky about the real inputs you want to deal with.

Be careful with any jumper you are playing around with in a live circuit, or power diwn, place the jumper and power back up and one by one check that the inputs are not somehow messing with each other.

a7

1 Like

Try puting a delay(1) between SetMuxChannel and when you do the digitalRead

Hi again! Sorry for the delay but I haven't able to spend much time in this project lately.

I tried adding the delay and also added Pull-Down resistors but seems like I'm having the exact same problem.

I'm starting to think that the mux might be malfunctioning or something.
Also I've noticed the difference seems to be of alwas 8 inputs, so if I press button connected to 0, inputs 0 and 8 get the high, if I press 1 inputs 1 and 9, if I press 8, inputs 8 and 15 and so on...

I'm completely lost why this can be happening.

Thank you for all your help so far!

Well is usually something that couldn't possibly be wrong but is.
Bad or missing ground somewhere, loose or broken wire, bad solder joint, cheap breadboard, etc...

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