In the following example sketch, you can see how we’ve put the binary values for each control possibility in the array byte controlPins – which is then used to set the pins easily in void loop(). For example – to select I/O 15 you need to turn on all the control pins – so you just have to set Arduino PORTD to B11110000 (which is binary 15 LSB first) and much neater than using four digitalWrite() functions. We do this as it reduces the code required and conceptually I feel it’s easier. With both of the examples we’ll use port manipulation to control the digital pins which are connected to the 74HC4067’s control pins. When doing so – take note of the limitations mentioned earlier – take some resistance measurements in your situation to determine what the maximum value will be from your ADC and calibrate code accordingly. In this example we connect the common pin to the board’s analog input pin – so this can be used as a method of reading sixteen analog signals (one at a time) using only one ADC. This is where we select one input pin of sixteen and allow current to flow through to the common pin (1). Pin 15 of the ‘4067 is set to GND, and control pins S0~S3 are connected to Arduino digital output pins D7~D4 respectively. For demonstration purposes we’re using an Arduino Uno-compatible board with the 74HC4067 running from a 5V supply voltage.
Now to show an example of both multiplexing and demultiplexing.