# Multiplexers (MUX)

## What is a multiplexer?

{% embed url="<https://en.wikipedia.org/wiki/Multiplexer>" %}

{% embed url="<https://www.electronicshub.org/multiplexerandmultiplexing/>" %}

<figure><img src="/files/PTGxwBSfRTPrHNLuWS8e" alt=""><figcaption><p>Functional diagram of a quad multiplexer</p></figcaption></figure>

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.&#x20;

#### 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&#x20;

This code was originally created for use with the 18 input MUX circuit that can be found [here](https://a360.co/3x5Ldh1).

```arduino
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);
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wvu-neuromint.gitbook.io/neuromint-resources/technical-guides/circuit-board-components/multiplexers-mux.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
