I don't think the first mux (note: I am going to use the term "mux" for "multiplexer" - its easier to write) is where he is stuck at - he's trying to connect up the other 4 mux's thru the singular mux.
What you're going to need to do is this; for what I am going to call the "primary" mux, you only need to hook up 2 of the 3 selector pins (because you are only going to hook up to that mux the other 4 mux's - and 2 ^ 2 = 4); so, the primary mux's output goes into the analog Arduino pin; this much I think (?) you understand. You also (?) I think understand that you need to connect up 2 digital pins to that first mux; in order to select one of the four first input lines, ok?
Now, for each of the four remaining mux's (which I will refer to as "secondary" as needed), in order to select one of the pins, you are going to need 3 digital lines on the Arduino, for a total of 12 lines. The Z output of each of these secondary mux's will go into one of the first four inputs of the "primary" mux; the selector pins pin of each secondary mux go to 3 other digital lines on the Arduino (for 12 other digital pins).
So - you have a total of 16 digital pins (2 pins for the primary mux, and 3 pins each for the 4 secondary mux's) involved, plus one analog input pin.
Do you see your problem, yet? You will...
All you have to do in order to select one of the analog lines from the mux's, is write code to:
-
The analog input pin you want is on one of the secondary mux's, so for that mux, set its selector pins to the binary value for the input pin on the mux (decimal 0-7).
-
On the primary mux, set its two selector lines to the value (decimal 0-3) for the secondary mux you are needing to measure.
-
Read the value with analogRead().
-
For another pin/sensor, repeat these steps as needed (in fact, stick your code into a function that decodes your number of 0-31 into the proper mux combo selection, so you can do "int x = myMuxAnalogRead(?);" to get the value, where "?" is an integer byte value of 0-31 decimal.
Here's your problem:
You need 16 digital i/o pins for this control on the Arduino (2 for the primary mux, and 12 for the secondary mux's); on the standard Arduino you have 14 digital i/o pins (0-13), and 6 analog input pins. The six analog pins can be used as digital pins, if pin numbers 14-19 are used with digitalWrite(). You need one analog pin.
So - lets say you -don't- forgo using the rx/tx pins (pins 0 and 1) for USB communication; that leaves 12 pins. Let's say you assign pins (2-3 for the primary mux selector pins. Now you need 12 more pins for the remaining mux's.
Let's say these pins are used:
- secondary mux #1 = pins 4-6
- secondary mux #2 = pins 7-9
- secondary mux #3 = pins 10-12
- secondary mux #4 = pins 13-15
Leaving you with pins 16-19 as analog input pins; one of which you need, leaving you with three remaining analog pins. If you forgo the use of rx/tx, you will get back two more analog input pins (but you won't have any USB serial communications).
You say you need those other analog pins, though - do you have enough given what I have said above? If not, your options are:
-
Use an Arduino Mega - more digital i/o and analog pins; worries over, you don't even have to use the extra primary mux.
-
Attempt to find an analog multiplexer chip (other than the 4051) which has more inputs - if you can find a 1 of 16, you will only need 4 lines per chip (8 total), plus your two for the 4051, leaving plenty of digital and analog inputs left over.
-
The cheap, but more complex solution: Use a set of latching shift registers to control the 4051s - see here:
If it were me, I would look into option 2 - while it is bound to be more expensive for such a chip, it won't be something that breaks the bank, provided you can find one (check Mouser or Digikey, or maybe someone here knows of something off the top of their head).
[edit]
Check out this - the mpc506A:
Not exactly cheap, but it will do the job - something I also noticed, and which I don't know if the 4051 has (?) - on the inputs of the mpc506a, there are current limiting resistors built into the chip; you will probably have to take these into account in your readings of your sensors.
[/edit]
Option 3 should only be considered if you don't mind the extra complexity; you being a newbie, more complexity isn't what you want, though, which is why option 2 is best. Option 3 might be something to shoot for when you have more experience and/or less change to spare for larger mux's.
I hope this helps (and I hope I haven't made any mistakes; I probably did - so please help me if I did, Arduino fans!)...
