a digital scan of a 35mm film image of a processing sketch running on an LCD
Skip to Content

Multiplexing

When the Arduino's inputs and outputs just aren't numerous enough, a multiplexer saves the day.

A multiplexer's basically just a soft switch — you send it a set of digital on / off values to specify which channel you want to read from, and take a look at the "common" pin with an analogRead() on the Arduino, and then rinse and repeat for the next channel (and the next...) until you have all the values you want.

In this way, you can collect readings from a bunch of sensors with the use of just one of the Arduino's analog pins (plus three or four digital control pins. You can think of the multiplexer as a basic parallel to serial converter -- it lets you take a bunch of different signals and put them into a single stream of data.

In short, it's an easy way to add inputs and outputs to your Arduino.

It's also possible to string several together to get control of even more inputs and outputs. More about this, including schematics, is available on the Arduino website.

Multiplexers come in a variety of flavors... 8 channel varieties (like the 4051) use a 3-bit digital channel picker, while 16 channel models like the 4067 shown below use a 4-bit channel picker. Everything you need to know about the pins and channel encoding is available in the datasheet.

The circuit I'm using for the media controller project looks like this. All of the sensors aren't hooked up, but it gives the idea.

Multiplexer circuit overview

Note The resistor between the common pin and ground is critical — I was getting noisy readings at first; the resistor fixed it:

Multiplexer circuit detail

Here's some Arduino code to read each channel from a multiplexer and then send each value out over serial in a comma-delimited format:

  1. // 4067 16 Channel Multiplexer
  2.  
  3. // Based on the code for the 8 channel 4051 available here:
  4. // http://www.arduino.cc/playground/Learning/4051
  5.  
  6. // Here's a handy datasheet for the 4067:
  7. // http://www.sparkfun.com/datasheets/IC/CD74HC4067.pdf
  8.  
  9. // How many of the 16 channels do we want to actually read?
  10. // set this to a lower number to read a fraction of the channels
  11. int activeChannels = 16;
  12.  
  13. // Keep count of which channel we're reading.
  14. int channel = 0;
  15.  
  16. // Set the common input / output pin.
  17. int pinCommon = 0;
  18.  
  19. // Set which pins tell the multiplexer which input to read from.
  20. int pinS0 = 2;
  21. int pinS1 = 3;
  22. int pinS2 = 4;
  23. int pinS3 = 5;
  24.  
  25. void setup() {
  26.   // Set the switch pins to output.
  27.   pinMode(pinS0, OUTPUT);
  28.   pinMode(pinS1, OUTPUT);
  29.   pinMode(pinS2, OUTPUT);
  30.   pinMode(pinS3, OUTPUT);
  31.  
  32.   // Fire up the serial.
  33.   Serial.begin(9600);
  34. }
  35.  
  36. void loop () {
  37.   // Loop through the channels and read each one.
  38.   for (channel = 0; channel < activeChannels; channel++) {
  39.    
  40.     // Get the switch states from the channel number, and
  41.     // send the state to the multiplexer to read from that
  42.     // channel. This configuration covers all the channels,
  43.     // but doesn't read them in a sequence that matches the datasheet.
  44.     // (This works, but needs revision.)
  45.     digitalWrite(pinS0, (channel & 15) >> 3);  // bit 4
  46.     digitalWrite(pinS1, (channel & 7) >> 2);   // bit 3
  47.     digitalWrite(pinS2, (channel & 3) >> 1);   // bit 2
  48.     digitalWrite(pinS3, (channel & 1));        // bit 1
  49.    
  50.     // Read the common pin and send it out over serial.
  51.     Serial.print(analogRead(pinCommon), DEC);
  52.    
  53.     if(channel < (activeChannels - 1)) {
  54.       // Separate each channel with a comma.
  55.       Serial.print(",");    
  56.     }
  57.     else {
  58.       // If it's the last channel, skip the
  59.       // period and send a line feed instead.
  60.       Serial.println();      
  61.     }
  62.   }
  63.  
  64.   // Take a breath.
  65.   delay(10);  
  66. }

October 28 2009 at 1 AM