Lesson 1. The CD4013 D flip-flop
In picturesAfter 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—Qbecomes equal toD; - the rest of the time
Qholds, andDcan change as it likes; S= 1 — 1 at once,R= 1 — 0 at once, without a clock.
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.
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.
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.
| Part | Pin | Label |
|---|---|---|
| Input port | output | A |
| Input port | output | N |
| Input port | output | P |
| Output port | input | B |
| nmos | gate drain source | N A B |
| pmos | gate 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.
| Part | Pin | Label |
|---|---|---|
| Input port | output | D |
| Input port | output | CL |
| Input port | output | S |
| Input port | output | R |
| Output port | input | Q |
| Output port | input | QN |
| NOT | I0 O0 | R nr |
| NOT | I0 O0 | S ns |
| NOT | I0 O0 | CL cln |
| NOT | I0 O0 | cln clp |
| TG | A N P B | D cln clp a |
| NAND | I0 I1 O0 | nr a m1 |
| NAND | I0 I1 O0 | m1 ns m2 |
| TG | A N P B | a clp cln m2w |
| Resistor (weak) | end 1 end 2 | m2w m2 |
| Capacitor (hold) | lead | a |
| TG | A N P B | m1 clp cln b |
| NAND | I0 I1 O0 | b ns s1 |
| NAND | I0 I1 O0 | nr s1 s2 |
| TG | A N P B | b cln clp s2w |
| Resistor (weak) | end 1 end 2 | s2w s2 |
| Capacitor (hold) | lead | b |
| NOT | I0 O0 | s1 q1 |
| NOT | I0 O0 | q1 Q |
| NOT | I0 O0 | b qn1 |
| NOT | I0 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.
| Part | Pin | Label |
|---|---|---|
| Input port | output | D1 |
| Input port | output | CLOCK1 |
| Input port | output | SET1 |
| Input port | output | RESET1 |
| Input port | output | D2 |
| Input port | output | CLOCK2 |
| Input port | output | SET2 |
| Input port | output | RESET2 |
| Output port | input | Q1 |
| Output port | input | QN1 |
| Output port | input | Q2 |
| Output port | input | QN2 |
| FF4013 | D CL S R Q QN | D1 CLOCK1 SET1 RESET1 Q1 QN1 |
| FF4013 | D 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.
| Part | Pin | Label |
|---|---|---|
| Power | output | VCC |
| Ground | output | GND |
| Input (switch) | output | D |
| Input (switch) | output | S |
| Input (switch) | output | R |
| Button «Clock» | output | CL |
| CD4013 | D1 CLOCK1 SET1 RESET1 D2 CLOCK2 SET2 RESET2 Q1 QN1 | D CL S R GND GND GND GND Q QN |
Step 5. Probe lamps
| Part | Pin | Label |
|---|---|---|
| LED | input | CL |
| LED | input | Q |
| LED | input | QN |
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.
| Part | Pin | Label |
|---|---|---|
| Clock | output | CK60 |
| Button «Reset» | output | RESET |
| CD4013 | D1 CLOCK1 RESET1 SET1 D2 CLOCK2 SET2 RESET2 Q1 QN1 | C30N CK60 RESET GND GND GND GND GND C30 C30N |
Step 7. Divider lamps
| Part | Pin | Label |
|---|---|---|
| LED | input | CK60 |
| LED | input | C30 |
1.6. Check
D flip-flop probe (switches D, S, R — at 0):
- First
R= 1 and back to 0 —Qgoes out,QNlights up. At power-on the flip-flop holds whatever it caught;Rsets it to 0. - Set
D= 1 —Qdoes not change: a D flip-flop listens to its input only on the clock edge. - Press Clock —
Qlights up,QNgoes out:Dwas written on the edge. - Hold Clock pressed and switch
D—Qstays. Only the moment of pressing writes (theCLedge 0 → 1), not the pressed state. D= 0, let go and press Clock again —Qgoes out.S= 1 —Qlights up at once, without a clock; setS= 0 back —Qholds.R= 1 —Qgoes out at once; setR= 0 back.
| What you did | Q | QN |
|---|---|---|
| D = 1, no clock | ○ | ● |
| pressed Clock | ● | ○ |
| holding Clock, D = 0 | ● | ○ |
| let go, Clock again | ○ | ● |
| S = 1 | ● | ○ |
| S = 0, R = 1 | ○ | ● |
Divider:
- At
60Hzthe lampsCK60andC30blink faster than the eye can see — they seem lit at half strength. - Click the generator body up to
1Hz(through4kHz…MAX):CK60blinks once a second, andC30changes each timeCK60lights up — lit for a second, off for a second. Half the frequency. - Hold Reset —
C30goes out and stays, the clocks have no effect. - Set the generator back to
60Hz(1Hz→2Hz→10Hz→60Hz) — the parking sensor needs it.
| Generator at 1Hz | CK60 | C30 |
|---|---|---|
| 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)¿No te sale? Algo no funciona →