Overview

Today, in this tutorial, we are going to interface 74HC4051 Multiplexer/Demultiplexer with Arduino. I will cover the multiplexing part first.

Components Required

  1. Arduino NANO or Uno (any version)
  2. CD74HC4051
  3. Potentiometer
  4. Joystick
  5. Breadboard
  6. Jumper Wires

Bill of Materials

S.N.COMPONENTS NAMEDESCRIPTIONQUANTITY
1Arduino  UnoArduino  Uno1https://www.evelta.com/launchxl-f28379d-c2000-delfino-mcus-f28379d-launchpad-development-kit/
 CD74HC4051Robot Smart Car Chassis  1https://robu.in/product/cd74hc4067-high-speed-%e2%80%8b%e2%80%8bcmos-16-channel-analogdigital-multiplexer-breakout-module/
2PotentiometerPotentiometer1https://robu.in/product/10k-ohm-3pin-15mm-shaft-potentiometer-pack-of-3/?gclid=Cj0KCQiAybaRBhDtARIsAIEG3kkcIAC_sbps7zacoKQfFIczMpNr_HiXBils3LaFo7zwDEDTw8WsiEAaAgbfEALw_wcB
 3JoystickJoystick1https://robu.in/product/joystick-module-ps2-breakout-sensor/?gclid=Cj0KCQiAybaRBhDtARIsAIEG3klWTzWaFvYRte11UHKb8JwxhQTvbMQNLTJlvnSNHX5tvjA8EXaWL98aAjYPEALw_wcB
5Connecting WiresJumper Wires20https://www.amazon.in/dp/B074JB6SX8/ref=twister_B074J9NMB9?_encoding=UTF8&psc=1
6BreadboardBreadboard1http://amazon.in/Electronicspices-Point-Solderless-Breadboard-Prototype/dp/B08YX57FJG/ref=asc_df_B08YX57FJG/?tag=googleshopdes-21&linkCode=df0&hvadid=396988870302&hvpos=&hvnetw=g&hvrand=5555375138870789824&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=9302486&hvtargid=pla-1364897184161&psc=1&ext_vrnc=hi

Circuit Diagram

Have you ever had enough contacts to read a set of analog sensors? Don’t worry, you are not alone, it happens to the best of us, and you can do something about it. An analog / digital multiplexer, such as the CD74HC4067 (reduction multiplexer), can help increase the number of contacts you have, and it’s insanely easy to connect to your Arduino or another microcontroller

CD74HC4067 is a 16-channel analog multiplexer / demultiplexer. It is available in a DIP package ready for the layout board, or if you are as dependent on the board as I am, robu.in offers an SSOP version. This allows you to use 4 digital contacts to control the transfer of one contact to 16 others. It can actually be used in any direction, as well as with serial or other digital interfaces. In this tutorial, we are just going to read the values ​​of 16 pots, because buying 16 analog sensors for this would be superfluous.

A multiplexer of this kind actually simply acts as a 16 to 1 switch. 4 digital contacts are used to set HIGH or LOW to binary (0-15) to determine which contact the “SIG” is connected to. Thus, transferring all 4 LOW contacts switches CD74HC4067 to channel 0 (thus SIG and C0 will be connected), transferring all HIGH switches to 15 (so SIG and C15 will be connected). It’s a simple binary code, but by chance, you’re one of the 99.8% of people in the world who don’t know binary, here’s a simple table on the right.

Features

  1. “On” resistance: 70 Ohms @ 4.5V
  2. 6ns break-before-make @ 4.5V

Pinout

How does it work

The working of a mux is pretty simple, and it remains the same for other multiplexers too.

Enable contact (E) is the active low contact.
This means that the device will switch on every time the contact level is low.
Now, as shown above, the combination of selected contacts must be omitted to connect the corresponding input contact (C) to the output (Seg).
To show the work, I am going to connect 3 ADC sensors to the input contacts (C0 to C2) and I read the sensor value from the output contact (Seg).

As you can see, the 3 selection pins are connected to 8, 9, 10 and 11, and the power output is connected to GND. I also renamed them accordingly.

Top layer

Bottom layer

Top and bottom layer

Final code



//connect s1 s2 s3 pin to mc arduino

//Mux control pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;

// connect adc reading pin 
//Mux in "SIG" pin
int SIG_pin = 0;  // arduino adc reading pin


void setup() {
  // put your setup code here, to run once:

// select all s0 s1 s2 s3 pins as a output

pinMode(s0, OUTPUT); 
  pinMode(s1, OUTPUT); 
  pinMode(s2, OUTPUT); 
  pinMode(s3, OUTPUT); 

// make all gpios are low at initial stage
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  // serial port
  
  Serial.begin(9600);
  

}

void loop() {

//Loop through and read all 3 values
  //Reports back Value at channel 3 is: 346
  for(int i = 0; i < 4; i ++){
 Serial.print("Value at channel ");
    Serial.print(i);
    Serial.print("is : ");
    Serial.println(readMux(i));
    delay(1000);
    
  }
}




// write small function to read mux



int readMux(int channel)
{
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[3][4]={
    {0,0,0,0}, //channel 0
    {1,0,0,0}, //channel 1
    {0,1,0,0}, //channel 2
   
  };

  //loop through the 4 sig
  for(int i = 0; i < 4; i ++){
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  return val;
}

By Devilal

Leave a Reply

Your email address will not be published. Required fields are marked *