Introduction: 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
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.
Attachments
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");
}
}
}
---------------------------------------------------------------------
Attachments
Step 3: 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.

Participated in the
Make It Glow Challenge
7 Comments
8 years ago on Introduction
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
10 years ago on Step 2
Thanks for this, I needed some code to try out my new 3-8 line decoders!
Reply 10 years ago on Step 2
Glad I could be of assistance, let me know if you do anything cool with it - I never did much past this tutorial.
Reply 10 years ago on Step 2
Just to multiplex the LED layers for an LED cube I'm making!
10 years ago on Introduction
isnt that called a shift register?
Reply 10 years ago on Introduction
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.)
Reply 10 years ago on Introduction
interesting :)