Decoders: one line for every code.
A decoder reads an n bit binary code and activates exactly one of its 2ⁿ output lines. Add a gate after it and the same chip builds any logic function of its inputs.
What a decoder does
A decoder is a combinational circuit with n inputs and up to 2ⁿ outputs. For every input code exactly one output is active, and every other output is inactive. If the active level is 1 the outputs are called active HIGH; if it is 0, as on the 74138, they are active LOW.
Three input bits select one of eight lines. This is also called a binary to octal decoder.
A BCD decoder turns one decimal digit (0 to 9) into one of ten lines.
In a computer, a decoder reads the upper address bits and enables the one memory or input output chip they point to.
The 74138 truth table
The 74138 has three select inputs, C (pin 3, the most significant bit), B (pin 2) and A (pin 1, the least significant bit), and eight active LOW outputs Y0 to Y7. With the chip enabled, the output whose number matches the code C B A goes to 0 and the rest stay at 1. Try it below, then read the table.
Select 5: only Y5 is LOW (its lamp is dark).
| C | B | A | Y0 | Y1 | Y2 | Y3 | Y4 | Y5 | Y6 | Y7 |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 0 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
| 0 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 |
The table is a diagonal of zeros: code 000 pulls Y0 LOW, code 101 pulls Y5 LOW, and so on. The pin numbers of every chip used on this page are in the viewer below.
Hold the chip with its notch on the left. Pin 1 is at the bottom left, the pins count up to the right, then continue along the top from right to left. Most 14 pin chips have VCC on pin 14 and GND on pin 7, but not all: check every new chip.
Enable inputs
The 74138 has three enable inputs: G1 (pin 6) is active HIGH, while G2A (pin 4) and G2B (pin 5) are active LOW. The decoder works only when G1 = 1 and G2A = G2B = 0. In every other case all eight outputs are 1, whatever the select inputs say.
| G1 | G2A | G2B | Decoder | Outputs |
|---|---|---|---|---|
| 0 | X | X | disabled | all 1 |
| X | 1 | X | disabled | all 1 |
| X | X | 1 | disabled | all 1 |
| 1 | 0 | 0 | enabled | selected line 0 |
Tie G1 to +5 V and both G2 pins to GND. Never leave them floating: an open TTL input behaves like a 1, and a 1 on G2A or G2B turns the decoder off. The enables have two more uses. Two 74138 chips with opposite enables on a fourth address bit make a 4-to-16 decoder, and a data signal on G2B turns the decoder into a demultiplexer (see the multiplexer lab).
Functions from minterms
A minterm is one row of a truth table. Because output Yn of an enabled 74138 goes LOW for minterm n and for nothing else, the decoder hands you every minterm of three variables on its own pin. To build a function, collect the lines of the rows where the function is 1.
Worked example, the full adder. Put A on C, B on B and Cin on A. The truth table shows which decoder line belongs to each row:
| Row | A | B | Cin | S | Cout | Line |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | Y0 |
| 1 | 0 | 0 | 1 | 1 | 0 | Y1 |
| 2 | 0 | 1 | 0 | 1 | 0 | Y2 |
| 3 | 0 | 1 | 1 | 0 | 1 | Y3 |
| 4 | 1 | 0 | 0 | 1 | 0 | Y4 |
| 5 | 1 | 0 | 1 | 0 | 1 | Y5 |
| 6 | 1 | 1 | 0 | 0 | 1 | Y6 |
| 7 | 1 | 1 | 1 | 1 | 1 | Y7 |
With a 7404 and 7432 this takes one inverter per line and three 2-input OR gates per output.
The NAND shortcut
A NAND output is 1 whenever any of its inputs is 0. Since exactly one decoder line is LOW at a time, the NAND gives 1 precisely when the active line is one of the function's minterms. One 7420 therefore replaces the inverters and all six OR gates of the full adder. A function with three minterms would use a 3-input NAND (7410), and one with two minterms a 2-input NAND (7400).
Worked example: full subtractor
The full subtractor computes A minus B minus Bin. Its difference D is 1 when an odd number of inputs are 1, exactly like the sum of an adder. Its borrow Bo is 1 when A is smaller than B plus Bin.
| Row | A | B | Bin | D | Bo |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 |
| 2 | 0 | 1 | 0 | 1 | 1 |
| 3 | 0 | 1 | 1 | 0 | 1 |
| 4 | 1 | 0 | 0 | 1 | 0 |
| 5 | 1 | 0 | 1 | 0 | 0 |
| 6 | 1 | 1 | 0 | 0 | 0 |
| 7 | 1 | 1 | 1 | 1 | 1 |
This is the design task of the bench. Try to reach these equations yourself from the truth table before you look here.
Common mistakes
- All eight LEDs stay lit for every code: an enable is wrong. Check G1 on +5 V and G2A, G2B on GND.
- The wrong LED goes dark: C and A are swapped. C is the most significant select input.
- The function output is inverted: an AND gate was used where a NAND is needed, or an inverter is missing before the OR gates.
- A chip does nothing: its VCC (pin 16 on the 74138, pin 14 on the gate chips) or GND (pin 8 or pin 7) is not wired.
Questions and answers
Q1The decoder is enabled and C B A = 110. Which output is LOW?
Y6, because 110 in binary is 6.
Q2Every LED stays on whatever the switches do. What is the likely cause?
The decoder is disabled: G1 is LOW, or G2A or G2B is HIGH or left open.
Q3Which 74138 lines feed the NAND gate for Cout of a full adder?
Y3, Y5, Y6 and Y7.
Q4Why is a NAND gate used and not an AND gate?
The lines are active LOW. A NAND of them is 1 whenever one of the chosen lines is LOW, which is the OR of those minterms.
Q5How many 74138 chips make a 4-to-16 decoder?
Two. The fourth bit enables one chip through G1 and the other through G2A, so only one chip is active at a time.