Multiplexer

I have 16 channel multiplexer from sparkfun:

and i want to read the c12 channel. Should i connect the enable to GND? Thats what i have done...

My code outputs this:
219351 131
222356 350
225361 138
228367 0
231371 0
234375 0
237379 39
240384 0

byte S0 = 16;
byte S1 = 6;
byte S2 = 7;
byte S3 = 8;
byte signal = 0;

                          




void setup() {
    digitalWrite(S0,LOW);
       digitalWrite(S1,LOW);
       digitalWrite(S2,HIGH);
       digitalWrite(S3,HIGH);
  Serial.begin(19200);

}
////////////////////////////////////////////////////////////

void loop()
{ 
Serial.print(millis());
Serial.print("\t");
Serial.println(analogRead(signal));


delay(3000);

}

Broken link to SparkFun above.

Link is fixed

Should i connect the enable to GND?

Yes.

However the code does not define the digital lines connected to the address lines as the multiplexer as outputs. You need to do this before you write to them.

What are you feeding into the multiplexer best bet is to use a 10K pot. Those readings you are getting looks like you have not connected anything up but it is most likely the effect of not driving the address lines correctly.

I added output as you said, and I already have a potmeter connected to C12. I will try the code later

Thanks for the help!!

byte S0 = 16;
byte S1 = 6;
byte S2 = 7;
byte S3 = 8;
byte signal = 0;

                          




void setup() {
    digitalWrite(S0,LOW);
       digitalWrite(S1,LOW);
       digitalWrite(S2,HIGH);
       digitalWrite(S3,HIGH);


pinMode(S0, OUTPUT);      // sets the digital pin as output
pinMode(S1, OUTPUT);      // sets the digital pin as output
pinMode(S2, OUTPUT);      // sets the digital pin as output
pinMode(S3, OUTPUT);      // sets the digital pin as output
  Serial.begin(19200);

}
////////////////////////////////////////////////////////////

void loop()
{
Serial.print(millis());
Serial.print("\t");
Serial.println(analogRead(signal));


delay(3000);

}

No you need to set up the pins as outputs before you write to them.

byte S0 = 16;
byte S1 = 6;
byte S2 = 7;
byte S3 = 8;
byte signal = 0;






void setup() {

pinMode(S0, OUTPUT);      // sets the digital pin as output
pinMode(S1, OUTPUT);      // sets the digital pin as output
pinMode(S2, OUTPUT);      // sets the digital pin as output
pinMode(S3, OUTPUT);      // sets the digital pin as output

    digitalWrite(S0,LOW);
       digitalWrite(S1,LOW);
       digitalWrite(S2,HIGH);
       digitalWrite(S3,HIGH);


  Serial.begin(19200);

}
////////////////////////////////////////////////////////////

void loop()
{
Serial.print(millis());
Serial.print("\t");
Serial.println(analogRead(signal));


delay(3000);

}

Then with this code you have to connect your pot to input 12 (as that is what you select with the writes) to see the changes.

By the way why is S0 mapped through to pin 16, that is one of the analogue pins you would be better off wiring it up to one of the digital pins. I am assuming you have wired the output of the multiplexer to analogue input pin 0.

Ooops it went a bit fast when i wrote the code.....

I would probably have been better of using an atmega 1280 but that wasn't relased when i wired it all up...

All my pins are used except of c14 and c15 on the multiplexer, so that why i had to used analog 1 as digital 16...

Kim

thanks heaps.. such a stupid little mistake....

It works superb now..

Thanks again...

Here is a bit more sophisticated code...

How can i autatically get the length of the array?

byte sensorRead[2] = {12,11};
byte antallSensorer = ???;

byte sensorRead[2] = {12,11};
byte antallSensorer = 2;

byte S0 = 16;
byte S1 = 6;
byte S2 = 7;
byte S3 = 8;
byte signal = 0;

                   



byte logicArray [16][4] = { {0,0,0,0},
                           {1,0,0,0},
                           {0,1,0,0},
                           {1,1,0,0},
                           {0,0,1,0},
                           {1,0,1,0},
                           {0,1,1,0},
                           {1,1,1,0},
                           {0,0,0,1},
                           {1,0,0,1},
                           {0,1,0,1},
                           {1,1,0,1},
                           {0,0,1,1},
                           {1,0,1,1},
                           {0,1,1,1},
                           {1,1,1,1}};
                           
byte pinSelect[4] =  {S0,S1,S2,S3};                           




void setup() {
 pinMode(S0, OUTPUT);      // sets the digital pin as output
pinMode(S1, OUTPUT);      // sets the digital pin as output
pinMode(S2, OUTPUT);      // sets the digital pin as output
pinMode(S3, OUTPUT);      // sets the digital pin as output
  Serial.begin(19200);

}
////////////////////////////////////////////////////////////

void loop()
{ 
Serial.print(millis());
Serial.print("\t");
for(byte i=0;i<antallSensorer ;i++){
       Serial.print(readAnalog(sensorRead[i]));
       Serial.print("\t");
}
Serial.println("");
delay(1000);

}


int readAnalog(byte pinNumber){
  
for(byte i=0;i<4;i++){
       digitalWrite(pinSelect[i],logicArray[pinNumber][i]);
}

return analogRead(signal);
}

How can i autatically get the length of the array?

byte sensorRead[2] = {12,11};
byte antallSensorer = sizeof (sensorRead) / sizeof (sensorRead [0]);

(actually, in this case you can simply get away with

byte antallSensorer = sizeof (sensorRead);

since sizeof (byte) == 1)