A multiplexer (MUX) is a digital switch that routes one of several input signals to a single output line based on the binary state of its select pins. By trading a few control pins for the ability to read many inputs, a MUX fundamentally changes a circuit's architecture: it drastically reduces microcontroller GPIO requirements, minimizes PCB trace congestion in dense layouts, and simplifies wire harness routing in physical installations.
The Core Mechanism: Select Lines and Data Routing
At the silicon level, a multiplexer is constructed from an array of AND gates feeding into a single OR gate (or NAND/NOR equivalents). The device relies on a strict mathematical relationship between its data inputs and its select (address) lines. For a MUX with 2n inputs, you will always need exactly n select lines to address every channel.
Think of a MUX like the rotary dial on a bench multimeter. The dial (select lines) doesn't generate the measurement; it simply connects the single display (output) to whichever specific internal shunt or voltage divider (input channel) you need to read at that exact moment. Only one path is active at a time, and the rest are electrically isolated from the output.
Worked Example: Reading 8 Sensors with a 74HC151
Let us look at a concrete implementation using the Texas Instruments SN74HC151, a standard 8-line to 1-line digital multiplexer. Suppose you have 8 digital limit switches on a CNC machine, but your microcontroller only has one spare interrupt pin available.
The SN74HC151 features eight data inputs (D0 through D7), three select inputs (A, B, C), one active-low enable pin (G), and two complementary outputs (Y and W). To route a specific sensor's HIGH/LOW state to the Y output, you must drive the select pins with the binary equivalent of that sensor's channel number.
You need to read the state of the limit switch connected to input D5.
1. Convert the channel number (5) to binary: 101.
2. Map the binary bits to the select pins (C, B, A): C=1 (HIGH), B=0 (LOW), A=1 (HIGH).
3. Ensure the Enable pin (G) is pulled LOW to activate the IC.
4. The microcontroller reads the Y pin. If the D5 switch is closed (HIGH), Y outputs HIGH.
From a timing perspective, the SN74HC151 is exceptionally fast. At a standard $V_{CC}$ of 5.0V, the typical propagation delay ($t_{pd}$) from the select pins to the Y output is just 14 nanoseconds. This means you can cycle through all 8 sensors in roughly 112ns, allowing your microcontroller to poll the entire array tens of thousands of times per second without missing a transient switch bounce.
| Select C | Select B | Select A | Active Input | Output Y |
|---|---|---|---|---|
| 0 | 0 | 0 | D0 | D0 State |
| 0 | 1 | 1 | D3 | D3 State |
| 1 | 0 | 1 | D5 | D5 State |
| 1 | 1 | 1 | D7 | D7 State |
Where You Meet Multiplexers in Practice
You will encounter multiplexing logic across three distinct domains in modern electronics, each requiring different IC families and design considerations.
1. Digital GPIO Expansion
When building control panels or LED matrices, microcontrollers like the ATmega328P or ESP32 quickly run out of pins. A bank of 74HC157 (quad 2-channel) or 74HC151 (8-channel) ICs allows a single 8-bit port on a microcontroller to address dozens of buttons or switches by strobing the select lines and reading the shared data bus.
2. Analog Signal Routing
Not all MUXes handle strictly digital 1s and 0s. The CD4051B is a CMOS analog multiplexer. It passes continuous voltage levels, making it ideal for routing audio signals or reading multiple analog thermistors into a single ADC pin. However, analog MUXes introduce an on-resistance ($R_{ON}$). For the CD4051B at 5V, $R_{ON}$ is roughly 120Ω. If your source has a high output impedance, this 120Ω will form a voltage divider and introduce measurement errors.
3. I2C Bus Multiplexing
The I2C protocol is notorious for address collisions—if you need four identical BME280 environmental sensors, they all share the same hardcoded I2C address. The TCA9548A is an I2C multiplexer that solves this. It sits on the main I2C bus and creates up to 8 isolated sub-buses. The microcontroller sends a command to the TCA9548A to open a specific channel, effectively giving you 8 separate I2C buses to place your sensors on. Watch your bus capacitance, though: each TCA9548A channel adds roughly 10pF to 15pF of capacitance, which can degrade I2C rise times if you exceed the 400pF spec limit.
Common Confusions: MUX vs. DEMUX vs. Encoders
Because they all deal with binary addressing and multiple pins, beginners frequently confuse multiplexers with similar logic ICs.
- MUX vs. Demultiplexer (DEMUX): A MUX is many-to-one (selects one of many inputs to send to a single output). A DEMUX is one-to-many (takes a single input and routes it to one of many outputs). A 74HC138 is a classic DEMUX (often used as a 3-to-8 line decoder).
- MUX vs. Encoder: A MUX passes the actual electrical state (HIGH or LOW) of the selected input pin to the output. An encoder (like a priority encoder) ignores the electrical state and instead outputs a binary number representing which pin is currently active. Encoders generally assume only one input is active at a time; a MUX does not care what is happening on the unselected pins.
- Analog vs. Digital MUX: A digital MUX (74HC series) uses standard CMOS logic gates and will square off slow-rising analog signals into sharp digital edges. An analog MUX (4000 series or dedicated analog switches like the TS5A3159) uses transmission gates (parallel NMOS and PMOS transistors) to pass raw voltages bidirectionally without distorting the waveform.
Frequently Asked Questions
How to use a multiplexer to expand ESP32 GPIO pins?
To expand digital inputs on an ESP32, wire the select pins (A, B, C) of a 74HC151 to three ESP32 GPIOs, and wire the Y output to a fourth GPIO. In your Arduino/ESP-IDF code, loop a counter from 0 to 7. Use bitwise operations to extract the bits and write them to the select pins using digitalWrite() or direct port manipulation for speed. Read the Y pin, store the result in an array indexed by the counter, and increment. Because the ESP32 operates at 3.3V, ensure you power the 74HC151 with 3.3V (the HC series is rated for 2V to 6V operation) to avoid frying the ESP32's GPIO with 5V logic levels.
What is the difference between a digital multiplexer and an analog multiplexer?
A digital multiplexer (e.g., 74HC151) processes discrete logic levels. It has a high-impedance input and actively drives the output to a hard rail (VCC or GND) based on the selected input's state. It cannot pass intermediate voltages. An analog multiplexer (e.g., CD4051 or 74HC4051) acts as a solid-state relay. It creates a low-resistance physical path between the input and output pins, allowing continuous, bidirectional voltage waveforms (like audio or sensor millivolt signals) to pass through, limited only by the switch's on-resistance ($R_{ON}$) and bandwidth.
Can I cascade multiple multiplexers to read 32 sensors?
Yes, you can cascade them using a tree architecture. To read 32 digital sensors, use four 8-channel MUXes (like 74HC151s) for the first stage. Wire the Y outputs of those four ICs into the D0 through D3 inputs of a fifth 74HC151. You will use 3 select lines to address the first stage (shared across all four ICs) and 2 additional select lines to address the second stage. This requires 5 total GPIO pins to address 32 inputs. The trade-off is propagation delay: the signal must pass through two silicon stages, effectively doubling your $t_{pd}$ latency, though at 14ns per stage, the total 28ns delay is still negligible for almost all hobbyist and industrial polling applications.






