Introduction: Interfacing a M74HC238 3-8 Line Decoder With an Arduino (Example)

Picture of Interfacing a M74HC238 3-8 Line Decoder With an Arduino (Example)

This is a basic tutorial designed to give you a basic understanding of how to use a M74HC238 3-8 Line Decoder.

As the name implies, you will only need 3 digital pins on your Arduino to control all 8 outputs.

The project will allow you to illuminate (up to 8) individual LEDS by sending commands via serial console.

Step 1: Set Up Your Breadboard

Picture of Set Up Your Breadboard

For the sake of this Instructable, I'm going to assume that you can understand a pinout diagram on a datasheet as well as have a basic knowledge of breadboards.

Now, set up your breadboard however you see fit. See the datasheet for the pin-out diagram. (Attached)

Notes on my setup:
- I could only easily fit 6 LEDs on my breadboard. I only used outputs 0-5, feel free to use all 8.
- G1 is tied to VCC to keep it HIGH at all times. (This prevents erratic behavior.)
- G2A is connected to digital pin 5, this allows us to turn off all outputs.
- G2B is tied to ground to keep it LOW at all times. (This prevents erratic behavior.)
- Output selection pins A, B, and C are connected digital pins 2, 3, and 4 respectively.

Step 2: Program Your Arduino

Upload the provided example sketch (below) to your Arduino. Be sure to note the digital pins used.

--------------------------------------
/*
Interfacing a M74HC238 3-8 Line Decoder With an Arduino (Example)
Zach Cross - Oct 19 2011
*/

const int selA = 2;
const int selB = 3;
const int selC = 4;
const int enable = 5;

int incomingByte;

void setup()
{
// initialize the serial communication:
Serial.begin(9600);

// initialize the control outputs
pinMode(selA, OUTPUT);
pinMode(selB, OUTPUT);
pinMode(selC, OUTPUT);

digitalWrite(selA, LOW);
digitalWrite(selB, LOW);
digitalWrite(selC, LOW);

digitalWrite(enable, HIGH);
}

void loop()
{

if (Serial.available() > 0) {

incomingByte = Serial.read();

if (incomingByte == '0') {
digitalWrite(enable, LOW);
digitalWrite(selC, LOW);
digitalWrite(selB, LOW);
digitalWrite(selA, LOW);
Serial.println("Green LED 0: HIGH");

}else if (incomingByte == '1') {
digitalWrite(enable, LOW);
digitalWrite(selC, LOW);
digitalWrite(selB, LOW);
digitalWrite(selA, HIGH);
Serial.println("Green LED 1: HIGH");

}else if (incomingByte == '2') {
digitalWrite(enable, LOW);
digitalWrite(selC, LOW);
digitalWrite(selB, HIGH);
digitalWrite(selA, LOW);
Serial.println("Yellow LED 2: HIGH");

}else if (incomingByte == '3') {
digitalWrite(enable, LOW);
digitalWrite(selC, LOW);
digitalWrite(selB, HIGH);
digitalWrite(selA, HIGH);
Serial.println("Yellow LED 3: HIGH");

}else if (incomingByte == '4') {
digitalWrite(enable, LOW);
digitalWrite(selC, HIGH);
digitalWrite(selB, LOW);
digitalWrite(selA, LOW);
Serial.println("Red LED 4: HIGH");

}else if (incomingByte == '5') {
digitalWrite(enable, LOW);
digitalWrite(selC, HIGH);
digitalWrite(selB, LOW);
digitalWrite(selA, HIGH);
Serial.println("Red LED 5: HIGH");

}else if (incomingByte == '6') {
digitalWrite(enable, LOW);
digitalWrite(selC, HIGH);
digitalWrite(selB, HIGH);
digitalWrite(selA, LOW);
Serial.println("Y6: HIGH");

}else if (incomingByte == '7') {
digitalWrite(enable, LOW);
digitalWrite(selC, HIGH);
digitalWrite(selB, HIGH);
digitalWrite(selA, HIGH);
Serial.println("Y7: HIGH");

}else if (incomingByte == '8') {
digitalWrite(enable, HIGH);
Serial.println("Disabled, all LEDS LOW");

}

}

}
---------------------------------------------------------------------

Step 3: Experiment With Your Creation!

Picture of Experiment With Your Creation!

Once the sketch is uploaded, open up the Serial Monitor and start sending commands. The code is relatively self explanatory, but here is a quick reference:

Sending 0-7 enables the respective output.
Sending 8 turns off all outputs using the enable pin.
The Arduino will print to the Serial Console each time you send a command.

Be sure to check out the datasheet, the Truth Table on page 2 maps out how bringing each input High/Low translates into output.

Comments

Amun_Ra (author)2013-12-30

thanks for the tutorial. Its great. But you should know that according to your pictures you have connected G2A to ground and G2B to digital pin5 - which is the opposite of what you say in the text. I guess it doesn't matter?

cheers

jacksonliam (author)2012-02-01

Thanks for this, I needed some code to try out my new 3-8 line decoders!

chriscross93 (author)jacksonliam2012-02-01

Glad I could be of assistance, let me know if you do anything cool with it - I never did much past this tutorial.

jacksonliam (author)chriscross932012-02-01

Just to multiplex the LED layers for an LED cube I'm making!

Bongmaster (author)2011-10-19

isnt that called a shift register?

chriscross93 (author)Bongmaster2011-10-19

Not quite, the 3-8 Line decoder simply brings one of it's outputs high based on what inputs are High/Low. Many patterns can be made with just the three control pins, thus eight outputs can be controlled in this case.

Here is a nice example of a shift register in action:
http://bildr.org/2011/02/74hc595/

Make sense? (Shift Registers are much more complicated, in my opinion.)

Bongmaster (author)chriscross932011-10-19

interesting :)

About This Instructable

6,420views

5favorites

License:

More by chriscross93:Interfacing a M74HC238 3-8 Line Decoder With an Arduino (Example)Treeduino - The Web Controlled Christmas Tree
Add instructable to: