4051 troubles

I have 6 analog sensors I would like to read data from, using a 4051 multiplexer in order to use only one analog pin. I just can't figure out what's going on, though. I know that basically, you have 3 select pins(ABC) which represent in binary the ports 0-7. So in theory, to read pin 0, you have A B C set to 0 0 0, and to read pin 1, you have A B C set to 0 0 1, etc.

The problem is, no matter what I do, the multiplexer always reads just 1 sensor, and writes it to all of my variables. Can anybody help?
This is set up just for two sensors, one connected to port 0, and the other to port 1 of the 4051. It should take the value of the one sensor and write it to var1, and the other to var2.
Lastly, here is the documentation for the 4051: STMICROELECTRONICS - HCF4051BEY - IC, ANALOG MUX/DMUX, 8 X 1, DIP-16

int var0;
int var1;
void setup() {
Serial.begin(9600);
}
void loop() {
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
delay(50);
var0 = analogRead(A2);
delay(50);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
delay(50);
var1 = analogRead(A2);
Serial.print("var0 = ");
Serial.println(var0);
Serial.print("var1 = ");
Serial.println(var1);
delay(1000);
}

Try reading twice each time, give the internal ADC a chance to settle on the voltage being read:

var0 = analogRead(A2);
var0 = analogRead(A2);

var1 = analogRead(A2);
var1 = analogRead(A2);

Thats why I threw the delay(50) in there, but even with reading twice, no luck...

Here is a video showing exactly the problem

Pin 6 is inhibit, needs to be low to get I/O from the switch (4051) Pin 7 connects to the most negative voltage Vee used (Bi-Polar supplies) and pin 8 is Ground Vss these pins must be pulled down (6) and 7 and 8 are usually grounded Pin 7 Must be Connected. From my experience several years ago... and the data sheet. To remember why mine didn't work the first time...

Doc

Docedison:
Pin 6 is inhibit, needs to be low to get I/O from the switch (4051) Pin 7 connects to the most negative voltage Vee used (Bi-Polar supplies) and pin 8 is Ground Vss these pins must be pulled down (6) and 7 and 8 are usually grounded Pin 7 Must be Connected. From my experience several years ago... and the data sheet. To remember why mine didn't work the first time...

Doc

Thanks for the advice. I assumed that inhibit had to be grounded, as well as ground. I connected Vee to ground too, but still no luck...

Well it's easy enough to test. Select any input, '0' would be best and measure the input voltage and the output voltage, they should be the same. The transmission gate has about 3 - 400 ohms resistance @ 5Vdc Vcc. Pin 13 is the 0 input and pin 3 is the I/O pin (the device is unique in that it is totally bi-lateral... to at least a Mhz) so you might load the pin slightly with an appropriate value resistor and see that the change is equal on both pins, verify that 6 is grounded or at least pulled down 100k is ok. and also one of my mistakes was not to verify the state of the switch address pins as well, I forgot pull-down resistors. When you have an issue like this a DVM can be your best friend. It 'should' work, the chip is reasonably bullet proof and it can source and sink a little current. If you put pull downs on the data address lines they can be pulled from the processor and tested that way easily. IMO

Doc

Docedison:
Well it's easy enough to test. Select any input, '0' would be best and measure the input voltage and the output voltage, they should be the same. The transmission gate has about 3 - 400 ohms resistance @ 5Vdc Vcc. Pin 13 is the 0 input and pin 3 is the I/O pin (the device is unique in that it is totally bi-lateral... to at least a Mhz) so you might load the pin slightly with an appropriate value resistor and see that the change is equal on both pins, verify that 6 is grounded or at least pulled down 100k is ok. and also one of my mistakes was not to verify the state of the switch address pins as well, I forgot pull-down resistors. When you have an issue like this a DVM can be your best friend. It 'should' work, the chip is reasonably bullet proof and it can source and sink a little current. If you put pull downs on the data address lines they can be pulled from the processor and tested that way easily. IMO

Doc

The voltage on the common and ch 0 were the same, both with and without a load. To ensure I had the correct channel selected, I connected the selector pins A, B, and C to ground. Everything was fine there. But to my surprise, as I took the A pin, and moved it from ground to +5v, effectively choosing ch 1, I still read the same voltage through ch 0!

You are not defining pins 11, 12, and 13 as outputs.

Grumpy_Mike:
You are not defining pins 11, 12, and 13 as outputs.

Wow. Can't believe I forgot that... All that trouble, just to find out that it was a stupid mistake like that. Well, all is working good now! Thanks everybody!