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.

Multiplexer pinout, from https://www.circuitbasics.com/what-is-a-multiplexer/ 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.

Device interfacing

Multiplexer chips are available from common sources such as Digikey and Mouser for several configurations of inputs and outputs. Some chips contain multiple multiplexer circuits, which can be useful to perform multiple smaller expansions (eg. 4 instances of 2:1) instead of a single larger expansion (eg. 1 instance of 8:1) to preserve a higher sampling rate. Interfacing a multiplexer to a microcontroller involves synchronizing the selection pin changes with the output readings in software. Many libraries abstract this process away from the developer with libraries or capes (Arduino, Bela). If performed manually, developers should consult the datasheet of the multiplexer to determine the read and write delays of the chip for proper synchronization.