cascade 16 74HC4067 multeplexers coding.

hi guys hope you are doing well,
i need to cascade a 256:1 milteplexer using the 16:1 for analog readings.

for the hardware its not a big deal, but for the code i need some help in the mapping.

will somthing similar to this work for me? (this is a 64:1)
thanks in advance.

int M_S0 = 2;
int M_S1 = 3;
int M_S2 = 4;

int S_S0 = 7;
int S_S1 = 6;
int S_S2 = 5;
int analogPin = A0;
int Z =0;
int MUX64[64] = {0};

void setup() {
      // CONFIGURE ADDRESS PINS
    pinMode(M_S0, OUTPUT);
    pinMode(M_S1, OUTPUT);
    pinMode(M_S2, OUTPUT);
    pinMode(S_S0, OUTPUT);
    pinMode(S_S1, OUTPUT);
    pinMode(S_S2, OUTPUT);
    pinMode(Z, OUTPUT);
  Serial.begin(9600);   
 
}

void loop() {
 
   // LOOP THROUGH ALL THE ADDRESSES OF THE MASTER
    for (int i = 0; i < 64; i++) {
       
    digitalWrite(M_S2, HIGH && (i & 0b1000000));
    digitalWrite(M_S1, HIGH && (i & 0b100000));
    digitalWrite(M_S0, HIGH && (i & 0b010000));       
    digitalWrite(S_S2, HIGH && (i & 0b000100));
    digitalWrite(S_S1, HIGH && (i & 0b000010));
    digitalWrite(S_S0, HIGH && (i & 0b000001));
    delay(1);
   
    Z = analogRead(analogPin);   
   
   
     

    Serial.print("Input ");
    Serial.print(i);
    Serial.print(": ");
    Serial.print(Z);
    Serial.println(" v ");
    delay(100);
}

}

The code depends on your exact circuit. If you wire 16 multiplexers in parallel, you need 16 chip select pins - Arduino Mega? Otherwise add a 1:16 decoder for chip select by only 4 bits (pins).

cherifdekari: for the hardware its not a big deal,

I would not be too sure about that. You are going to have quite a bit of parasitic capacitance there with some risk of crosstalk.

Possible "XY problem" - what is your actual application?

To me, it looks like your code is basically correct. I have no way to test it. I think it's (or - it could be) 8 selector pins - 4 for the one root mux and 4 for all 16 of the leaf muxes (address pins wired in parallel). So if you can find a way to locate the pins on the same port you could just write 'i' to the port, instead of all of that code.

This seems odd

 digitalWrite(M_S2, HIGH && (i & 0b1000000));

to me it is just

 digitalWrite(M_S2, i & 0b1000000);

because of the way HIGH and LOW are defined. digitalWrite() actually does

if (val == LOW) {

to determine how to set the port. This does guarantee that anything that is "not LOW" will set the port output. That would be my flimsy justification for the simplication above. This implicit guarantee is the result of the fact that a zero is bit-agnostic, in the sense that when it is used as a boolean 'false' and contains multiple bits like a byte does, there is no representation of zero that considers any of the bits differently (if one is 0, then they all are).

Note also that this addressing can be sparsely populated, i.e. you could use say, one root mux and 4 leaf muxes. If you make the right pin mapping (binary integer) you can achieve contiguous direct addressing of inputs from 0 to 63. In that case the leaf muxes use 4 bits and the root mux uses 2. In such cases you have other choices of root mux such as CD4051B.

16 CMOS inputs in parallel have a very large total input capacitance. It's worth looking at the driving circuit to ensure that it can drive such a heavy capacitive load.

@aarg, it is odd ;)

@cherifdekari The evaluation of your logical AND is from left to right; HIGH will always evaluate to true on an Arduino so after that it will evaluate i & 0b1000000 which will return a number.

The better way

digitalWrite(M_S2, (i & 0b1000000) == 0b1000000);

(i & 0b1000000) == 0b1000000 will give you true or false and in Arduino land HIGH and true are both 1 and LOW and false are both 0 so it will work.

The purists probably want something like

digitalWrite(M_S2, (i & 0b1000000) == 0b1000000 ? HIGH : LOW);

This uses a ternary operator (obfuscated if/else) . If the condition evaluates to true, it returns HIGH as the second argument for the digitalWrite, if it evaluates to false it will return LOW as the second argument for the digitalWrite).

oky u all agree that HIGH && (i & 0b1000000) is odd; ill change it. one more question Mr @sterretje to access to sensor 256 i need one more bit right? can we use 0b10000000 ? (4bits for each of the mux) how can i do this?

    pinMode(M_S0, OUTPUT);
    pinMode(M_S1, OUTPUT);
    pinMode(M_S2, OUTPUT);
    pinMode(M_S3, OUTPUT);
        
    pinMode(S_S0, OUTPUT);
    pinMode(S_S1, OUTPUT);
    pinMode(S_S2, OUTPUT);
    pinMode(S_S3, OUTPUT);

0bXXXXXX 0b1000000 0b0100000 0b0010000 0b0001000 0b0000100 0b0000010 0b0000001 what is the address of M_S3? PS: im working with ESP32.

@DrDiettrich im using esp32 not arduino mega the hardawre is similar to this: http://www.cesarebrizio.it/Arduino/mux_daisy_chain.jpg im just using 16 from 16:1 mux and i collect there outputs using another 16:1 mux.

@Paul__B the input capacitance of the 4067 is about 3.5pf will this cause a pb?

Each channel has a resistance and capacity, forming a low pass filter of degree 2 with chained multiplexers. Find out how much time is required for stable values after a channel switch.

Hi,
Can you tell us what your project is that requires the control of 256 outputs?

Thanks… Tom… :slight_smile:

Hey, I asked that! :cold_sweat:

TomGeorge: Hi, Can you tell us what your project is that requires the control of 256 outputs?

Thanks.. Tom.... :)

Sorry 256 ANALOG INPUTS!!!

How fast do you want to read these inputs? Have you written code and tested it for one 4067 multiplexer?

Are you aware that the AtoD conversion is not instant and it takes processing and input settling time to get any accurate readings?

Paul__B: Hey, I asked that! :cold_sweat:

Great minds think alike. Tom.... :) :)

@TomGeorge @Paul__B thank you for your reply. for the project its a home automation app using esp32 i may not use all the 256 but i want such a large number. for testing the 4067, no not yet i just tested the 4052 ( 4 chanels) it was okey. "AtoD conversion is not instant" i need one reading each lets say 10 secondes its okey. can you answer my question #5. how to map the control signals i.é 0bXXXXXXX for 8 control signals. thank you

Home automation sensors have to sit in many locations, and it's very hard and expensive to route analog signals through the building to the µC. That's why intelligent sensors with digital outputs are typically easier to use at less overall cost.

Mr @DrDiettrich i just wanna know how to code the selector pins. 0bxxxxxxx. thats all

Have an array of output pin numbers and digitalWrite() the bits of your index value to these outputs. See arrays, for loops and bitRead().

@cherifdekari

Other post/duplicate DELETED Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum. It will help you get the best out of the forum in the future. Other general help and troubleshooting advice can be found here.