Lesson 2. The «bug»: a bumper and the CD4017

In pictures

After this lesson the robot drives by itself: it goes forward, and when the bumper hits a wall or the obstacle it backs off, turns and drives on. There is no program — the CD4017 counter steps the manoeuvre and three gates drive the wheels.


2.1. Idea: a manoeuvre is a sequence of steps

The manoeuvre «bumped — backed off — turned» is a chain of steps in time. A 10 Hz clock gives a tick every 0.1 s, and the CD4017 lights the next of its ten outputs O0…O9 on each tick:

  • O0 — forward;
  • O1…O4 — back (4 ticks = 0.4 s);
  • O5…O8 — turn (0.4 s);
  • O9 — forward, and O0 again.

Folding four steps into one signal is a 4-input OR, the CD4072: BACK = O1 + O2 + O3 + O4, TURN = O5 + … + O8.

CKHITstep0123456789FWDBACKTURNbump — the counter goesback 0.4 s, turn 0.4 s
The manoeuvre in 10 Hz ticks: until a bump the INH inhibit holds the CD4017 on step 0. The bump releases it — steps 1–4 «back», 5–8 «turn», 9 — forward again.

2.2. Idea: wait for a bump — HOLD

What we want. While the bumper is free, the robot stays on step 0 and drives forward. It bumped — the counter walks through the steps. A manoeuvre once started is finished, even if the bumper is already released.

The CD4017 has an INH (inhibit) pin for this: while INH = 1, ticks are not counted. So we need the inhibit exactly when we are on step 0 AND there is no bump:

FWD (step 0)HIT (bump)HOLD (don't step)
101 — wait for a bump
110 — bump, go
000 — manoeuvre runs
010 — manoeuvre runs
  • Formula: HOLD = FWD AND NOT HIT.

It could be built as written: an inverter on HIT and an AND. But we already have NFWD = NOT FWD (it is needed later too), and De Morgan’s rule says: «A AND NOT B» is the same as «NOT (NOT A OR B)». That is one NOR gate:

  • HOLD = NOR(NFWD, HIT) — check with the table: HOLD = 1 only when NFWD = 0 and HIT = 0.

The NOR comes from the CD4001 — it has four such gates, the second one goes to the right wheel.

Why not «inhibit = NOT HIT» without FWD? Then, as soon as the bumper is released (the robot backed off a centimetre), the counter would stop in the middle of the manoeuvre — the robot would freeze half way. The FWD condition holds only step 0.

2.3. Idea: which wheel goes which way — LDIR and RDIR

What we want on each step (1 — the wheel forward, 0 — back):

StepLDIR (left)RDIR (right)
0 — forward11
1–4 — BACK00
5–8 — TURN10
9 — forward11
  • The left wheel goes back only on BACK: LDIR = NOT BACK — one inverter of the CD4069.
  • The right one goes back both on BACK and on TURN: RDIR = NOT (BACK OR TURN) = NOR(BACK, TURN) — the second gate of the CD4001.

On the turn the left wheel goes forward and the right one back — the robot spins clockwise on the spot. Want anticlockwise — swap LDIR and RDIR on the chassis pins.

And where do BACK and TURN come from? On each step exactly one CD4017 output is lit, so «going back» = «any of O1…O4 is lit» — a 4-input OR: BACK = O1 + O2 + O3 + O4, TURN = O5 + O6 + O7 + O8. Two such ORs are in one CD4072.

2.4. Build

On top of lesson 1. The chips are ready-made, from the kit file robot-chips.json; their pins carry labels (double-click a pin).

Step 1. Chips into the library

If the library has no Robot folder with the TI CD4017B, TI CD4072B, TI CD4069UB, TI CD4001B chips yet: the library’s import button → the robot-chips.json file. A Robot folder appears — ten chips for the whole kit (and the parts they are built from inside).

Step 2. Remove the lesson 1 trial

The chips now set the wheel direction — delete the LDIR and RDIR switches (select, Delete). The LDIR and RDIR labels on the chassis pins stay.

PartAction
Input (switch) whose output = LDIRdelete
Input (switch) whose output = RDIRdelete

Step 3. The clock and «Reset»

Clock — click its body until 10Hz.

PartPin / actionLabel / where
Clock 10HzoutputCK
ButtonoutputRESET

Step 4. The bumper

Drag the bumper by its orange nut to the front green point of the chassis — it snaps on. Length — with the handle (written by the handle): 110, wider than the wheels — otherwise a body corner hits the wall first and the bumper does not feel it.

PartPin / actionLabel / where
Bumperlength (handle)
HIT
mount
110
HIT
on f (front point of the chassis)

Step 5. Chips

PartPin / actionLabel / where
TI CD4017BCLOCK
RESET
INH
O0
O1
O2
O3
O4
O5
O6
O7
O8
CK
RESET
HOLD
FWD
B1
B2
B3
B4
T1
T2
T3
T4
TI CD4072BA
B
C
D
J
E
F
G
H
K
B1
B2
B3
B4
BACK
T1
T2
T3
T4
TURN
TI CD4069UBA
G
B
H
C
D
E
F
FWD
NFWD
BACK
LDIR
GND
GND
GND
GND
TI CD4001BA
B
J
C
D
K
E
F
G
H
BACK
TURN
RDIR
HIT
NFWD
HOLD
GND
GND
GND
GND

Leave unconnected: O9 and CO of the CD4017, I…L of the CD4069UB, L and M of the CD4001B.

Step 6. Manoeuvre LEDs

LED colour — Alt/⌘+click it.

PartPin / actionLabel / where
LED, greeninputFWD
LED, yellowinputBACK
LED, redinputTURN

2.5. Check

  1. Switch the power on and press «Reset» once — the green FWD is lit.
  2. Switch GO on — the robot drives forward.
  3. It reached a wall or the obstacle — the bumper turns red, and the robot by itself goes back (yellow BACK, about 0.4 s), turns on the spot clockwise (red TURN, 0.4 s) and goes forward again. Between the turn and the green, for a moment no LED is lit — that is step 9: the wheels already turn forward.
  4. If after the turn the bumper still presses — the green does not light, the robot backs off again at once. Out of a corner it gets in two or three such bounces.
  5. On the paper — straight runs and «stars» where it bumped.
LitWheelsRobot
FWD (green)both forwarddrives forward, waits for a bump
BACK (yellow)both backbacks off for about 0.4 s
TURN (red)left forward, right backturns clockwise 0.4 s — by ~35°

Lesson 2 paste (Cmd+I)

Paste into a new project — it puts lessons 1 and 2 whole (without the lesson 1 trial switches). The chips from robot-chips.json are needed.

# Lesson 2 — the bug: a bumper; the CD4017 steps the manoeuvre: forward, bump, back 0.4 s, turn 0.4 s, forward.# Paste: Cmd+I into a new project (puts lessons 1 and 2 whole, without the lesson 1 trial).# ---------- power ----------power(VCC)ground(GND)# ---------- arena: paper, contour over it, obstacle ----------mpaper p{w=800, h=500}mwall w{w=800, h=500}mblock ob{w=80, h=80}at(p, 0, 0)at(w, p, 0, 0)at(ob, p, 440, 210)# ---------- robot: chassis, pen on the rear point ----------mchassis rb{spd=60}(L_ON=GO, R_ON=GO, L_DIR=LDIR, R_DIR=RDIR)at(rb, p, 80, 210)mpen pen(DOWN=VCC)mount(pen, rb.r)# ---------- GO — motors (stays to the end of the kit) ----------input(GO)# ---------- clock and reset ----------clock{rt=10}(CK)button(RESET)# ---------- bumper on the front point of the chassis ----------mbumper bp{len=110}(HIT=HIT)mount(bp, rb.f)# ---------- CD4017: manoeuvre steps ----------"TI CD4017B"(CLOCK=CK, RESET=RESET, INH=HOLD, O0=FWD, O1=B1, O2=B2, O3=B3, O4=B4, O5=T1, O6=T2, O7=T3, O8=T4)# ---------- CD4072: back = steps 1-4, turn = steps 5-8 ----------"TI CD4072B"(A=B1, B=B2, C=B3, D=B4, J=BACK, E=T1, F=T2, G=T3, H=T4, K=TURN)# ---------- CD4069: not-forward, left wheel ----------"TI CD4069UB"(A=FWD, G=NFWD, B=BACK, H=LDIR, C=GND, D=GND, E=GND, F=GND)# ---------- CD4001: right wheel, hold step 0 ----------"TI CD4001B"(A=BACK, B=TURN, J=RDIR, C=HIT, D=NFWD, K=HOLD, E=GND, F=GND, G=GND, H=GND)# ---------- manoeuvre LEDs ----------led{color=green}(FWD)led{color=yellow}(BACK)led{color=red}(TURN)

Stuck? Something doesn't work →