This is an old revision of the document!
Table of Contents
Multiplexing inputs to a microcontroller
Introduction
Microcontrollers commonly offer a finite number of input pins for analog and digital uses. When creating a project that requires more input pins, multiplexing can be used to expand the amount of available inputs using the same number of pins. Multiplexer devices let the microcontroller switch between inputs to read one at a time through a single input pin. This functionality comes at a cost to the sampling rate of the inputs, but is often necessary for sensor-rich applications. This report discusses connecting multiplexer devices to microcontrollers both in terms of circuit construction and software considerations.
Multiplexer operation and considerations
A multiplexer acts as a switching circuit that selects one of several inputs to send to the single output. Both analog and digital values can be used as inputs for these devices, as the multiplexer acts only as a gate for the input voltages. Inputs are selected using the selection pins on the device, which are controlled using digital binary values that represent the input pin to send through to the output. In the image below, a 4-1 multiplexer takes in 4 inputs, and the 2 selection inputs determine which is passed through. Using the binary encoding, multiplexers with n
selection inputs are able to switch between 2^n
inputs, and one must consider that the multiplexer's selection inputs also take up digital output pins from the microcontroller.
Image source: https://www.circuitbasics.com/what-is-a-multiplexer/
One major disadvantage of multiplexers is their effect on the sampling rate of the inputs. Instead of sampling each input value every frame, inputs to multiplexers must wait their turn to be sampled from the single input pin. For sensors requiring fast sample rates, data may be lost during frames where the input is waiting to be read.
The decision of the multiplexing ratio (inputs per output) must be informed by the requirements of the sensor and the limitations of the microcontroller being used. Multiplexing more inputs per pin may expand the number of values that can be read, but can also degrade the sampling rate to a point where the data isn't as useful.