multiplexing question

hey folks. i've been messing around with the arduino for awhile, but for some reason, i just can't get my 4051 multiplexer working.

i followed the tutorial on the playground, and i've got the digital outputs sending the right values, etc., and i believe i've got it hooked up correctly, but i'm having trouble actually reading the values. i'm pretty sure it's got to be in my coding, but i don't know where.

i have two pots connected right now, on pins y0 and y1 of the 4051. as the arduino cycles through it's loop, i have an if statement that should select for a row value of 0 (all digital pins output 0, which should select pin y0 on the 4051); if row = 0, then it should take a reading from the analog input. I'm sure there are better/more efficient ways of doing this, but I'm just trying to get something that works! it takes a reading, but instead of reading from just the first pot connected to pin y0 on the 4051, it will take readings from either one and add them together.

if both pots are at 0%, it reads 0; if i turn one to 100%, it will read around 500 (out of a possible 1024); subsequently turning the other pot to 100% will then give a reading of 1024. But it works either way, depending on which one you turn first.

What am I doing wrong??? Any ideas? I know it's got to be something stupid in the code, but I can't figure it out.

here is my code:

int pot1 = 0;
int led = 13; //just a led
int r0 = 0; //value select pin at the 4051 (s0)
int r1 = 0; //value select pin at the 4051 (s1)
int r2 = 0; //value select pin at the 4051 (s2)
int row = 0; // storeing the bin code
int count = 0; // just a count
int bin [] = {000, 1, 10, 11, 100, 101, 110, 111};//bin = binär, some times it is so easy
void setup(){
pinMode(2, OUTPUT); // s0
pinMode(3, OUTPUT); // s1
pinMode(4, OUTPUT); // s2
digitalWrite(led, HIGH);
beginSerial(9600);
}
void loop () {
for (count=0; count<=7; count++) {
row = bin[count];
r0 = row & 0x01;
r1 = (row>>1) & 0x01;
r2 = (row>>2) & 0x01;
digitalWrite(2, r0);
digitalWrite(3, r1);
digitalWrite(4, r2);
if (row == 0) {
pot1 = analogRead(0);
Serial.print("row= "); Serial.print(row);Serial.print(" ");
Serial.print("pot1 ="); Serial.print(pot1);
Serial.println("");
delay (1000);
}
}
}

Thanks!!

How did you connect the two pots ?

Center pin should go to Arduino analog pin
one of the outer pins should go to +5V
the other to ground.

Try to use only one pot hooked up as above and see what happens

right, that's how i have them hooked up. i will try hooking just one up and see what happens.

when i unplug one of the pots, the remaining pot will read 0-1024. it doesn't matter which input on the multiplexer i connect it to, it still shows up on the serial monitor-- it's supposed to be selecting for input y0. i don't get it!

The fact that you now with only one pot have the right values is a good sign, but also an indication that something was not right when you had two pots hooked up.

It's really strange that the pot value shows up no matter what pin you connect it to.......

try to replace this line

if (row == 0)

with this

if (count == 0)

to rule out the possibillity that some of the bit shifting has a side effect on the row variable.

Still no luck! This is so weird. I've had nothing but success with the arduino so far, I don't get it!

Maybe I've connected something wrong? I've tried switching out all different kinds of connections. Even tested it out with multiple 4051's, as I ordered several of them.

per the pinout on the playground article, i connected pin 16 to the Arduino's +5V output, and pin 8 to ground. Pins 11,10,9 connected to 3 of the Arduino's digital outs, pin 3 connected to one of the analog ins. Double checked the pinout with the specific Texas Instruments 4051 that I'm using, and it's the same. Also tried connecting pins 6 and 7 (E and Vee) to ground.

Found that the 4051 seems to function the same regardless of whether the digital outs are connected-- it automatically lets the pot data pass through, no matter what input it's connected to.

Any help??

The only advice i have right now is to try to ground all unused inputs on the 4051. some CMOS IC's have a problem with floating input pins.

I made a dual 4051 multiplexer with 16 slide pot's recently and had no problems.

maybe you can post a photo of your set up ??

(by the way pin 6 and 7 one the 4051 need to always connected to ground)

ha det bra
tomek

Here's a picture of the PCB i made for the 16 pot multiplexer, before the IC's were put in the sockets, pin 1 on each 4051 is in the upper right corner of the sockets.

http://www.mikmo.dk/misc/mux-pcb.jpg

It's hard to see what is connected to what in the picture so:

4051 (1)

pins 6, 7, 8 to GND
pin 16 to 5V
pin 3 to Arduino analog input pin 0
pin 11 to Arduino digital pin 2
pin 10 to Arduino digital pin 3
pin 9 to Arduino digital pin 4
pot 1- 8 wiper to pins 13, 14, 15, 12, 1, 5, 2, 4

4051 (2)

pins 6, 7, 8 to GND
pin 16 to 5V
pin 3 to Arduino analog input pin 1

pin 11 to Arduino digital pin 2
pin 10 to Arduino digital pin 3
pin 9 to Arduino digital pin 4
pot 9 - 16 wiper to pins 13, 14, 15, 12, 1, 5, 2, 4

The code i use is based on the 4051 sample in the playground, i just changed variable names because i found those in the sample code sligthly confusing, and i changed it to work with two 4051 IC's

I have been so busy lately that I haven't had a chance to give it another go.

Thanks for all the input! you guys are so helpful

I think you need to change your bin[] declaration line:

int bin [] = {B000, B001, B010, B011, B100, B101, B110, B111};

... perhaps the Arduino is reading outside your array as you have specified decimal values that are greater than 7 (last channel #).