Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ Electronics ➜ Microprocessors ➜ 74HC4051 multiplexer / demultiplexer

74HC4051 multiplexer / demultiplexer

Postings by administrators only.

Refresh page


Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Thu 14 Mar 2013 06:33 AM (UTC)

Amended on Thu 30 Jul 2015 07:58 PM (UTC) by Nick Gammon

Message
The 74HC4051 is a useful device which can multiplex (mux) or demultiplex (demux) up to 8 analog signals into a single analog signal.

It has a 16-bit cousin, the 74HC4067, which can be used to mux/demux up to 16 signals.

Pinouts




Multiplexing


Multiplexing is choosing from one of various lines (one at a time) and forwards its contents down a single line.

http://en.wikipedia.org/wiki/Multiplexer

Example:



In this case by setting the A/B/C lines to have 2 (in binary), that is 010, then line 02 (pin 15) is chosen to be the "active" line, and whatever voltage is on it is forwarded to the "common" in/out line (pin 3).

Note: A is the low-order bit. So therefore if you set A and B high, and C low, you would activate channel 3, not channel 6.


Demultiplexing


Demultiplexing is doing the reverse operation. A single input line is forwarded to one of many output lines.

Example:



In this case by setting the A/B/C lines to have 7 (in binary), that is 111, then line 07 (pin 4) is chosen to be the "active" line, and whatever voltage is on the "common" in/out line (pin 3) is forwarded to it.


How does it know which direction to go?


The device is bi-directional. If you have a signal on one end of the pair (the selected line, and the common line) then that signal is echoed on the other end.

Effectively the selected channel has around a 80 ohm resistance (between the selected pin and the common pin) achieved by side-by-side MOSFETs (one P-channel and one N-channel). The unselected channels have a very high resistance between them and the common pin.

So you could use it, for example, to connect up 8 analog inputs, and connect them up to a single analog port on your microprocessor (eg. your Arduino). The Arduino does not generate analog outputs (the so-called analogWrite function actually does digital pulse-width modulation or PWM) so the device isn't quite as useful as an output device.

However you could conceivably connect up LEDs to some of the pins and flash them from time to time, interleaving with taking analog readings.


Suggested wiring


Below is an example schematic for reading multiple LDR (light dependent resistors) using a single analog port. Of course, in this case, it is quite a few Arduino pins (4) to read two sensors, but once you start extending it to 8 sensors you start to save pins. Also in this particular example, if you only had two sensors you would only need to control the "A" pin on the multiplexer, and tie the other two to Gnd (so that they were zero).



Example of the above wiring:



Example code


The code below assumes you have wired up to 8 sensors as described above. It sets the "address select" lines for each of the 8 lines and takes a reading.


// Example of using the 74HC4051 multiplexer/demultiplexer

// Author: Nick Gammon
// Date:   14 March 2013

const byte sensor = A0;  // where the multiplexer in/out port is connected

// the multiplexer address select lines (A/B/C)
const byte addressA = 6; // low-order bit
const byte addressB = 5;
const byte addressC = 4; // high-order bit

void setup ()
  {
  Serial.begin (115200);
  Serial.println ("Starting multiplexer test ...");
  pinMode (addressA, OUTPUT); 
  pinMode (addressB, OUTPUT); 
  pinMode (addressC, OUTPUT); 
  }  // end of setup

int readSensor (const byte which)
  {
  // select correct MUX channel
  digitalWrite (addressA, (which & 1) ? HIGH : LOW);  // low-order bit
  digitalWrite (addressB, (which & 2) ? HIGH : LOW);
  digitalWrite (addressC, (which & 4) ? HIGH : LOW);  // high-order bit
  // now read the sensor
  return analogRead (sensor);
  }  // end of readSensor
  
void loop ()
  {
  // show all 8 sensor readings
  for (byte i = 0; i < 7; i++)
    {
    Serial.print ("Sensor ");
    Serial.print (i);
    Serial.print (" reads: ");
    Serial.println (readSensor (i));
    }
  delay (1000);
  }  // end of loop


What do the Vee and Inh pins do?


The Vee pin is the "negative supply". You could supply a negative voltage (eg. -5V) and then the device could copy an AC signal (from -5V to +5V). If you don't need to do that, and the Arduino does not take negative inputs anyway, just connect Vee to ground.

The Inh pin "inhibits" output (in both directions) if it is high. So to leave the chip active, just connect it to ground.


Screenshot of it in operation


This shows the device "mirroring" an input signal (in yellow - Channel 1) to the output (in blue - Channel 2). There is a slight lag (around 5 nS) between input and output.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


73,336 views.

Postings by administrators only.

Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.