Analog multiplexor reading random values (CD74HC4067)

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

the code of the simple circuit is as following:

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);
}



(updates to for code tags and to show images made by moderator)

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.

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

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

I got the very same output.

Any other tips?

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.

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.

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". :roll_eyes:

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

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.

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?

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.

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

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

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.

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! :astonished:

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.

Another name for which is - a multiplexer!

Semantics!

Oh come on, you should know better than that.

Well, it is clearly correct.

Take a look at Figure 14-5 in the datasheet.

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

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