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.

12 thoughts on “[tutorial] MUX74HC4067

  1. 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

    Reply
    • 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

      Reply
      • 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…

        Reply
        • 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

          Reply
  2. 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?

    Reply
    • Hello,

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

      Reply
  3. 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.

    Reply
    • 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

      Reply
  4. 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

    Reply
    • 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

      Reply
  5. 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.

    Reply

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

required