Avatar
Arduino and CD74HC4067 MUX Multiplexor
Aug 28th, 2022 | by: ELECTRONOOBS | Views 1168
Views 1168

Multiplexers and demultiplexers are devices that allow us to control a greater number of devices with a smaller number of inputs or outputs. We can use these devices to expand the number of inputs and outputs of a processor like Ardunio.

Tutorial Video

A multiplexer (or mux) allows multiple signals to be directed to a single input. The reverse device is called a demultiplexer (or demux) and allows a single signal to be directed to multiple outputs.

The CD74HC4067 is a bidirectional 16 channel Multiplexer/Demultiplexer. We can think of it as a digitally controlled switch, connecting the common pin to one of the available channels.

This allows to write or read from 16 devices with only 5 pins. 4 of them control the device we want to read. The remaining pin collects the signal read or written on the selected channel.

The CD74HC4067 works with digital and analog signals both as input and output. For this reason, these types of devices are called input and output expanders (I/O expanders).

The CD74HC4067 can also be used in communication devices such as serial port, I2C bus or SPI bus. For example, we can connect the TX pin of the UART to send information to multiple devices. However, for a complete communication we will need more than one CD74HC4067, one for each pin involved in the communication.

The maximum current it can provide is 20mA, limited, but that's not a big deal either since it's similar to the maximum current an Arduino pin can provide. For higher currents, an amplification stage using a transistor or relay output will be necessary.

The supply voltage of the CD74HC4067 is between 2V to 6V. In any case, the device signal voltages cannot exceed the Vcc voltage.

Price

CD74HC4067 multiplexers are cheap devices. We can find them for €0.80 in international eBay or AliExpress sellers.

MUX Module



ASSEMBLY DIAGRAM

The connection is simple. On the one hand, we power the CD74HC4067 by connecting Vcc and Gnd respectively to 5V and Gnd on Arduino. We also connect the EN chip to 5V, to activate the CD74HC4067.

On the other hand, we connect the pins S0, S1, S2 and S3, which control the active channel, to any 4 digital outputs of the Arduino. Finally, we connect the signal pin to an Arduino input or output. In the schematic we will use pin A0, but it could be any other analog or digital pin.

 

MUX Diagram

The connection, seen from Arduino, would be the following, where again we have used pin A0 but we could use any other pin.

MUX Arduino schematic
CODE MUX as output

In the first example we show the use of the CD74HC4067 to add outputs. The following code progressively turns all 16 channels on and off. To do this we change the address pins, and use an Arduino digital output to turn the signal pin on and off.


const int muxSIG = 9;
const int muxS0 = 3;
const int muxS1 = 4;
const int muxS2 = 5;
const int muxS3 = 6;
 
int SetMuxChannel(byte channel)
{
   digitalWrite(muxS0, bitRead(channel, 0));
   digitalWrite(muxS1, bitRead(channel, 1));
   digitalWrite(muxS2, bitRead(channel, 2));
   digitalWrite(muxS3, bitRead(channel, 3));
}
 
void setup()
{
   pinMode(muxSIG, OUTPUT);
   pinMode(muxS0, OUTPUT);
   pinMode(muxS1, OUTPUT);
   pinMode(muxS2, OUTPUT);
   pinMode(muxS3, OUTPUT);
   Serial.begin(9600);
   delay(1000);
}
 
void loop()
{
   for (byte i = 0; i < 16; i++)
   {
      Serial.println(i);
      SetMuxChannel(i);
      digitalWrite(muxSIG, HIGH);
      delay(10);
      digitalWrite(muxSIG, LOW);
      delay(10);
   }
}





MUX as INPUT

const int muxSIG = A0;
const int muxS0 = 3;
const int muxS1 = 4;
const int muxS2 = 5;
const int muxS3 = 6;

int SetMuxChannel(byte channel)
{
   digitalWrite(muxS0, bitRead(channel, 0));
   digitalWrite(muxS1, bitRead(channel, 1));
   digitalWrite(muxS2, bitRead(channel, 2));
   digitalWrite(muxS3, bitRead(channel, 3));
}
 
 
void setup()
{
   pinMode(muxSIG, INPUT);
   pinMode(muxS0, OUTPUT);
   pinMode(muxS1, OUTPUT);
   pinMode(muxS2, OUTPUT);
   pinMode(muxS3, OUTPUT);
   Serial.begin(9600);
   delay(1000);
}
 
void loop()
  SetMuxChannel(7);               //Select MUX channel
  int val = analogRead(muxSIG);   //Make analog read
  Serial.println(val);            //Print it on the monitor
  delay(100);                     //Small delay
}


0 Comments

Login or Sign Up to post comments on this tutorial.