Lesson 5. A shift register: 74HC595 and 8 lamps over three wires

In pictures

After this lesson the library has the 74HC595 — the very chip an Arduino uses to light eight LEDs over three wires — and the canvas has eight lamps that you fill bit by bit and show at once.


5.1. The idea: bits over one wire

A shift register is eight flip-flops in a chain: on every SHCP edge each one takes its neighbour's value, and the first takes the bit from DS. Eight edges — and the register holds a whole byte that came over one wire. The last flip-flop comes out on the Q7S pin: feed it to DS of the next 74HC595 — and you get 16, 24, 100 outputs over the same three wires.

74HC595shift register1Q116VCC2Q215Q03Q314DS4Q413OE5Q512STCP6Q611SHCP7Q710MR8GND9Q7S
74HC595: DS — data, SHCP — shift, STCP — latch (both on the edge), MR — clear the shift register, OE — enable the outputs (both active 0), Q0 … Q7 — outputs, Q7S — the output for the next 74HC595. Q0 is pin 15, the other outputs run from pin 1.

5.2. The idea: a latch shows everything at once

While a byte is shifting in, its bits run through all eight positions. If the lamps sat right on the shift register, they would flicker. So the 74HC595 has a second row of eight flip-flops — the latch: on the STCP edge it copies the whole byte at once, and the lamps change only once.

101100101Q70Q61Q51Q40Q30Q21Q10Q0shift register: a bit enters on the right and moves leftlatchDSQ7SSTCP edge —all 8 at once
Every SHCP edge shifts the register by one cell and puts the DS bit at the start. Eight edges — and the register holds the byte 1011 0010 (the top bit travelled furthest, to Q7). The STCP edge copies all eight bits into the latch, and the pins change at once.

5.3. The idea: the third state

The output of an ordinary gate is always 0 or 1. The 74HC595 output can be disconnected (OE = 1): the wire belongs to nobody. That is needed when several chips sit on the same wires — only one talks. A disconnected wire is neither 0 nor 1 by itself, so a resistor to ground is put on the board. Inside, the switch is a Transmission gate: at OE = 0 it connects the latch to the pin, at OE = 1 it opens.

5.4. Building

The same project as in lesson 4: the library has the 74HC04 and the 74HC74.

Step 1. Chip 74HC595 — shift register with a latch

Package pins from the datasheet: 1 Q1, 2 Q2, 3 Q3, 4 Q4, 5 Q5, 6 Q6, 7 Q7, 9 Q7S, 10 MR, 11 SHCP, 12 STCP, 13 OE, 14 DS, 15 Q0; 8 — GND, 16 — VCC. DS — data, SHCP — shift (edge), STCP — latch (edge), MR — clears the shift register (active 0), OE — enables the outputs (active 0), Q7S — serial output (for a chain of 74HC595). Inside: 16 flip-flops — eight 74HC74 (8 for shifting, 8 for the latch), a 74HC04 and eight Transmission gate switches — the third state: at OE = 1 the outputs are disconnected from the latch.

PartPinLabel
Input portoutputMR
Input portoutputSHCP
Input portoutputSTCP
Input portoutputOE
Input portoutputDS
Output portinputQ1
Output portinputQ2
Output portinputQ3
Output portinputQ4
Output portinputQ5
Output portinputQ6
Output portinputQ7
Output portinputQ0
74HC741RD
1D
1CP
1SD
1Q
1QN
2QN
2Q
2SD
2CP
2D
2RD
MR
DS
SHCP
VCC
s0
sn0
sn1
s1
VCC
SHCP
s0
MR
74HC741RD
1D
1CP
1SD
1Q
1QN
2QN
2Q
2SD
2CP
2D
2RD
MR
s1
SHCP
VCC
s2
sn2
sn3
s3
VCC
SHCP
s2
MR
74HC741RD
1D
1CP
1SD
1Q
1QN
2QN
2Q
2SD
2CP
2D
2RD
MR
s3
SHCP
VCC
s4
sn4
sn5
s5
VCC
SHCP
s4
MR
74HC741RD
1D
1CP
1SD
1Q
1QN
2QN
2Q
2SD
2CP
2D
2RD
MR
s5
SHCP
VCC
s6
sn6
sn7
s7
VCC
SHCP
s6
MR
74HC741RD
1D
1CP
1SD
1Q
1QN
2QN
2Q
2SD
2CP
2D
2RD
VCC
s0
STCP
VCC
r0
rn0
rn1
r1
VCC
STCP
s1
VCC
74HC741RD
1D
1CP
1SD
1Q
1QN
2QN
2Q
2SD
2CP
2D
2RD
VCC
s2
STCP
VCC
r2
rn2
rn3
r3
VCC
STCP
s3
VCC
74HC741RD
1D
1CP
1SD
1Q
1QN
2QN
2Q
2SD
2CP
2D
2RD
VCC
s4
STCP
VCC
r4
rn4
rn5
r5
VCC
STCP
s5
VCC
74HC741RD
1D
1CP
1SD
1Q
1QN
2QN
2Q
2SD
2CP
2D
2RD
VCC
s6
STCP
VCC
r6
rn6
rn7
r7
VCC
STCP
s7
VCC
74HC041A
1Y
2A
3A
4A
5A
6A
OE
en
GND
GND
GND
GND
GND
Transmission gateA
Enable
B
r0
en
Q0
Transmission gateA
Enable
B
r1
en
Q1
Transmission gateA
Enable
B
r2
en
Q2
Transmission gateA
Enable
B
r3
en
Q3
Transmission gateA
Enable
B
r4
en
Q4
Transmission gateA
Enable
B
r5
en
Q5
Transmission gateA
Enable
B
r6
en
Q6
Transmission gateA
Enable
B
r7
en
Q7

Ctrl+G → name 74HC595. Legs: inputs MR, SHCP, STCP, OE, DS; outputs Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q0.

Step 2. Three wires and buttons

Switch DATA is the bit we shift in. The Shift button is SH, Latch — ST, Clear — CLRB. Switch OFF turns the outputs off (OE).

PartPinLabel
Input (switch)outputDATA
Button «Shift»outputSH
Button «Latch»outputST
Button «Clear»outputCLRB
Input (switch)outputOFF

Step 3. Clear through a 74HC04

The 74HC595 MR is active 0, the button gives 1: we invert it.

PartPinLabel
74HC041A
1Y
2A
3A
4A
5A
6A
CLRB
nCLRB
GND
GND
GND
GND
GND
74HC595DS
SHCP
STCP
MR
OE
Q0
Q1
Q2
Q3
Q4
Q5
Q6
Q7
Q7S
DATA
SH
ST
nCLRB
OFF
L0
L1
L2
L3
L4
L5
L6
L7
SO

Step 4. 74HC595

Outputs Q0–Q7 go to lamps L0–L7, Q7S to lamp SO.

PartPinLabel
74HC595DS
SHCP
STCP
MR
OE
Q0
Q1
Q2
Q3
Q4
Q5
Q6
Q7
Q7S
DATA
SH
ST
nCLRB
OFF
L0
L1
L2
L3
L4
L5
L6
L7
SO

Step 5. Resistors to ground

When the outputs are off (OE = 1), the wire from the 74HC595 is connected to nothing — the third state. By itself it is neither 0 nor 1, so on a real board you put a resistor to ground: it pulls the disconnected wire to 0. Resistor from the palette — between L<n> and GND.

PartPinLabel
Resistorend 1
end 2
L0
GND
Resistorend 1
end 2
L1
GND
Resistorend 1
end 2
L2
GND
Resistorend 1
end 2
L3
GND
Resistorend 1
end 2
L4
GND
Resistorend 1
end 2
L5
GND
Resistorend 1
end 2
L6
GND
Resistorend 1
end 2
L7
GND

Step 6. Lamps

Eight lamps in a row, L7 on the left — like a byte: the top bit on the left.

PartPinLabel
LEDinputL0
LEDinputL1
LEDinputL2
LEDinputL3
LEDinputL4
LEDinputL5
LEDinputL6
LEDinputL7
LEDinputSO

5.5. Check

  1. Press Clear, then Latch: all lamps go out.
  2. Shift in the byte 1011 0010 — top bit first: for each bit set DATA (1, 0, 1, 1, 0, 0, 1, 0) and press Shift. Lamps L0–L7 do not change — the data is still in the shift register. Lamp SO shows the bit coming out at the end.
  3. Press Latch: the lamps show 1011 0010 at once (L7 on the left).
  4. OFF = 1 — all lamps go out: the outputs are disconnected, the resistors pull the wires to 0. OFF = 0 — 1011 0010 again.

Why it is handy: the byte goes in over one wire, and the lamps change only on Latch — all at once, without flicker. Three wires — DATA, Shift, Latch — instead of eight; that is how an Arduino drives eight LEDs, and a chain of 74HC595 through Q7S — even a hundred.


Lesson 5 paste (Cmd+I)

On top of lesson 4.

# Lesson 5 — 74HC595: a shift register with a latch; 8 lamps over three wires, like Arduino.# Paste: Cmd+I on top of lesson 4. chip 74HC595 {  portIn(MR)  portIn(SHCP)  portIn(STCP)  portIn(OE)  portIn(DS)  portOut(Q1)  portOut(Q2)  portOut(Q3)  portOut(Q4)  portOut(Q5)  portOut(Q6)  portOut(Q7)  portOut(Q0)  portOut{name=Q7S}(s7)  74HC74(1RD=MR, 1D=DS, 1CP=SHCP, 1SD=VCC, 1Q=s0, 1QN=sn0, 2QN=sn1, 2Q=s1, 2SD=VCC, 2CP=SHCP, 2D=s0, 2RD=MR)  74HC74(1RD=MR, 1D=s1, 1CP=SHCP, 1SD=VCC, 1Q=s2, 1QN=sn2, 2QN=sn3, 2Q=s3, 2SD=VCC, 2CP=SHCP, 2D=s2, 2RD=MR)  74HC74(1RD=MR, 1D=s3, 1CP=SHCP, 1SD=VCC, 1Q=s4, 1QN=sn4, 2QN=sn5, 2Q=s5, 2SD=VCC, 2CP=SHCP, 2D=s4, 2RD=MR)  74HC74(1RD=MR, 1D=s5, 1CP=SHCP, 1SD=VCC, 1Q=s6, 1QN=sn6, 2QN=sn7, 2Q=s7, 2SD=VCC, 2CP=SHCP, 2D=s6, 2RD=MR)  74HC74(1RD=VCC, 1D=s0, 1CP=STCP, 1SD=VCC, 1Q=r0, 1QN=rn0, 2QN=rn1, 2Q=r1, 2SD=VCC, 2CP=STCP, 2D=s1, 2RD=VCC)  74HC74(1RD=VCC, 1D=s2, 1CP=STCP, 1SD=VCC, 1Q=r2, 1QN=rn2, 2QN=rn3, 2Q=r3, 2SD=VCC, 2CP=STCP, 2D=s3, 2RD=VCC)  74HC74(1RD=VCC, 1D=s4, 1CP=STCP, 1SD=VCC, 1Q=r4, 1QN=rn4, 2QN=rn5, 2Q=r5, 2SD=VCC, 2CP=STCP, 2D=s5, 2RD=VCC)  74HC74(1RD=VCC, 1D=s6, 1CP=STCP, 1SD=VCC, 1Q=r6, 1QN=rn6, 2QN=rn7, 2Q=r7, 2SD=VCC, 2CP=STCP, 2D=s7, 2RD=VCC)  74HC04(1A=OE, 1Y=en, 2A=GND, 3A=GND, 4A=GND, 5A=GND, 6A=GND)  tgate(r0, en, Q0)  tgate(r1, en, Q1)  tgate(r2, en, Q2)  tgate(r3, en, Q3)  tgate(r4, en, Q4)  tgate(r5, en, Q5)  tgate(r6, en, Q6)  tgate(r7, en, Q7)} input(DATA)button{label="Shift"}(SH)button{label="Latch"}(ST)button{label="Clear"}(CLRB)input(OFF)74HC04(1A=CLRB, 1Y=nCLRB, 2A=GND, 3A=GND, 4A=GND, 5A=GND, 6A=GND)74HC595(DS=DATA, SHCP=SH, STCP=ST, MR=nCLRB, OE=OFF, Q0=L0, Q1=L1, Q2=L2, Q3=L3, Q4=L4, Q5=L5, Q6=L6, Q7=L7, Q7S=SO)resistor(L0, GND)resistor(L1, GND)resistor(L2, GND)resistor(L3, GND)resistor(L4, GND)resistor(L5, GND)resistor(L6, GND)resistor(L7, GND)led(L0)led(L1)led(L2)led(L3)led(L4)led(L5)led(L6)led(L7)led(SO)

Stuck? Something doesn't work →