# Analog multiplexor reading random values (CD74HC4067)

**URL:** <https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853>\
**Category:** LEDs and Multiplexing\
**Created:** [September 14, 2019, 3:28pm UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853 "2019-09-14T15:28:44Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![VaclavProch](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@VaclavProch](https://forum.arduino.cc/u/VaclavProch)\
**Post date:** [September 14, 2019, 3:28pm UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/1 "2019-09-14T15:28:44Z")

</div>

Hello, I've bought a CD74HC4067 multiplexer for my project involving an array of photoresistors, but whenever I've tried to read an output I got almost identical values on all pins usually 2-4 numbers apart which constantly fluctuated between 300 and 330.  
So I've decided to just make the simplest possible circuit to test the multiplexer shown on the image bellow which should output a simple 0 as the C0 pin is connected to the ground, but the board keeps reading the same values (also shown bellow).  
I have no idea what's going on so I would love to get some thoughts from anyone more competent than me on what might be causing the problem.  
Documentation for the exact multiplexer is [here](https://drive.google.com/file/d/0B4B30jzMyzG8bnA2clBXU3A3emdfU1huOGVMeTdZZVZhNEJB/view)

the code of the simple circuit is as following:

```cpp
void setup() {
 Serial.begin(9600);
}

void loop() {
 delay(500);
 digitalWrite(4, 0);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 int val = analogRead(A0);
 Serial.println(val);
}

```

 ![COM5screenshot.png](https://europe1.discourse-cdn.com/arduino/original/4X/d/2/b/d2bcaa2a4279edc223de5adee5a7bb04a201bfe7.png)  
 ![fritzing0.png](https://europe1.discourse-cdn.com/arduino/original/4X/5/c/f/5cf54c5f10c3048219b70bf90aa156146fa0fc7b.png)  
(updates to for code tags and to show images made by moderator)

 ![COM5screenshot.png](https://europe1.discourse-cdn.com/arduino/original/4X/d/2/b/d2bcaa2a4279edc223de5adee5a7bb04a201bfe7.png)

 ![fritzing0.png](https://europe1.discourse-cdn.com/arduino/original/4X/5/c/f/5cf54c5f10c3048219b70bf90aa156146fa0fc7b.png)

---

<div class="post-metadata">

**Author:** ![CrossRoads](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/crossroads/32/214033_2.png) [@CrossRoads](https://forum.arduino.cc/u/CrossRoads)\
**Post date:** [September 14, 2019, 3:49pm UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/2 "2019-09-14T15:49:19Z")

</div>

You need to declare 4,5,6,7 as OUTPUTs in setup()  
pinMode (4, OUTPUT);

They are INPUT by default, writing a LOW (or 0) to an input just disables the internal pullup resistor.

---

<div class="post-metadata">

**Author:** ![VaclavProch](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@VaclavProch](https://forum.arduino.cc/u/VaclavProch)\
**Post date:** [September 14, 2019, 4:35pm UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/3 "2019-09-14T16:35:43Z")

</div>

Oh I forgot about that in the simple circuit, but after I added this at the start of my setup:

```cpp
pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

```

I got the very same output.

Any other tips?

---

<div class="post-metadata">

**Author:** ![HKJ-lygte](https://avatars.discourse-cdn.com/v4/letter/h/c68b51/32.png) [@HKJ-lygte](https://forum.arduino.cc/u/HKJ-lygte)\
**Post date:** [September 14, 2019, 5:00pm UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/4 "2019-09-14T17:00:35Z")

</div>

What do you expect with unconnected inputs?

Add a 1Mohm pulldown on you analog pin and then feed voltage into some of the mux inputs to check.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 14, 2019, 5:36pm UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/5 "2019-09-14T17:36:25Z")

</div>

Yes a lot of beginners get fooled with this.

On the input of the A/D is a capacitor that forms a sample and hold circuit. In front of that is a data selector, just like your 74HC4067.

When the sample and hold signal is switch to a new input, that input has to charge or discharge the capacitor to the new voltage. If that new input is not connected to anything or if the voltage is a high impedance source, then the capacitor will not get the right voltage on it, and so the reading will be the same or only slightly different.

One solution is to read each input twice and only use the second reading. This allows the voltage on the capacitor more time to charge up.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [September 14, 2019, 10:46pm UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/6 "2019-09-14T22:46:40Z")

</div>

> Grumpy\_Mike:  
> When the sample and hold signal is switch to a new input, that input has to charge or discharge the capacitor to the new voltage. If that new input is not connected to anything or if the voltage is a high impedance source, then the capacitor will not get the right voltage on it, and so the reading will be the same or only slightly different.

Good point - **except** that he is only reading one input, only selecting one input, and it is supposedly connected to ground which I would generally term a "low impedance source". 🙄

My bet is that his wiring does not actually match that Fritzing diagram **or** that he failed the **critical** step of pre-testing all his Chinese-assembled Dupont wires. 😲

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 15, 2019, 6:18am UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/7 "2019-09-15T06:18:19Z")

</div>

> except that he is only reading one input

Yes but as CrossRoads pointed out in reply #1 he did not declare the multiplexer select pins to be outputs, so he was not actually addressing the one input but any of the other floating inputs of his multiplexer.

When he comes on to actually using the multiplexer this is something he will need to know, or at least take account of.

The inputs of a multiplexer chip should never be left floating whether addressed or not.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [September 15, 2019, 1:53pm UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/8 "2019-09-15T13:53:04Z")

</div>

> Grumpy\_Mike:  
> Yes but as CrossRoads pointed out in reply #1 he did not declare the multiplexer select pins to be outputs, so he was not actually addressing the one input but any of the other floating inputs of his multiplexer.
> 
> When he comes on to actually using the multiplexer this is something he will need to know, or at least take account of.

See #2!

> Grumpy\_Mike:  
> The inputs of a multiplexer chip should never be left floating whether addressed or not.

Why not?

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 15, 2019, 11:24pm UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/9 "2019-09-15T23:24:36Z")

</div>

Because the floating inputs pick up interference and this induces cross talk in the chip and screws up your readings.

That is what I have found actually happens, it is not some theoretical thing.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [September 16, 2019, 1:59am UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/10 "2019-09-16T01:59:06Z")

</div>

That would suggest you do not want to have unconnected "analog" pins on the ATmega chip itself.

---

<div class="post-metadata">

**Author:** ![CrossRoads](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/crossroads/32/214033_2.png) [@CrossRoads](https://forum.arduino.cc/u/CrossRoads)\
**Post date:** [September 16, 2019, 2:28am UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/11 "2019-09-16T02:28:52Z")

</div>

Agreed - turn on the internal pullup resistors at least so the inputs are randomly switching between hig & low & who knows what.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 16, 2019, 5:41am UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/12 "2019-09-16T05:41:23Z")

</div>

> Paul\_\_B:  
> That would suggest you do not want to have unconnected "analog" pins on the ATmega chip itself.

It would, but only if the circuit of the 74HC4067 was the same as the analogue multiplexer inside the ATmega chip. I suspect that as the Arduino's internal multiplexer is part of a configurable pin system, this mitigates against it. But as CrossRoads points out it is easily fixed if it proves a problem.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [September 16, 2019, 12:53pm UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/13 "2019-09-16T12:53:18Z")

</div>

> Grumpy\_Mike:  
> I suspect that as the Arduino's internal multiplexer is part of a configurable pin system,

Another name for which is - a multiplexer!

Semantics! 😲

OK, basically these are nothing more than an array of CMOS switches. A4 and A5 are of course, used in the I2C interface, so the effect of activity on this interface (or any other external input to the analog pins) would for what it is worth, arguably be a far greater risk of interference than a floating pin.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 16, 2019, 3:15pm UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/14 "2019-09-16T15:15:12Z")

</div>

> Another name for which is - a multiplexer!
> 
> Semantics!

Oh come on, you should know better than that.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [September 16, 2019, 10:00pm UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/15 "2019-09-16T22:00:55Z")

</div>

Well, it is clearly correct.

Take a look at Figure 14-5 in the [datasheet](http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf).

The two lowest elements for input functions are a **multiplexer switch** which is used to (selectively) disable inputs in sleep mode (to prevent pass-through currents due to floating or indeterminate inputs) and a **direct** connection to the analog multiplexer detailed in Figure 24-1 and Figure 24-8.

So a "configurable pin system" is in this case clearly just - a multiplexer (and a bit).

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 17, 2019, 12:30am UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/16 "2019-09-17T00:30:47Z")

</div>

Those diagrams in data sheets are not what is built into the silicon. They are a representative equivalent circuit only.

---

<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:** [May 6, 2021, 3:13am UTC](https://forum.arduino.cc/t/analog-multiplexor-reading-random-values-cd74hc4067/610853/17 "2021-05-06T03:13:01Z")

</div>

Closed on May 6, 2021, 3:13am UTC


