4051 cascade

Hi everyone,
I am quite struggling to make some 4051 working together in order to get more than 8 analog inputs to be read for my Arduino... I will need 24 analog inputs to be read by the arduino but I don't know how to tell the arduino to do so :frowning:
I did one 4051, but when I need to cascade them it all becomes quite hard for a beginner like me.

Moreover I would also like to have a feedback (e.g. a Serial.println) in order to know exactely which input has passed the threshold, because I'll need LEDs to fadeIn and out according to it.

So far I tried to came out with something like this with only one 4051

int s0 = 10;
int s1 = 9;
int s2 = 8;

int rowTTL = 0;    

int  binTTL [] = {
  0, 1, 2, 3, 4, 5, 6, 7};//bin = bin?r, some times it is so easy


int inPin = 4;
int result;

void setup(){
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  Serial.begin(9600);
}


void loop(){

  for(int i=0; i<8; i++){

    rowTTL = binTTL[i];
    sendABCTTL(rowTTL);
    result = analogRead(inPin);

    if(result < 1020) { // no contact
      Serial.println(0);
    }
    else{
      Serial.println(1); // contact!
    }

    delay(500);

  }

}

void sendABCTTL(int rowTTL){
  switch(rowTTL) {
  case 0: 
    digitalWrite(s0, LOW);
    digitalWrite(s1, LOW);
    digitalWrite(s2, LOW);
    break; 
  case 1: 
    digitalWrite(s0, HIGH);
    digitalWrite(s1, LOW);
    digitalWrite(s2, LOW);
    break; 
  case 2: 
    digitalWrite(s0, LOW);
    digitalWrite(s1, HIGH);
    digitalWrite(s2, LOW);
    break; 
  case 3:
    digitalWrite(s0, HIGH);
    digitalWrite(s1, HIGH);
    digitalWrite(s2, LOW);
    break; 
  case 4: 
    digitalWrite(s0, LOW);
    digitalWrite(s1, LOW);
    digitalWrite(s2, HIGH);
    break; 
  case 5: 
    digitalWrite(s0, HIGH);
    digitalWrite(s1, LOW);
    digitalWrite(s2, HIGH);
    break; 
  case 6:
    digitalWrite(s0, LOW);
    digitalWrite(s1, HIGH);
    digitalWrite(s2, HIGH); 
    break; 
  case 7: 
    digitalWrite(s0, HIGH);
    digitalWrite(s1, HIGH);
    digitalWrite(s2, HIGH);
    break;
  }
}

I guess I'll need as many "for (...){}" as 4051 to make it work, but I don't know how exactely. Moreover... Some doubts with the circuits:
in this link www.arduino.cc/playground/Learning/4051 the mux are in cascade in a way (left img) but in this other link I found they are set in a different way 4051.jpg (image)

Anyone has an idea on how to help me? I am doing my thesis project and this part is slowing me down quite a lot. Help would be extremely appreciated. Thanks to everyone!

With 24 IPs you will need 3 4051s with the control pins (A0, A1 and A2) connected together, all the Z "outputs" connected together, and you have to decode the E(nable) pins (pin 6 on the 4051) so only one 4051 is enabled at a time.

If you have 3 spare digital outputs you can do the decoding in software, ie pull pin_x low to use 4051 #1, pin_y low to use 4051 #2 and pin_y low to use 4051 #3.

then do something like

void sendABCTTL(int rowTTL){
// lower 3 bits in rowTTL select IP on mux
// next 2 bits in rowTTL select mux
// m0,1,2 have been defined as three pins to enable the muxes

  switch (rowTTL >> 3) {
     case 0:
        digitalWrite(m0, LOW); // this enables mux 1
        digitalWrite(m1, HIGH);
        digitalWrite(m2, HIGH);
        break;
     case 1:
        digitalWrite(m0, HIGH);
        digitalWrite(m1, LOW); // this enables mux 2
        digitalWrite(m2, HIGH);
        break;
     case 2:
        digitalWrite(m0, HIGH);
        digitalWrite(m1, HIGH);
        digitalWrite(m2, LOW); // this enables mux 3
        break;
}

  switch(rowTTL [glow]& 7[/glow]) { // slight change to your existing code
  case 0:

Note that these large switch statements are really ugly, but without direct port accessing that's probably as good a method as any.

I also posted to your ? about using the 4051s as OPs.


Rob

Some 4051 ideas:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1227132571/22

This project uses two chips and two analogue inputs, but it is simple to add another chip and use another input.
http://www.thebox.myzen.co.uk/Hardware/MIDI_Footsteps.html

Thank you so much.
I finally got my inputs working!
Now I'd like them to control the outputs,but for that I'll have to wait for the components to arrive.

@Greynomad: thank you for posting both here and on the other topic.
I think I'll ask you some questions as soon as the components arrive.

Cheers!