74HC4051 Not showing right data

I trying to read values from Potentiometers throught multiplexer to arduino. But i am not get the correct values. When i change pot it display in serial, data on 3 colums not only at one.
Or i have set the channel wrong?

Here is my code.

const int selectPins[] = {7, 8, 9};
const int Z = A0;

int ch0;
int ch1;
int ch2;
int ch3;
int ch4;


void setup() 
{
  Serial.begin(9600); // Initialize the serial port
  // Set up the select pins as outputs:
  for (int i=0; i<3; i++)
  {
    pinMode(selectPins[i], OUTPUT);
    digitalWrite(selectPins[i], HIGH);
  }
  pinMode(Z, INPUT); // Set up Z as an input


}


void loop() {
  
  ch0 = readMux(0);
  ch1 = readMux(1);
  ch2 = readMux(2);
  ch3 = readMux(3);
  ch4 = readMux(4);

  
  Serial.print(ch1);
  Serial.print("\t");
  Serial.print(ch2);
  Serial.print("\t");
  Serial.print(ch3);
  Serial.print("\t");
   Serial.print(ch4);

  Serial.println();
  delay(100);
  
  
}

int readMux(int channel){

    int selectPins[]={7, 8, 9 };

    int muxChannel[8][3]={
      {0,0,0},
      {0,0,1},
      {0,1,0},
      {0,1,1},
      {1,0,0},
      {1,0,1},
      {1,1,0},
      {1,1,1}
      
    };
    for (int i=0; i<3; i++){
      digitalWrite(selectPins[i], muxChannel[channel][i]);
    }
    int val = analogRead(A0);
    return val;
}

Ard2016: But i am not get the correct values. When i change pot it display in serial, data on 3 colums not only at one.

Can you explain what you mean here. You tell it to print 4 columns of data. I would expect you to see 4 columns of data always in the serial monitor. Only one should be changing at a time unless you wired it up funny. You haven't showed us any of that.

Yes that's the point. When i change one pot only one column should display data. But here 3 of them show different data.

Ard2016: When i change one pot only one column should display data.

No that's not true. The say you have the code written it should display some data in four separate columns. Why do you think that this:

Serial.print(ch1);
  Serial.print("\t");
  Serial.print(ch2);
  Serial.print("\t");
  Serial.print(ch3);
  Serial.print("\t");
   Serial.print(ch4);

will ever print only one column? None of those prints are conditional. So all 4 will happen every time through loop no matter which pot you are looking at.

Why don't you stop for a second and apply your brain and think. What can we see and not see here on the forum? We can't see your wiring. We can't see your output. Why don't you post a sample of the output instead of trying (very poorly) to explain it.

English is not my native language and sometime create misconceptions.

I mean when a pot is on 0 position, values in serial should be zero and not random numbers.

Multiplexer in connected to Arduino pro micro on pins
S0->7
S1->8
S2->9
Z->A0
Vcc->5v
E-VEE-Gnd->Gnd.

I have attached a snapshot of serial monitor.

Take out a piece of paper. Draw a picture of how the wiring looks. Show the pots and the multiplexer and the arduino and everything. Post a picture of that to the forum. How hard can it be to get someone to post their damned wiring?

In your picture I see 4 columns of data, not 3 like you say. It looks just like what you said for it to do in the code. I still don't see under what conditions you think your code would only print one column.

I said when 1 pot change only one column should display data other should stay 0 or whatever value they have before.
For example.

pot move to 30° and the serial should display.
0 0 0 750
0 0 0 750
0 0 0 750

Not

485 189 489 750
135 156 168 750
139 987 461 750

I have made a schematic of wiring i use.

 for (int i=0; i<3; i++){
      digitalWrite(selectPins[i], muxChannel[channel][i]);
    }
    int val = analogRead(A0);
    return val;

Since you are reading on the same pin and changing what is connected, you should probably throw away the first read after you switch the multiplexer. You don’t know which line the sample and hold sampled there.

 for (int i=0; i<3; i++){
      digitalWrite(selectPins[i], muxChannel[channel][i]);
    }
    int dummy = analogRead(A0);
    int val = analogRead(A0);
    return val;

If you read any multiplexer inputs that are not connected to a voltage source the analog input will read whatever is in the sample/hold capacitor. This will often be close to whatever voltage was last applied to the analog input.

If you want a multiplexer input to read as 0, connect it to ground either directly or through a resistor.

Hi,
How have you got the ends of your trimpots connected to gnd and supply?

Try adding these connections to you circuit.
ArdProMicroMuxEdit.jpg
Are you using a protoboard?
Are you using the trim pots you have pictured in your schematic?

If yes to both, DOUBLE CHECK that the trimpot “pins”/“wires” are making proper contact with the protoboard contacts.

I design this schematic fast and forgot to connect 5v to left pin of pot and Gnd to the right pin, middle pin for data.

Yes i'am using a protoboard.

Hi, Do you have a DMM to measure the voltages on the wipers of each pot at the 4051 input pin?

Have you tried @Delta_G code suggestion?

Tom... :)

Get your mutlimeter out and measure the actual voltage on the centre pin of the pot. Make sure that matches your expectations, and changes as you move the pot. You may measure this both at the pot, and at the pin of your mux (where the same voltage should appear).

Those values look like the PWR and GND are not connected, leaving the pin floating. Some breadboards have gaps in the power rails, which you have to bridge with wires. One of my breadboards has the power rails divided in three sections... So make sure you actually have +5 and GND at your pots.

TomGeorge: Hi, Do you have a DMM to measure the voltages on the wipers of each pot at the 4051 input pin?

Have you tried @Delta_G code suggestion?

Tom... :)

I measure voltage at each pin and result are: When pot is turn all clock wise multimeter show 4.40V and when i move pot voltage move simultaneously to 0V.

4.4V only? That should be 5V (assuming resistance goes all the way to zero).

How about the pins of the 4051 IC? Same voltage?

wvmarle: 4.4V only? That should be 5V (assuming resistance goes all the way to zero).

How about the pins of the 4051 IC? Same voltage?

Yes 4.4V Which pins? Pins that are connected with Pots? If so these pins have the same value as the pots. If the pot moves the value increase or decrease depend on situation.

I disconnect all pots and measure volts in Input pins and these are all 0.

Ps. With the same code i try different channel on mux and some channel respond to Pot and display data in serial. Maybe is wrong configuration for mux.

Thanks for help.

Sounds like your hardware is working as it should - so indeed configuration/software error is the most likely culprit.

Hi, Can you leave the pots disconnected and just wire input 1 and 3 of the 4051 to 5v and 2 and 4 inputs to 0V. So you should see 1023, 0 , 1023, 0 as your outputs.

[u]Did you try @Delta_G coding suggestion? In post #8?[/u]

Thanks.. Tom.. :)

    for (int i=0; i<3; i++){
      digitalWrite(selectPins[i], muxChannel[channel][i]);
    }

I’m not sure exactly what this does…

I’d write

    for (int i=0; i<3; i++){
      digitalWrite (selectPins[i];

    }
    for (int i=0; i<3; i++){
      digitalWrite (channel[muxChanne][i]);
    }

etc

You have to set up all the chip select pins before you can read the data…

Allan