Tutorial – 74HC4067 16-Channel Analog Multiplexer Demultiplexer

Introduction

Now and again there’s a need to expand the I/O capabilities of your chosen micorocontroller, and instead of upgrading you can often use external parts to help solve the problem. One example of this is the 74HC4067 16-channel analog multiplexer demultiplexer. That’s a mouthful – however in simple form it’s an IC that can direct a flow of current in either direction from one pin  to any one of sixteen pins. Another way to think abou it is that you can consider the 74HC4067 to be a digital replacement to those rotary switches that allow you to select one of sixteen positions.

Here’s an example of the SMD version:

74HC4067

Don’t let that put you off, it’s just what we had in stock at the time. The part itself is available in through-hole and surface mount versions.

Using the 74HC4067

At this point you should download the data sheet, as we refer to it through the course of the article. The first thing to note is that the 74HC4067 can operate on voltages between 2 and 6V DC, which allows use with 3.3V and 5V microcontrollers and boards such as Arduino and Raspberry Pi. If for some reason you have the 74HCT4067 it can only work on 4.5~5.5V DC.  Next – consider the pinout diagram from the data sheet:

74HC4067 pinoutThe power supply for the part is applied to pin 24, and GND to … pin 12. Pin 15 is used to turn the control the current flow through the inputs/outputs – if this is connected to Vcc the IC stops flow, and when connected to GND it allows flow. You can always control this with a digital output pin if required, or just tie it to GND if this doesn’t matter.

Next – pin one. This is where the current either flows in to be sent to one of the sixteen outputs – or where the current flows out from one of the sixteen inputs. The sixteen inputs/outputs are labelled I0~I15. Finally there are the four control pins – labelled S0~S3. By setting these HIGH or LOW (Vcc or GND) you can control which I/O pins the current flow is directed through. So how does that work? Once again – reach for the the data sheet and review the following table:

74HC4067 truth tableNot only does it show what happens when pin 15 is set to HIGH (i.e. nothing) it shows what combination of HIGH and LOW for the control pins are required to select which I/O pin the current will flow through. If you scroll down a bit hopefully you noticed that the combination of S0~S3 is in fact the binary equivalent of the pin number – with the least significant bit first. For example, to select pin 9 (9 in binary is 1001) you set the IC pins S0 and S3 to HIGH, and S1 and S2 to LOW. How you control those control pins is of course up to you – either with some digital logic circuit for your application or as mentioned earlier with a microcontroller.

Limitations 

Apart from the power supply requirements, there are a few limitations to keep in mind. Open you data sheet and consider the “DC Electrical Specifications” table. The first two parameters show what the minimum voltage that can be considered as a HIGH and the maximum for a LOW depending on your supply voltage. The next item of interest is the “ON” resistance – that is the resistance in Ohms (Ω) between one of the sixteen inputs/outputs and the common pin. When a channel is active, and a 5V supply voltage, we measured a resistance of 56Ω without a load through that channel – and the data sheet shows other values depending on the current load and supply voltage. Finally, don’t try and run more than 25 mA of current through a pin.

Examples

Now to show an example of both multiplexing and demultiplexing. For demonstration purposes we’re using an Arduino Uno-compatible board with the 74HC4067 running from a 5V supply voltage. Pin 15 of the ‘4067 is set to GND, and control pins S0~S3 are connected to Arduino digital output pins D7~D4 respectively.

Multiplexing

This is where we select one input pin of sixteen and allow current to flow through to the common pin (1). 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. 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.

With both of the examples we’ll use port manipulation to control the digital pins which are connected to the 74HC4067’s control pins. We do this as it reduces the code required and conceptually I feel it’s easier. 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.

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().

This simply sets each input pin in turn, then reads the ADC value into an array – whose values are then sent to the serial monitor:

… and a quick video of the results:

Demultiplexing

Now for the opposite function – sending current from the common pin to one of sixteen outputs. A fast example of this is by controlling one of sixteen LEDs each connected to an output pin, and with 5V on the 74HC4067 common pin. We don’t need current-limiting resistors for the LEDs due to the internal resistance in the 74HC4067. Here’s the sketch:

… and the LEDs in action:

Conclusion

If you’re considering the 74HC4067 or hadn’t known about it previously, we hope you found this of interest. If you have any questions please leave them below or privately via the contact page. And if you made it this far – check out my new book “Arduino Workshop” from No Starch Press.

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

 

The following two tabs change content below.

John Boxall

Founder, owner and managing editor of tronixstuff.com.

24 Responses to “Tutorial – 74HC4067 16-Channel Analog Multiplexer Demultiplexer”

  1. Bill Cahill says:

    Would an IC such as this be able to switch audio signals? If not this, is there an IC you would recommend that would perform that function well?

    Thanks!

    • John Boxall says:

      Not this one – the internal resistance will affect the amplitude. Nothing springs to mind right now but if you’re only wanting A-B switching perhaps DPDT relays would do.

  2. gunndo says:

    Try in Analog or Maxim, “video crosspoint switch” or “audio crosspoint…”.

    They have nice ICs for switching audio and video.

  3. kyr says:

    This CAN be used to switch audio.
    You just have to respect the min max voltages and other characteristics of the device. It’s not for high power or hi-fi but it works.
    Most CRT TVs use variants of the same device family (fewer channels: 4051/4052/4053) to switch audio channels to scart/speakers, etc

  4. Eimi says:

    Hi, John.
    I’m trying to test Demux with an ARDUINO UNO and 74HC4067N but I can’t.
    Please, would you mind to check if the wiring I do is correct?

    VCC Arduino to Pin 24 and
    GDN Arduino to Pin 12
    D7 Arduino to Pin 10 (S0)
    D6 Arduino to Pin 11 (S1)
    D5 Arduino to Pin 14 (S2)
    D4 Arduino to Pin 13 (S3)
    (-)leg of each LED to Pin 15 (E)
    (+)leg of each LED to output pins (I0 – I15)

    Load the sketch but nothing happend.
    Please, what is wrong?
    I’m copying the wiring schema from the video but I don’t know if is right.
    Thanks in advance.

    • John Boxall says:

      Don’t connect the LED cathodes (‘-‘) pin to the 74HC4067, they should go to GND instead. Also connect 5V for the LEDs to pin 1. Connect pin 15 to GND.

  5. Eimi says:

    Hi again, John.

    Just a pair of doubts.

    – How must I do to turn off just one of the outputs?
    (i.e. with SetPin(5), turn ON pin 5 but how to do to turn it OFF?)

    – Do you know if is possible to manage a 4-relay shield with this demux? (Arduino Uno -> demux -> 2 relay shields -> 8 solenoids)
    If so, would you mind to tell me how must I do?

    Thanks in advance.
    Regards.

    • John Boxall says:

      Only one output is selected at a time, you can turn it on/off using enable (pin 15). This wouldn’t be the part to control your relay shield. Try the MCP23017 or even a shift register.

  6. Eimi says:

    OK, already understood
    Will try to do with the MCP23017.

    Thanks again.

  7. Porto says:

    Hello,

    I’m in a project in which two different circuits make use of one and the same 1602a dot matrix display; it’s 8 bit connected so a total of 12 wires are to be switched (10 when ground not included). Do you know of any sort switching IC that can perform this task? (some kind of 12 pins on/off/on switch)

    circuit 12 output _> IC 12input -> IC 12 output -> display

    • John Boxall says:

      Oh that would be a bit of a mess. Assuming both circuits had a microcontroller of sorts, couldn’t the both send data to a third which controls the LCD?

  8. Porto says:

    I can try that, which sort of controller would you advise?

    I can run it now the most simple way, just wire the 12 wires 1 on 1 parallel to eachother, but this causes some strange side effects: one circuit works 100%, but when feeding voltage to the other (and cut off the first circuit) I have to disconnect at least 2 wires from the first circuit: VDD (supply voltage), RS (Register Select) and maybe also VO (contrast). RS is most important because it writes garbage to the display when I leave this connected. Both circuits seems to work fine this so probably now I only need to switch 3 wires. I guess I can solve this with a mechanical switch.

  9. noob says:

    Hi great tutorial!

    I would like to hook up 6 of these to all of the uno’s analog inputs. Would the arduino struggle with that many (96) adc conversions?

    • John Boxall says:

      No, as the Uno can only do one at a time.

      • Noob2 says:

        I want to make a test with the demux but I need to connect more than 5 74HC4067 (cascade mode) is this possible? for example using the output of the demux as signal to get 256 outputs is this possible?

      • John Boxall says:

        Possibly, could be messy. If you need that many consider using I2C expanders with higher pin counts e.g. MCP23017

  10. Edgar says:

    Hi.
    I want to make a test with more than 3 74HC4067 it this possible? I know I can connect only one but it is possible to connect in cascade? and it is possible how can I connect and the cycle is easy to do it?

  11. TimB says:

    Hi. First of all, thank you. Your tutorials are brilliant. I’m a noob to arduino but not to computers. I’ve got a project in mind to build a multi-battery capacity tester. There are plenty of designs for single cell testers which track current flow and log it; I’m wondering if this could be used to measure 16 cells at once, cycling through measurements. You see I’ve got about 200 old laptop batteries I am trying to reuse and need to figure out their capacities. Do you think this multiplexer would work as a way to cycle through voltage measurements? Again, thanks for such a great site.

  12. StephaneAG says:

    Hi !

    Your tutorial in neat !
    -> now I just have t omake my test-implms ;)

    Nb: I stumbled upon one thing that makes me tick, though: in the “multiplexing” tutorial ( 1st example ), shouldn’t the ‘setup()’ function be declared as the following ? ( as we ‘re reading from the ‘4067, one ‘d expect to have stuff set as INPUTS , not as OUTPUTS ? )

    void setup()
    {
    Serial.begin(9600);
    DDRD = B00000000; // set PORTD (digital 7~0) to inputs / right ?
    }

    Anyway, ‘ll digg another tuts’ of your ;p
    Thanks for sharing
    +

  13. litoy2x says:

    Hi John, I need advice about multiplexing the gpio. I notice that there are many ways you can multiply the gpio: shift register, mux/demux and i2c.

    Questions:
    1. what is the main difference between them?
    2. advantage and disadvantage?
    3. when do i need to use them, the right approach I mean?
    4. examples would be great how to use each?

    Thanks :)

Trackbacks/Pingbacks


Leave a Reply

Receive notifications of new posts by email.

The Arduino Book

Arduino Workshop

Für unsere deutschen Freunde

Dla naszych polskich przyjaciół ...

Australian Electronics!

Buy and support Silicon Chip - Australia's only Electronics Magazine.

Use of our content…

%d bloggers like this: