Confused with my MUX Shield

Hi everyone

a while ago I got a MUX shield (v1) from Sparkfun (Mux Shield | Mayhew Labs), which I am just starting to play to play with. To familiarize myself with it, what I'm trying to do is make an LED blink. The LED is connected to pin 0 in the M0 section of the board. I'm using the simple code below.

However... when I upload the code, I realize that the LED does NOT blink. Instead, it will only blink if I connect the + terminal to pin #3 in the M0 section (instead of pin 0)!!

Anyone knows what could be going on? Thanks!

#include <MuxShield.h>
MuxShield muxShield;

void setup()
{
  muxShield.setMode(1,DIGITAL_OUT);        //set I/O 1 as digital output
  muxShield.setMode(2,DIGITAL_OUT);        //set I/O 2 as digital output
  muxShield.setMode(3,ANALOG_IN);          //set I/O 3 as analog input
}

void loop()
{
  //Make an LED blink (should be connected to pin 0, in M0)
  muxShield.digitalWriteMS(1,0,HIGH); 
  delay(1000);
  muxShield.digitalWriteMS(1,0,LOW); 
  delay(500);
}

:frowning: Anyone has any ideas on how I can make this MUX work?

Have you tried using their example code? It doesn't use the arduino library, so bypasses one possible problem.

Thanks for the suggestion... After I saw your posted I found the example code you mentioned and I actually advanced... slightly. However, it is still very confusing because the code uses a loop and a binary approach for selecting each pin to power up.

Below is a subset of the example code that selects one of the 3 multiplexer chips, and uses a for loop to cycle through the pins (using binary) to blink a series of LEDs .

My question would be... how can I do to specify which pin to turn on? For instance, how can I specify to light up pin #7, or pin #10? I understand that to power up pin #0, following the bitwise AND operations, 0 & 1 ==> 0; and to select pin #1, you do 1 & 1 ==> 1. But I am confused on how to specify all the other pins :frowning:

Any help with this would be suuuuper appreciated!! Thanks!!

//Mux_Shield_DigitalOut_Example
//http://mayhewlabs.com/arduino-mux-shield

/*
This example shows how to output high or low on all 48 pins.  To use the analog pins as digital, we use
pin numbers 14-16 (instead of analog numbers 0-2). 

To simplify this code further, one might use nested for loops or function calls.
*/

//Give convenient names to the control pins
#define CONTROL0 5    //MUX control pin 0 (S3 is connected to Arduino pin 2)
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

void setup()
{
  //Set MUX control pins to output
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
}
  

void loop()
{

  //Since all 3 multiplexers have the same control pins, the one multiplexer data line we want to 
  //talk to should be set to output and the other two multiplexer lines should be be 'bypassed' by 
  //setting the pins to input
    
  //Turn on output to digital pin 14 (MUX 0) and turn off the other 2 multiplexer data pins
  pinMode(14, OUTPUT);
  pinMode(15, INPUT);
  pinMode(16, INPUT);
    
  //This for loop is used to scroll through the FIRST multiplexer
  for (int i=0; i<16; i++)
  {    
    //The following 4 commands set the correct logic for the control pins to select the desired input
    //See the Arduino Bitwise AND Reference: http://www.arduino.cc/en/Reference/BitwiseAnd
    //See the Aruino Bitshift Reference: http://www.arduino.cc/en/Reference/Bitshift
    digitalWrite(CONTROL0, (i&15)>>3); //S3
    digitalWrite(CONTROL1, (i&7)>>2);  //S2
    digitalWrite(CONTROL2, (i&3)>>1);  //S1
    digitalWrite(CONTROL3, (i&1));     //S0
    
    digitalWrite(14, HIGH);
    delay(100);
    digitalWrite(14, LOW);
    delay(100);
  }
}

I don't quite understand what you're saying, but I'll try my best to answer it.

This statement:

I understand that to power up pin #0, following the bitwise AND operations, 0 & 1 ==> 0; and to select pin #1, you do 1 & 1 ==> 1

is not strictly correct and is probably causing your confusion. A bitwise AND operation is being used to mask the values. Basically, the 4 control lines are allocated as one line per bit, giving you 4 bits. So for example, the number 9 decimal is 1001 in binary. Therefore, control lines 0 and 3 need to be '1', and lines 1 and 2 need to be '0' to select pin 9. That is what the masking/shifting lines of code are doing below. Instead of the decimal numbers 15,7,3,1, think of it as binary numbers 1111, 0111, 0011, 0001. Even better, use binary 1000, 0100, 0010, 0001 as you're actually trying to get just the one bit in each case.

The way you select an output pin is to use the control lines.

    int i=10; // this is a number from 0 - 15 and is the pin you want selected.

    digitalWrite(CONTROL0, (i&8)>>3); //S3
    digitalWrite(CONTROL1, (i&4)>>2);  //S2
    digitalWrite(CONTROL2, (i&2)>>1);  //S1
    digitalWrite(CONTROL3, (i&1));     //S0

    digitalWrite(14, HIGH);
    delay(100);
    digitalWrite(14, LOW);
    delay(100);

Hey arduinodlb

thank you SO MUCH!!! Your example was so helpful!! I now understand how this works... My confusion was in the fact that I erroneously believed that the actual "pin number" was somehow constructed within the for loop. Duhhhh!!!

Again... thank you sooo much for the incredibly enlightening responses :slight_smile:

You're welcome. Glad you're up and running.