Demultiplexer with Arduino

demultiplexerAfter my (somewhat failed) Charlieplexing experiment, I ordered a 3-8 line decoder (demultiplexer) to attempt an 8 bit LED counter in a much easier way. At some point I am wanting to created an LED matrix board, and this will require at least 32 LEDs.

Unfortunately, the demultiplexer I ordered is an inverting demultiplexer. This wasn’t obvious on the product page, but so be it… Its not a major problem, since I want to drive LEDs with it–I can simply have the cathode side go to the demultiplexer (by default, it only sends low when the input lines indicate which output to select–all others are H–kind of the inverse of what one might think) and anode end to 5vcc from the Arduino (and put a resistor on that end). Its important to also connect the control lines, and I just connected them as per the datasheet: In the case of this part (IC74HC138-2), the control lines (G2A, G2B and G1) must be set to L, L, H. To disable all (and by disable I mean set all outputs to high, since this is an inverting demultiplexer–yes, my eyes went crossed too).

While ultimately there are more lines (because only 8 things are being driven) than with the Charlieplexing eledxperiment, the lines aren’t all needed specifically for the LEDs, and the number of outputsn drive is 2^n rather than n * (n – 1). The additional lines in this example are only for control inputs to the demultiplexer. So, while driving 8 outputs from using a demultiplexer looks just as messy, it is actually much more straightforward to wire. I didn’t have to deal with reversing inputs and outputs and trying to figure out which lines are related. Also, if we were to drive 16, 32 or more outputs, demultiplexing/n-line decoding becomes the only real approaLED Testch.

So in my experiement here, I simply wanted to light up 8 lights in order. In another example I will show how to display two at once (or what appears to be), so that I can display binary values.

For fun, I added a potentiometer to adjust the delay between each step in the series. The code below is not meant to show a graceful or clever way to enable/disable lights. If I wanted to do that, I would do something slick with structs and bitwise ANDs. This code is just meant to show how the demultiplexer works.

(Expand to view the code and the video.)

int a = 12;
int b = 11;
int c = 10;
int e1 = 4;
int e2 = 3;
int e3 = 2;

// the setup routine runs once when you press reset:
void setup() {                
  // inputs
  pinMode(a, OUTPUT);  
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);  

  // controls
  pinMode(e1, OUTPUT);
  pinMode(e2, OUTPUT);
  pinMode(e3, OUTPUT);
  digitalWrite(e1, LOW);
  digitalWrite(e2, LOW);
  digitalWrite(e3, HIGH);
}

void lightBit(short whichBit) {
  if (whichBit == 0) {
    digitalWrite(e1, HIGH);
  }
  else if (whichBit == 1) {
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
  }
  else if (whichBit == 2) {
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
  }
  else if (whichBit == 3) {
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW);
  }
  else if (whichBit == 4) {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW);
  }
  else if (whichBit == 5) {
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, HIGH);
  }
 else if (whichBit == 6) {
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, HIGH);
  }
  else if (whichBit == 7) {
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
  }
  else if (whichBit == 8) {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
  }
}

void loop() {
  delay(analogRead(A0));  
  lightBit(1);
  delay(analogRead(A0)); 
  lightBit(2);
  delay(analogRead(A0)); 
  lightBit(3);
  delay(analogRead(A0)); 
  lightBit(4);
  delay(analogRead(A0)); 
  lightBit(5);
  delay(analogRead(A0)); 
  lightBit(6);
  delay(analogRead(A0)); 
  lightBit(7);
  delay(analogRead(A0)); 
  lightBit(8);
}

And here’s what it looks like (as an added bonus, you can hear my wife doing dishes in the background):

About these ads

4 thoughts on “Demultiplexer with Arduino

  1. How do you power the IC74HC138-2?

    More specifically, do you power it from the Arduino, and if so, which Arduino output pins connect to what pins on the IC74HC138? Or, if you power it separately, do you have one battery/power source for the chip, and one for the arduino?

    Thanks!

Comments are closed.