# Strange digital inputs with Arduino and CD74HC4067

**URL:** <https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291>\
**Category:** Programming\
**Created:** [December 27, 2023, 4:14pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291 "2023-12-27T16:14:52Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![axteroide](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/axteroide/32/1028141_2.png) [@axteroide](https://forum.arduino.cc/u/axteroide)\
**Post date:** [December 27, 2023, 4:14pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/1 "2023-12-27T16:14:52Z")

</div>

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](https://europe1.discourse-cdn.com/arduino/original/4X/2/d/4/2d410256744799a5a099ff4d925f81de051c03be.png)  
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:

```arduino
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?

---

<div class="post-metadata">

**Author:** ![oldcurmudgeon](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oldcurmudgeon/32/1280882_2.png) [@oldcurmudgeon](https://forum.arduino.cc/u/oldcurmudgeon)\
**Post date:** [December 27, 2023, 5:47pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/2 "2023-12-27T17:47:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [December 27, 2023, 5:53pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/4 "2023-12-27T17:53:59Z")

</div>

> [@axteroide](#):
>
> And my buttons directly to arduino's VCC and to each one of the 16 mux inputs

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.

 ![изображение](https://europe1.discourse-cdn.com/arduino/original/4X/f/4/0/f40a9d7e2a26ee54c377fbcc633d139f2ddc3b49.png)

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [December 27, 2023, 6:07pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/5 "2023-12-27T18:07:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oldcurmudgeon](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oldcurmudgeon/32/1280882_2.png) [@oldcurmudgeon](https://forum.arduino.cc/u/oldcurmudgeon)\
**Post date:** [December 27, 2023, 10:09pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/6 "2023-12-27T22:09:08Z")

</div>

> [@jim-p](#):
>
> Just connect a single 5.1K resistor between A0 and GND

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.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [December 28, 2023, 1:56am UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/7 "2023-12-28T01:56:56Z")

</div>

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

a7

---

<div class="post-metadata">

**Author:** ![axteroide](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/axteroide/32/1028141_2.png) [@axteroide](https://forum.arduino.cc/u/axteroide)\
**Post date:** [December 28, 2023, 11:34am UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/8 "2023-12-28T11:34:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [December 28, 2023, 12:00pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/9 "2023-12-28T12:00:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![axteroide](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/axteroide/32/1028141_2.png) [@axteroide](https://forum.arduino.cc/u/axteroide)\
**Post date:** [December 30, 2023, 5:09pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/10 "2023-12-30T17:09:41Z")

</div>

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 |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 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: 1 | 9: 0 | 10: 0 | 11: 0 | 12: 0 | 13: 0 | 14: 0 | 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!

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [December 30, 2023, 5:19pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/11 "2023-12-30T17:19:33Z")

</div>

> [@axteroide](#):
>
> Any ideas of something I could try?

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

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [December 30, 2023, 5:53pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/12 "2023-12-30T17:53:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![axteroide](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/axteroide/32/1028141_2.png) [@axteroide](https://forum.arduino.cc/u/axteroide)\
**Post date:** [January 31, 2024, 12:03pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/13 "2024-01-31T12:03:53Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [January 31, 2024, 1:27pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/14 "2024-01-31T13:27:12Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 29, 2024, 1:27pm UTC](https://forum.arduino.cc/t/strange-digital-inputs-with-arduino-and-cd74hc4067/1204291/15 "2024-07-29T13:27:33Z")

</div>

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