Part review – NXP 74HC238 Decoder/demultiplexer

In this article we are going to examine the 74HC238 decoder/demultiplexer IC. Our reason for writing this was to examine another way to get more output pins when using an Arduino or compatible board.

However you can use any combination of three logic lines to turn on or off eight mutually exclusive outputs. How? Let’s find out…

First of all, here is the IC:

74hc238imagesm1

 

The pin layout is very simple, apart from +5V and ground, you have six pins that control the outputs, and eight output pins, however in reality you only need to control three from the microcontroller or other logic lines. Here is the pinout diagram:

pinouts

To get the output pins high, you use a combination of levels on 74HC238 pins A0~A2 and possibly E3. If you leave E3 low, no outputs can be set to high. The input combination required for each output is described in this table from the data sheet:

functiontable1

 

Notice that columns with an X can be set either high or low, but you must not leave them floating, so always connect or set an X to high or low. If you need to have active low outputs (that is, outputs are high instead of low), there is the 74HC138. So now to do this in real life! Here is a demonstration schematic to use the 74HC238 with an Arduino Uno or 100% compatible board:

democircuit1

And here is a demonstration video, using the following sketch:

/*
Demonstrating use of 74HC238 with Arduino

 74HC238 pinouts:
 Y0~Y7 > 39 ohm resistor > LED (3.2V forward voltage) > ground;
 A0~A2 >> Arduino digital 2~4
 E1, E2 >> ground
 E3 > Arduino digital 5
 16 > +5V
 */

int A0 = 2;
int A1 = 3;
int A2 = 4;
int E3 = 5;
int d = 0; // for delay

void setup()
{
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(E3, OUTPUT);
}

void allLow()
// sets all outputs to LOW
{
  digitalWrite(E3, LOW);
}

void turnOn(int outputPin)
// turns on output at pin 'outputPin'
{
  digitalWrite(E3, HIGH); // enables outputs
  switch(outputPin)
  {
  case 0:
    digitalWrite(A0, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    break;
  case 1:
    digitalWrite(A0, HIGH);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    break;
  case 2:
    digitalWrite(A0, LOW);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, LOW);
    break;
  case 3:
    digitalWrite(A0, HIGH);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, LOW);
    break;
  case 4:
    digitalWrite(A0, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, HIGH);
    break;
  case 5:
    digitalWrite(A0, HIGH);
    digitalWrite(A1, LOW);
    digitalWrite(A2, HIGH);
    break;
  case 6:
    digitalWrite(A0, LOW);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, HIGH);
    break;
  case 7:
    digitalWrite(A0, HIGH);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, HIGH);
    break;
  }
}

void emulateKITT(int dd)
{
  for (int i=0; i<8; i++)   {     turnOn(i);     delay(dd);   }   for (int i=7; i>=0; --i)
  {
    turnOn(i);
    delay(dd);
  }
  allLow();
}

void allOn(int dd)
// scans all outputs on to emulate all being on at once for 'dd' cycles
{
  for (int j=0; j<dd; j++)
  {
    for (int i=0; i<8; i++)
    {
      turnOn(i);
    }
  }
  allLow();
}

void loop()
{
  emulateKITT(100);
  delay(1000);
  allOn(10000);
  delay(1000);
}

As with most other ICs of this type, you can only source 25 milliamps of current from each output, so if you need more you will have to consider the use of a switching NPN transistor etc. Although only one output can be high at a time, if you scan them quick enough, you can create the illusion that all are on at once (as in the video).

Apart from LEDs and other items, you could use this IC to control stepper motors or even create a safe working environment on a model train layout.

To conclude, the 74HC238 offers one of several ways to control more things with less control pins. Ideal for mutually exclusive outputs, however if you needed more than one high at once, the 74HC595 shift register would be the better solution.

This post brought to you by pmdway.com – everything for makers and electronics enthusiasts, with free delivery worldwide.

To keep up to date with new posts at tronixstuff.com, please subscribe to the mailing list in the box on the right, or follow us on twitter @tronixstuff.

2 thoughts on “Part review – NXP 74HC238 Decoder/demultiplexer

  1. diegogmx

    dont you know an ic like this wich allows to set the state to a pin and hold it instead of having it changing state as soon as you change the input?

Comments are closed.