Lesson 4. A decoder in a package: 74HC138 and a running light

In pictures

After this lesson the library has the 74HC138 — a 3-to-8 decoder — and the canvas has 16 lamps and a running light: a counter chooses the number, two 74HC138 put out the right lamp.


4.1. The idea: number → one wire

Three wires A0 A1 A2 are a number 0–7. The decoder chooses the one output of eight with this number. That is how a processor chooses a memory chip or a device by its address: the top address bits go to a decoder, its outputs go to the "select" inputs of the chips.

A2 = 1A1 = 0A0 = 174HC138101 = 5Y0 = 1Y1 = 1Y2 = 1Y3 = 1Y4 = 1Y5 = 0← chosenY6 = 1Y7 = 1
A decoder: number 5 (101) on the inputs — a zero only on Y5, the other seven outputs are 1. The number chooses a wire: that is how a processor chooses which memory chip to talk to.

4.2. The idea: active 0

The chosen 74HC138 output is 0, the others are 1. In the datasheet Y0…Y7 have a bar. It is more convenient this way: the "select" input of memories and devices also acts on a zero, so the wire goes straight. The enable inputs differ: E1, E2 act on a zero, E3 on a one. That is done on purpose.

74HC1383-to-8 decoder1A016VCC2A115Y03A214Y14E113Y25E212Y36E311Y47Y710Y58GND9Y6
74HC138: a number on A2 A1 A0 — a zero on one of Y0 … Y7 (active 0). It works only when E1 = 0, E2 = 0 and E3 = 1; otherwise all outputs are 1. Note that Y7 is pin 7 and Y0 is pin 15.

4.3. The idea: 4 → 16 from two 3 → 8

The top bit of the number goes to E1 of one chip and to E3 of the other: at 0 the first works, at 1 the second. No extra gates — that is why the 74HC138 has three enable inputs. Decoders with 32 and 64 outputs are built the same way.

4.4. Building

The same project as in lesson 3: the library has the gates and the chips of lessons 1–3.

Step 1. Chip 74HC138 — 3-to-8 decoder

Package pins from the datasheet: 1 A0, 2 A1, 3 A2, 4 E1, 5 E2, 6 E3, 7 Y7, 9 Y6, 10 Y5, 11 Y4, 12 Y3, 13 Y2, 14 Y1, 15 Y0; 8 — GND, 16 — VCC. A0 A1 A2 is a number (0–7); the output with this number becomes 0, the others are 1 (the outputs are active 0). Enable: E1, E2 — active 0, E3 — active 1; when not enabled, all outputs are 1. Inside — packages from lessons 1–2: 74HC04 (inverters), two 74HC08 (combinations and enable), two 74HC00 (outputs).

PartPinLabel
Input portoutputA0
Input portoutputA1
Input portoutputA2
Input portoutputE1
Input portoutputE2
Input portoutputE3
Output portinputY7
Output portinputY6
Output portinputY5
Output portinputY4
Output portinputY3
Output portinputY2
Output portinputY1
Output portinputY0
74HC041A
1Y
2A
2Y
3A
3Y
4Y
4A
5Y
5A
6A
A0
nA0
A1
nA1
A2
nA2
nE1
E1
nE2
E2
GND
74HC081A
1B
1Y
2A
2B
2Y
3Y
3A
3B
4Y
4A
4B
nA1
nA0
t0
nA1
A0
t1
t2
A1
nA0
t3
A1
A0
74HC081A
1B
1Y
2A
2B
2Y
3Y
3A
3B
4Y
4A
4B
nE1
nE2
g
g
E3
G
G0
G
nA2
G1
G
A2
74HC001A
1B
1Y
2A
2B
2Y
3Y
3A
3B
4Y
4A
4B
G0
t0
Y0
G0
t1
Y1
Y2
G0
t2
Y3
G0
t3
74HC001A
1B
1Y
2A
2B
2Y
3Y
3A
3B
4Y
4A
4B
G1
t0
Y4
G1
t1
Y5
Y6
G1
t2
Y7
G1
t3

Ctrl+G → name 74HC138. Legs: inputs A0, A1, A2, E1, E2, E3; outputs Y7, Y6, Y5, Y4, Y3, Y2, Y1, Y0.

Step 2. Clock and reset

Clock — 2Hz. The Reset button is CLR, through a 74HC04: the 74HC74 reset acts on a zero.

PartPinLabel
ClockoutputCLK
Button «Reset»outputCLR
74HC041A
1Y
2A
3A
4A
5A
6A
CLR
nCLR
GND
GND
GND
GND
GND

Step 3. Counter 0–15

Two 74HC74 — the same divider as in lesson 2: Q3 Q2 Q1 Q0 count 0–15.

PartPinLabel
74HC741RD
1D
1CP
1SD
1Q
1QN
2QN
2Q
2SD
2CP
2D
2RD
nCLR
Q0N
CLK
VCC
Q0
Q0N
Q1N
Q1
VCC
Q0N
Q1N
nCLR
74HC741RD
1D
1CP
1SD
1Q
1QN
2QN
2Q
2SD
2CP
2D
2RD
nCLR
Q2N
Q1N
VCC
Q2
Q2N
Q3N
Q3
VCC
Q2N
Q3N
nCLR

Step 4. Two 74HC138

The three low bits are the number within a group of eight, on A0 A1 A2 of both. The top bit Q3 chooses which one works: on the first it goes to E1 (works at 0), on the second to E3 (works at 1). That is what the enable inputs are for: two 3-to-8 decoders make a 4-to-16 without a single extra gate.

PartPinLabel
74HC138A0
A1
A2
E1
E2
E3
Y0
Y1
Y2
Y3
Y4
Y5
Y6
Y7
Q0
Q1
Q2
Q3
GND
VCC
L0
L1
L2
L3
L4
L5
L6
L7
74HC138A0
A1
A2
E1
E2
E3
Y0
Y1
Y2
Y3
Y4
Y5
Y6
Y7
Q0
Q1
Q2
GND
GND
Q3
L8
L9
L10
L11
L12
L13
L14
L15

Step 5. 16 lamps

Lamps L0–L15 go straight to the outputs. Put them side by side, in order.

PartPinLabel
LEDinputL0
LEDinputL1
LEDinputL2
LEDinputL3
LEDinputL4
LEDinputL5
LEDinputL6
LEDinputL7
LEDinputL8
LEDinputL9
LEDinputL10
LEDinputL11
LEDinputL12
LEDinputL13
LEDinputL14
LEDinputL15

4.5. Check

  1. Clock — 2Hz. Press Reset: all lamps are lit except L0.
  2. The dark lamp runs: L1, L2 … L15, then L0 again — one per CLK edge.
  3. At L7 → L8 the shadow jumps to the second 74HC138: Q3 became 1.

Why a shadow runs and not a light: the 74HC138 output is active 0, the chosen output goes out. On a real board an LED is connected to VCC through a resistor — and it lights exactly on a zero. There a light would run.

What you seeWhat it is
dark lampthe chosen 74HC138 output = 0
L0–L7first 74HC138 — while Q3 = 0
L8–L15second 74HC138 — when Q3 = 1

Lesson 4 paste (Cmd+I)

On top of lesson 3.

# Lesson 4 — 74HC138: a 3-to-8 decoder; a running light on 16 lamps (two 74HC138, a counter on two 74HC74).# Paste: Cmd+I on top of lesson 3. chip 74HC138 {  portIn(A0)  portIn(A1)  portIn(A2)  portIn(E1)  portIn(E2)  portIn(E3)  portOut(Y7)  portOut(Y6)  portOut(Y5)  portOut(Y4)  portOut(Y3)  portOut(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