Last Update: 24/10/2013 at 08:21 -- NEXT UPDATE WILL BE AT >2-02-2014

Multiplexers

I will teach you how to use a multiplexer using arduino. It is really simple.
So what is a multiplexer?
Nowadays we use a multiplexer to select data from a input and redirected to the output. Lets see the following image:


The inputs S1 and S0 are the select pins. The S1 is the most significant which means that if we write the word "00" on select pins, it will select the X0 input and redirect tha X0 data to Y.
If we select "10" the data from X2 will be redirected to Y.

So if you want to increase your arduino ports you can use a Analog or Digital multiplexer. For example. Imagine that this is a analog multiplexer and X3...X0 is connected to 4 line sensors. To read each sensor you will have to use the digital pins in arduino to select the data input.

Multiplexers can be single, dual, quad and more. What is thit that a multiplexer can be dual or quad and more? Well lets see:

This is a quad multiplexer. because if your select S0 = 0, it redirects xD0 data To xQ and if S0 = 1 you will redirect data to xQ.

So we can conclude that a quad multiplexer redirects one of the 2 words depending of S0 value.
I bought some CD4052BE IC. It is a Analog dual multiplexer. It is very good if you have sensors that you need to read and need some space to put more sensors. I just have 2 obstacle sensors, and i'm low with line sensors. so i will use just 2 obstacle sensors. In the next image the yellow wires are the analog outputs.

Here is the arduino code that i used.
int r0 = 0;      //value of select pin at the 4051 (s0)
int r1 = 0;      //value of select pin at the 4051 (s1)
int count = 0;   //which y pin we are selecting

void setup(){
  Serial.begin(9600);
  pinMode(2, OUTPUT);    // s0
  pinMode(3, OUTPUT);    // s1
}

void loop () {
  for (count=0; count<=3; count++) {
    // select the bit
    r0 = bitRead(count,0);     
    r1 = bitRead(count,1);   

    // select the multiplexer position
    digitalWrite(2, r0);
    digitalWrite(3, r1);
    
    // print sensors info
    Serial.print(count); // print the position of the multiplexer that is selected
    Serial.print(" : ");
    Serial.print(analogRead(A0)); // print the sensor values
    Serial.print(" | ");
    
    if(count == 3) // when select word "11" change line
      Serial.println();   
  }  
}


Note: I call word to the combination of bits. Example: Number 3 is "11" in base 2