[tutorial] MUX74HC4067

MUX74HC4067 Tutorial from codebender on Vimeo.

This is a tutorial on the MUX74HC4067 library.

project_zps8b2d4a42

The name of the library refers to the IC that is on the Analog/Digital MUX Breakout from Sparkfun. This IC is a 16-channel analog multiplexer/demultiplexer. You can think of it as an SP16T switch that is digitally controlled. You position the switch by writing to the control pins and then you let the data flow in either direction.

sp16t_zps45e15bff

You can use this module to increase the number of inputs and outputs on your Arduino, since it only needs 6 or 5 pins to provide access to 16 data lines. You could connect to these 16 lines… sensors, buttons, transistors to control relays, or even serial lines from other Arduinos.

mux_zpsb70bdc5d

The SIG pin connects to any of the 16 channels, C0C15. S0 to S3 are the control pins. You write to these pins a binary code, and the corresponding channel is selected. For example, by writing 1010 to S3 S2 S1 S0, the C10 channel gets connected to the SIG pin.

table_zpsc1c7d742

There is also the EN pin that if driven HIGH, it disables the connection of the SIG pin with any of the 16 channels.

The maximum current that can go through a channel is 25 mA. If you need to power things like relays, solenoids, or motors, you will need to use transistors, and pass through the channels only the control signals.

readDigitalSignals


This example assumes there are push buttons with pullup 10 kOhm resistors connected to the 16 channels like in the following figure.

schematic_buttons_zpsb9d3f54f

The code reads the input from all the channels, one after the other, and prints on the serial monitor a statement about whether a push button is pressed or not.

All you have to do is declare a MUX74HC4067 instance, configure as a DIGITAL INPUT the Arduino pin to which the SIG pin connects, and you are ready to read from the channels. Just call read with argument the channel number and read will return either LOW or HIGH.

readAnalogSignals


This example assumes there are potentiometers connected to the 16 channels like in the following figure.

schematic_pots_zps18d61b5c

The code takes measurements of the potentiometers’ values and prints on the serial monitor a statement about that value.

By configuring the signal pin as an ANALOG INPUT and then calling read, you get back the value measured by the A/D converter. It’s an integer from 0 to 1023.

writeDigitalSignals


This example assumes there are 16 LEDs with current limiting 150 Ohm resistors and the positive leads of the LEDs are connected to the 16 channels like in the following figure.

schematic_leds_zps5b08114f

The code just lights up the LEDs, one after the other. You configure the signal pin as a DIGITAL OUTPUT and then you write to a channel by calling the write method and providing as arguments the channel number and a LOW or HIGH value.

writePWMSignals


The circuit for this example is identical to the previous one. The difference here is that the output to the LEDs is a PWM signal.

You can output a PWM signal, by configuring the signal pin as an ANALOG OUTPUT and then calling the write method and providing as arguments the channel number and a value (0 to 255) for the duty cycle of the PWM signal.


That’s all folks. We hope you enjoy this library, and if you have any comments or suggestions you can contact us at girder [at] codebender [dot] cc

Sounds are from freesound.org. Schematics were based on Fritzing.

31 Comments

  1. Ammirmohammad

    05 Dec 2014 08:52:11

    Hi Nick,
    Your article was very nice!. I simulated the multiplexer with 14 logicstates in proteus. I wrote a program in codevision avr that checks the bits that comes from output pin and it shows the bits on LCD 2*16.
    I want to make a line follower with this multiplexer and command to motors when start to spin. I took a screenshot out of my project. as an example if 2 sensors in the middle are 1, then the motors turn on and so on. I can’t sync with multiplexer(4067).
    I hope you can help me.
    http://www.pixentral.com/pics/1SqaagOA9Bg2v4B1X8GLbkKpwlheh2.png
    http://www.pixentral.com/pics/11h0ebSedjxaR8BvVqPagMUWozGT1.png

    • Nick Lamprianidis

      05 Dec 2014 14:49:44

      Hey Amir,

      Sadly you are not giving me much with your problem description (“I can’t sync with multiplexer(4067).”). Would you care to elaborate?

      Regards,
      Nick

      • Ammirmohammad

        05 Dec 2014 17:05:37

        Hi again,
        My problem is that i cant make an if statement in my codes.
        i tried this code by giving it address:
        if(D==1&&C==1&&B==1&&A==1) {
        //do something
        }
        OR (out pin is set to PINA.0 and 15th sensor named sensor15)
        if(sensor15==1) {
        //do something
        }
        OR
        if(PINA.0=sensor15) { //even PINA.0==sensor15 didn’t work
        //do something
        }

        PLZ help me…

        • Nick Lamprianidis

          16 Dec 2014 18:45:45

          Hey Amir,

          Sorry for the delay, I hadn’t noticed your reply.

          From what you’ve mentioned, I understand that you have problems running your program. There could be a few things that are going wrong, I’ll try to give some hints:
          * If you’d like know more about if statements please read here.
          * You can’t have variables with dots in their names, so PINA.0 is not valid.
          * Make sure that before you use any variable, you have defined it.

          And then, what are all those variables? Have you assigned them any values from the multiplexer? Are you using my library, MUX74HC4067? Please provide as much information as possible, so I can help you.

          Regards,
          Nick

  2. Mayank Gulati

    17 Mar 2015 22:08:58

    Hi Nick, you say that the diagrams are based in Fritzing, but I am unable to locate any frtizting file for the mux74hc4067 part… would you be able to share yours?

    • Nick Lamprianidis

      17 Mar 2015 23:01:06

      Hello,

      Only parts of my diagrams are borrowed from Fritzing. In this case, the MUX74HC4067 is an actual photo of the module from Sparkfun.

  3. benjamin

    22 Dec 2015 12:37:17

    Hello, very nice post,
    i got some question for you, on your readDigitalSignals example did you manage cases of multiple impultion (i.e. if you press at the same time on the button 1 and 2)?

    thanks Nick.

    • Nick Lamprianidis

      24 Feb 2016 18:19:26

      Hey Benjamin,

      In order to read the value on a channel, you call the read(<channel>) method (as in the example). So, as long as you call this function fast enough (or your presses are slow enough), you can sample all your buttons at a specific moment. But, there is no real “at the same time” capability here, since you poll only one channel at a time.

      Nick

  4. David

    23 Feb 2016 04:37:29

    Hi Nick,

    How could I use 3 of these boards together to read analog pots with this library? I have the example working well for 1 board, but what about using multiple boards?

    David

    • Nick Lamprianidis

      24 Feb 2016 18:36:28

      Hello David,

      The easy way would be to just connect any additional modules in parallel, the same way you connected the first one. This means you’ll need 6 pins for each module. So, as long as you have enough pins on your uC, you connect as many as you want (although I’m guessing it won’t be practical if you’ll want to do frequent sequential accesses).
      The other way would be to use similar mux/demux modules to pick which MUX74HC4067 module to use each time, and basically share the 6 pins required on the arduino. But that’s not trivial to implement (neither easy for me to describe in a few sentences).

      Nick

  5. Fynx

    20 Mar 2016 23:59:32

    Hi Nick,

    Your Tutorial is very useful. Does your library support connecting 2-3 of this MUX chips? If so, do you also have a diagram that support’s this?

    Thanks a lot.

  6. Sean

    27 Jan 2017 17:55:25

    I copied the code for the digitalwritesignals and it not compiling?

  7. jazzy gerard

    09 Feb 2017 13:47:59

    WOW this looks really nice! does this library takes care off debouncing the buttons in?

    thanks in advance!

  8. ARMAND

    21 Jun 2017 13:23:59

    Is it possible to read and write digital signals on the same arduino code?

  9. Julius Bangert

    19 Jul 2017 15:22:24

    Hi Nick. I’m trying your readDigitalSignals example but can’t seem to get different readings for the different inputs. It just reads back as all of them on or all of them off. Any suggestions? I’m using an Arduino Nano with this code :


    #include "MUX74HC4067.h"
    MUX74HC4067 mux( 2, 3, 4, 5 );

    void setup() {
    mux.signalPin( 10, INPUT, DIGITAL );
    Serial.begin( 9600 );
    }

    void loop(){
    for ( int i = 0; i < 16; ++i ) {
    Serial.print( mux.read( i ) );
    Serial.print( "\t" );
    }

    Serial.println();
    }

    • Julius Bangert

      19 Jul 2017 15:50:12

      Ok. I see I missed the EN pin as the first argument of the MUX74HC4067 initialisation but even if with that, it behaves the same. Any ideas?
      MUX74HC4067 mux( 12, 2, 3, 4, 5 );

      Also, is there a way to skip the EN pin in the initialisation if I’m just letting the breakout board pull it low for continuous use?

  10. Julius Bangert

    19 Jul 2017 15:29:00

    Another question I have :
    I would like to use your library in conjunction with Paul Badger’s Capacitive Sensing Library. Do you know of a way I could initialise a CapacitiveSensor ( which requires two pin values ) using the MUX74HC4067?

  11. Donald Haas

    08 Aug 2017 15:13:51

    I want to turn on a group of leds on for a given delay and then turn them off for another delay.
    These tutorials never show how to do this. They always show how to cycle through the leds with a for statement.

    Thanks Donald

  12. Christian

    22 Oct 2017 20:14:03

    Helo Nick,

    The Tutorial is really good, but how can i use more than one multiplexer with this library? Or is it possible at all?

    Christian

  13. Vincent

    04 Dec 2017 00:20:40

    Hi, I guess you should fix the “readAnalogSignals” circuit schema because both ends of the potentiometers are tied to ground 🙂

  14. Diptangshu Sen

    10 Feb 2018 08:18:03

    Hi Nick,
    I wanted to know how I could go about trying to interface a 16*2 lcd with a demultiplexer. I am short of GPIO pins on my micro-controller

  15. George

    19 Mar 2018 22:51:13

    I have question, is it posible to push 3-4 or more buttons in same time? and if YES are they working correctly ?

    Thanks!

  16. ANG XIAN JIA

    20 Apr 2018 14:01:12

    Hi, do you know how to use this component using MATLAB Simulink?

  17. James

    12 Jun 2018 22:46:46

    Hi Nick,
    I have been using your Library quite extensively for data acquisition and it is a pleasure to use, now i need to write to ports. If i write to port as follows;

    mux.write(10,1);
    delay(10);
    mux.write(11,1);
    delay(10);

    The result is that Port 10 will switch “HIGH” and then after delay switch “LOW” without a “LOW” command and Port 11 will switch “HIGH”and the same will happen. My need is to switch ports on and off at random. I find no example for this operation in any sketches, so i am not sure if the library supports this function.
    Any advise would be most appreciated. Thank you.

  18. William

    02 Sep 2018 09:12:24

    Alo friends. It is possible to connect two MUX74HC4067 on arduino I am doing a test gig and need to read 24 analog inputs. Att William

  19. Markus Schierz

    11 Nov 2018 19:43:59

    I have tried to use your library with arduino ide on an esp32.
    as i only need 16 pushbuttons i edited the source to get rid of the analogwrite issue with the esp32.
    so far so good: the example code works as expected on an arduino. however on the esp32 i can only use c1 to c15 as when i push the button connected to C0 all other inputs are also shown as low (button pressed).
    is there anything i can do (except just connecting C0 via pullup resistor to vcc and not use it for any push button)?

  20. paul

    12 Feb 2019 15:19:19

    Bonjour,
    Peut-on lire les 16 pins en même temps ?
    J’aimerai faire un if pour chacun
    Merci

  21. Anthony

    16 May 2019 23:35:29

    Hello Nick – This is going to be a great help to me. Thank you. If I am running a few multiplexers in parallel, should I still use or can I use a write from the arduino to the “en” pin to turn on the multiplexer in use and similarly turn off the other multiplexers with a write to “en”

    Thank you

    • Nick Lamprianidis

      24 May 2019 08:53:33

      In principle, you can do that. But I would have to look into it to verify that the logic of the library is still sound.

  22. Blaise

    24 May 2019 12:55:14

    Many thanks for the knowledge being shared here, am just getting started with using a multiplexer. Over time I haven’t had a reason to use one but now am going to have to make use of one. I am trying to get multiple OLED display(12C IIC SPI 128 X 64) working and i have been unable to make each of them display independently. My last trial all OLED’s were displaying same thing. I got to understand that they share same address so that explained my issue. Am not so sure how to go by it using a multiplexer but with tutorials and videos i have seen so far they were able to achieve that using a multiplexer. Please any knowledge on how to achieve this would be well appreciated. Thanks in anticipation.

Comments are closed.

Stay tuned:

* we promise that we won´t spam you, never.