Lesson 1. The CD4013 D flip-flop

In pictures

After this lesson the library has the CD4013 chip — two D flip-flops, and the canvas has the first part of the parking sensor: the divider the whole device steps on. We try the flip-flop with switches and a button.


1.1. Idea: remember at a moment

The parking sensor polls its sensors in turn, step by step. The steps need a clock, and the clock needs an element that remembers the value at the moment the clock changes from 0 to 1 (the edge) and holds it until the next edge, whatever the input does. This is a D flip-flop: input D (data), clock CL, output Q.

  • an edge of CL — Q becomes equal to D;
  • the rest of the time Q holds, and D can change as it likes;
  • S = 1 — 1 at once, R = 1 — 0 at once, without a clock.
CLDQQ takes D only at the CL rising edge (dashed); the rest of the time it holds, whatever D does
A D flip-flop on a timing chart: at every CL rising edge (0 → 1) the output Q becomes equal to D at that moment. Between edges D may jump — Q stays.

1.2. Idea: master and slave

Inside are two latches in a row. The first (the master) lets D through while CL = 0 and closes when CL = 1. The second (the slave) does the opposite: it opens when CL = 1 and takes what the master has locked. Together they are never open all the way through, so Q changes only on the edge.

Each latch is two NANDs and two TG switches: the data switch and the feedback switch. A switch is a pair of transistors, nmos + pmos; it closes the wire when N = 1 and P = 0. The switches are driven by cln = NOT CL and clp = NOT cln — always opposite: when one switch of a latch is closed, the other is open.

RSDCLQQNNOTNOTNOTNOTTGABNPNANDNANDTGABNPresistorcapTGABNPNANDNANDTGABNPresistorcapNOTNOTNOTNOTQQNnrnsclnclpam1m2m2wbs1s2s2wq1qn1MASTER — passes D while CL = 0SLAVE — takes the master while CL = 1
FF4013 — the CD4013 D flip-flop as one drawing, for building with wires; the drawing scrolls sideways. The master on the left passes D while CL = 0; the slave on the right — while CL = 1. TG is a switch of two transistors: legs A, B at the sides, N on top, P below. Resistors make the feedback weak, cap holds the node.

1.3. Idea: weak feedback

For the moment the clock switches, cln and clp are both "halfway", and both switches of a latch are closed: the input meets the output of the feedback. In a real chip the feedback transistors are small — the input wins. Here this is a Resistor (weak) in front of the feedback switch: a "weak wire" that gives way to a real output.

Capacitor (hold) on nodes a and b is their capacitance: it holds the value when both switches are open for a moment.

1.4. Idea: divide by 2

If QN is fed back to D, on every edge the flip-flop writes its own inverted value: Q changes every other clock. A 60 Hz generator turns into 30 Hz — this is the step of polling the parking sensor's sensors.

CK60C30C30 changes at every CK60 rising edge — half as fast: 60 Hz → 30 Hz
The divide-by-2 on a timing chart: QN is fed to D, and every CK60 edge flips C30.
Clock 60HzResetCD4013first flip-flopCLOCK13RESET14SET16D15Q11QN12LEDC30NC30
A divide-by-2: output QN1 is fed back to D1, so every CK60 edge stores the inverse — C30 changes every other tick. Numbers at the legs are the TI pin numbers.

1.5. Build

A new project. You need the gates NOT and NAND — from lesson 0 or any kit of the series.

Step 1. Chip TG — a switch on a pair of transistors

If TG is already in the library (from the "Visitor Counter" kit — inside the CD4511), skip this step.

A switch as on the TI drawings: nmos and pmos side by side, both connect A to B. nmos conducts at N = 1, pmos — at P = 0. They are always driven by a pair of opposite signals: N = 1 and P = 0 — the switch is closed, N = 0 and P = 1 — open. Transistor: gate, drain, source.

PartPinLabel
Input portoutputA
Input portoutputN
Input portoutputP
Output portinputB
nmosgate
drain
source
N
A
B
pmosgate
drain
source
P
A
B

Ctrl+G → name TG. Legs: inputs A, N, P; outputs B.

Step 2. Chip FF4013 — CD4013 D flip-flop (master and slave)

The whole circuit as one drawing — above, in the "Idea" (and in the file ff4013-schema.html). Build order: the inverters R, S, CL; the master — the data switch, two NANDs, the feedback switch with a resistor, cap on node a; the slave — the same; four inverters for the outputs. Resistor (weak) and Capacitor (hold) — from the palette. On the switches watch where N and P go: on the master data switch N = cln, on the master → slave switch — the other way round, N = clp.

PartPinLabel
Input portoutputD
Input portoutputCL
Input portoutputS
Input portoutputR
Output portinputQ
Output portinputQN
NOTI0
O0
R
nr
NOTI0
O0
S
ns
NOTI0
O0
CL
cln
NOTI0
O0
cln
clp
TGA
N
P
B
D
cln
clp
a
NANDI0
I1
O0
nr
a
m1
NANDI0
I1
O0
m1
ns
m2
TGA
N
P
B
a
clp
cln
m2w
Resistor (weak)end 1
end 2
m2w
m2
Capacitor (hold)leada
TGA
N
P
B
m1
clp
cln
b
NANDI0
I1
O0
b
ns
s1
NANDI0
I1
O0
nr
s1
s2
TGA
N
P
B
b
cln
clp
s2w
Resistor (weak)end 1
end 2
s2w
s2
Capacitor (hold)leadb
NOTI0
O0
s1
q1
NOTI0
O0
q1
Q
NOTI0
O0
b
qn1
NOTI0
O0
qn1
QN

Ctrl+G → name FF4013. Legs: inputs D, CL, S, R; outputs Q, QN.

Step 3. Chip CD4013 — 2 × D flip-flop

Package pins from the TI datasheet: 1 Q1, 2 QN1, 3 CLOCK1, 4 RESET1, 5 D1, 6 SET1, 8 SET2, 9 D2, 10 RESET2, 11 CLOCK2, 12 QN2, 13 Q2; 7 — VSS (ground), 14 — VDD (power) — our power is shared, so these pins are not there. Inside — two FF4013: the first on the pins with the digit 1, the second — with the digit 2.

PartPinLabel
Input portoutputD1
Input portoutputCLOCK1
Input portoutputSET1
Input portoutputRESET1
Input portoutputD2
Input portoutputCLOCK2
Input portoutputSET2
Input portoutputRESET2
Output portinputQ1
Output portinputQN1
Output portinputQ2
Output portinputQN2
FF4013D
CL
S
R
Q
QN
D1
CLOCK1
SET1
RESET1
Q1
QN1
FF4013D
CL
S
R
Q
QN
D2
CLOCK2
SET2
RESET2
Q2
QN2

Ctrl+G → name CD4013. Legs: inputs D1, CLOCK1, SET1, RESET1, D2, CLOCK2, SET2, RESET2; outputs Q1, QN1, Q2, QN2.

Step 4. Probe: switches, the Clock button, lamps

Button (momentary) — caption "Clock" (double-click the button). The second flip-flop of the package is not needed in the probe — its inputs go to GND.

PartPinLabel
PoweroutputVCC
GroundoutputGND
Input (switch)outputD
Input (switch)outputS
Input (switch)outputR
Button «Clock»outputCL
CD4013D1
CLOCK1
SET1
RESET1
D2
CLOCK2
SET2
RESET2
Q1
QN1
D
CL
S
R
GND
GND
GND
GND
Q
QN

Step 5. Probe lamps

PartPinLabel
LEDinputCL
LEDinputQ
LEDinputQN

Step 6. Parking sensor: generator, Reset, divide-by-2

This is the first part of the parking sensor — it stays until the end of the kit. Clock — click the body up to 60Hz. The button — caption "Reset". The output QN1 (C30N) is fed back to D1: on every edge of CK60 the flip-flop writes its own inverted value, and C30 changes every other clock — 30 times a second.

PartPinLabel
ClockoutputCK60
Button «Reset»outputRESET
CD4013D1
CLOCK1
RESET1
SET1
D2
CLOCK2
SET2
RESET2
Q1
QN1
C30N
CK60
RESET
GND
GND
GND
GND
GND
C30
C30N

Step 7. Divider lamps

PartPinLabel
LEDinputCK60
LEDinputC30

1.6. Check

D flip-flop probe (switches D, S, R — at 0):

  1. First R = 1 and back to 0 — Q goes out, QN lights up. At power-on the flip-flop holds whatever it caught; R sets it to 0.
  2. Set D = 1 — Q does not change: a D flip-flop listens to its input only on the clock edge.
  3. Press Clock — Q lights up, QN goes out: D was written on the edge.
  4. Hold Clock pressed and switch D — Q stays. Only the moment of pressing writes (the CL edge 0 → 1), not the pressed state.
  5. D = 0, let go and press Clock again — Q goes out.
  6. S = 1 — Q lights up at once, without a clock; set S = 0 back — Q holds. R = 1 — Q goes out at once; set R = 0 back.
What you didQQN
D = 1, no clock○●
pressed Clock●○
holding Clock, D = 0●○
let go, Clock again○●
S = 1●○
S = 0, R = 1○●

Divider:

  1. At 60Hz the lamps CK60 and C30 blink faster than the eye can see — they seem lit at half strength.
  2. Click the generator body up to 1Hz (through 4kHz … MAX): CK60 blinks once a second, and C30 changes each time CK60 lights up — lit for a second, off for a second. Half the frequency.
  3. Hold Reset — C30 goes out and stays, the clocks have no effect.
  4. Set the generator back to 60Hz (1Hz → 2Hz → 10Hz → 60Hz) — the parking sensor needs it.
Generator at 1HzCK60C30
1st second●●
2nd second○●
3rd second●○
4th second○○
holding Reset●/○○

Keep the probe until lesson 2 — there you can delete it. Do not touch the divider: the whole parking sensor steps on it.


Lesson 1 paste (Cmd+I)

You need the gates NOT, NAND (lesson 0). If TG is already in the library, delete its block from the paste.

# Lesson 1 — CD4013B: a D flip-flop on TG switches (master and slave), a 2 × D flip-flop chip; the parking sensor 60 → 30 Hz divider.# Paste: Cmd+I into a new project with the NOT and NAND gates (lesson 0). chip TG {  portIn(A)  portIn(N)  portIn(P)  portOut(B)  nmos(N, A, B)  pmos(P, A, B)} chip FF4013 {  portIn(D)  portIn(CL)  portIn(S)  portIn(R)  portOut(Q)  portOut(QN)  NOT(R, nr)  NOT(S, ns)  NOT(CL, cln)  NOT(cln, clp)  TG(A=D, N=cln, P=clp, B=a)  NAND(nr, a, m1)  NAND(m1, ns, m2)  TG(A=a, N=clp, P=cln, B=m2w)  resistor(m2w, m2)  cap(a)  TG(A=m1, N=clp, P=cln, B=b)  NAND(b, ns, s1)  NAND(nr, s1, s2)  TG(A=b, N=cln, P=clp, B=s2w)  resistor(s2w, s2)  cap(b)  NOT(s1, q1)  NOT(q1, Q)  NOT(b, qn1)  NOT(qn1, QN)} chip CD4013 {  portIn(D1)  portIn(CLOCK1)  portIn(SET1)  portIn(RESET1)  portIn(D2)  portIn(CLOCK2)  portIn(SET2)  portIn(RESET2)  portOut(Q1)  portOut(QN1)  portOut(Q2)  portOut(QN2)  FF4013(D=D1, CL=CLOCK1, S=SET1, R=RESET1, Q=Q1, QN=QN1)  FF4013(D=D2, CL=CLOCK2, S=SET2, R=RESET2, Q=Q2, QN=QN2)} power(VCC)ground(GND)input(D)input(S)input(R)button{label="Clock"}(CL)CD4013(D1=D, CLOCK1=CL, SET1=S, RESET1=R, D2=GND, CLOCK2=GND, SET2=GND, RESET2=GND, Q1=Q, QN1=QN)led(CL)led(Q)led(QN)clock{rt=60}(CK60)button{label="Reset"}(RESET)CD4013(D1=C30N, CLOCK1=CK60, RESET1=RESET, SET1=GND, D2=GND, CLOCK2=GND, SET2=GND, RESET2=GND, Q1=C30, QN1=C30N)led(CK60)led(C30)

Stuck? Something doesn't work →