Multiplexers (MUX)

Clarus Goldsmith, June 2024

What is a multiplexer?

Essentially acts like a switchboard. Only inputs with their respective control signal (marked with # in left figure) set to high (i.e., 1) will transmit through the MUX. For ex, input signal 1 will only transmit to output 2 if 13 is set to 1.

Multiplexing vs. Demultiplexing

Multiplexing refers to taking a single input and sending it to multiple possible outputs

Demultiplexing refers to taking in multiple inputs and switching which is sent to a common output

Hardware considerations for a MUX

Using a MUX with a microcontroller

A common use for multiplexers is to functionally expand the number of ports available on a microcontroller. By wiring all of the output pins together and attaching them to a microcontroller pin, you can send multiple input signals into the pin by cycling which command pin is high. By keeping track of this cycling, you can then record these multiple signals distinctly. In this way, a single port can functionally play the role of 2-4 ports. This is particularly invaluable for analog signals, as microcontrollers typically have only 10-18 analog ports. For cases involving, for example, 36 analog strain signals from a hexapod robot, demultiplexing is the only way to record all signals simultaneously.

Example demultiplexing code

This code was originally created for use with the 18 input MUX circuit that can be found here.

int numDigitalSetIndices = 12;
int analogMUXdigitalSet[12] = {10,12,13,11,12,10,11,13,10,12,13,11}; 
int analogMUXdigitalPins[4] = {10,11,12,13};
int numDigitalPins = 4;
int analogMUXanalogIn[12] = {0,0,1,1,2,2,3,3,4,4,5,5};
int numAnalogPins = 6;
int analogMUXanalogPins[6] = {0,1,2,3,4,5};
int analogVal;
int value;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while(!Serial);

for(int i=0;i<numDigitalPins;i++) // Set all of the digital pins to output mode and LOW before starting
  {
    pinMode(analogMUXdigitalPins[i], OUTPUT);
    digitalWrite(analogMUXdigitalPins[i],LOW);
  }
for(int i=0;i<numAnalogPins;i++) // Set all analog pins to input mode
  {
    pinMode(analogMUXanalogPins[i], INPUT);
  }
}


void loop() {
  // put your main code here, to run repeatedly:
    for(int i=0;i<numDigitalSetIndices;i++)
    {
     digitalWrite(analogMUXdigitalSet[i],HIGH);
     analogVal = analogRead(analogMUXanalogIn[i]);
     Serial.print(analogVal);
     Serial.print(" ");
     digitalWrite(analogMUXdigitalSet[i],LOW);
    }
    delay(5);
  }
}

Last updated