Lesson 3. Ultrasound: centimetres on the display
In picturesAfter this lesson the robot has an ultrasonic range finder in front, and the display shows how many centimetres it is to the wall or obstacle ahead. Six times a second: ping, echo, count, show.
3.1. Idea: time is distance
The sensor (like a real HC-SR04): give TRIG a pulse — on its fall the sensor «beeps». While the sound flies to the wall and back, ECHO is 1. The farther the wall, the longer ECHO stays on: here — 0.125 ms per centimetre.
So the centimetres can be counted: take a clock with a period of exactly 0.125 ms (that is 8 kHz) and count its ticks while ECHO is on. As many ticks — as many centimetres.
3.2. Idea: a measurement is a sequence of steps
The second CD4017 steps from a 60 Hz clock (a step is 16.7 ms) and splits the measurement into steps:
- step 0,
PING: the counter is reset to 00, the ping goes off at the end of the step; - step 1: the sound flies,
ECHOis on, the counter counts. The farthest, 99 cm, is 12.4 ms: it fits in the step; - step 2,
SHOW: the finished number is copied to the display; - steps 3…9 — a pause. Ten steps — 1/6 of a second: six measurements a second.
3.3. Idea: the centimetre counter — two CD4029s
The CD4029 counts on the CLOCK edge if allowed: the CI (enable) pin works the other way round — it counts when CI = 0. With BD = 0 it is decimal: 0…9 and 0 again, and on the 9 → 0 step it gives a carry CO = 0. Two packages in a chain: the units’ CO into the tens’ CI, and we have a 00…99 counter. PE = 1 loads the J1…J4 pins into the counter — ours are on GND: on the PING step the counter sits at 00.
3.4. Idea: when to count — CIU
What we want: count while the echo is on, but not past 99 — otherwise after 99 the counter would roll over to 00 and the display would show «wall right here» when there is none at all.
| ECHOS (echo) | NFULL (not 99) | CIU (0 — count) |
|---|---|---|
| 1 | 1 | 0 — counting |
| 1 | 0 | 1 — 99, stop |
| 0 | any | 1 — no echo, stop |
- Count =
ECHOSANDNFULL; theCIpin must be 0 when counting:CIU=NOT (ECHOS AND NFULL)=NAND(ECHOS,NFULL) — one gate of the CD4011.
3.5. Idea: «full» — FULL
What we want: FULL = 1 exactly at 99. The counter is binary-coded decimal: each digit is 4 bits, 9 = 1001. Of the digits 0…9 only nine has both the highest and the lowest bit on (8 = 1000, 1 = 0001). So:
- the tens are nine:
X9=CT3ANDCT0; - the units are nine:
Y9=CU3ANDCU0; FULL=X9ANDY9— threeANDgates of the CD4081;NFULL=NOT FULL— an inverter of the CD4069.
3.6. Idea: the echo in step with the clock — ECHOS
ECHO goes on and off at any moment — including the very instant an 8 kHz edge arrives. The counter could then count «half» a tick. So the echo first passes through a CD4013 D flip-flop that writes it on the falling edge of the clock (CL1 = NCK8K — the clock through an inverter): ECHOS changes only between counting edges, and the counter sees it clean.
3.7. Idea: a display that does not flicker
If the counter itself were shown, the display would run 00 → 01 → … → number on every measurement. So two more CD4029s work as a register: CLOCK = GND (they do not count), PE = SHOW — on the show step they load the finished number from the counter and hold it until the next measurement.
The display is an 8-bit Display (bus) in HEX view: each hexadecimal digit is 4 bits, and we have exactly 4 bits per decimal digit. So a binary-coded decimal number on a HEX display reads like an ordinary one: 0011 0111 — 37.
3.8. Build
On top of lesson 2. The robot still turns away when the bumper hits, and now it has an ultrasonic range finder in front: the display shows how many centimetres it is to the wall or obstacle ahead.
Step 1. Ultrasound on the chassis
The sensor — Sensors menu → Ultrasonic: it comes together with a reflector — delete the reflector, it is not needed: any wall or obstacle gives an echo. Drag the sensor by its orange nut to the centre green point of the chassis — it snaps on nose forward. The labels go on the sensor pins.
| Part | Pin / action | Label / where |
|---|---|---|
| Ultrasonic (sensor) | TRIG ECHO mount | PING ECHO on c (centre of the chassis) |
Step 2. Clocks
The first Clock — click until 60Hz, the second — until 8kHz.
| Part | Pin / action | Label / where |
|---|---|---|
| Clock 60Hz | output | CK60 |
| Clock 8kHz | output | CK8K |
Step 3. Chips
| Part | Pin / action | Label / where |
|---|---|---|
| TI CD4017B | CLOCK RESET INH O0 O2 | CK60 RESET GND PING SHOW |
| TI CD4069UB | A G B H C D E F | CK8K NCK8K FULL NFULL GND GND GND GND |
| TI CD4013B | S1 R1 D1 CL1 Q1 S2 R2 D2 CL2 | GND RESET ECHO NCK8K ECHOS GND GND GND GND |
| TI CD4081B | A B J C D K E F L G H | CT3 CT0 X9 CU3 CU0 Y9 X9 Y9 FULL GND GND |
| TI CD4011B | A B J C D E F G H | ECHOS NFULL CIU GND GND GND GND GND GND |
Leave unconnected: on the second CD4017 — O1, O3…O9, CO; on the second CD4069 — I…L; on the CD4013 — QN1, Q2, QN2; on the CD4081 — M; on the CD4011 — K, L, M.
Step 4. The counter and the number for the display
| Part | Pin / action | Label / where |
|---|---|---|
| TI CD4029B | CLOCK PE CI UD BD J1 J2 J3 J4 Q1 Q2 Q3 Q4 CO | CK8K PING CIU VCC GND GND GND GND GND CU0 CU1 CU2 CU3 COU |
| TI CD4029B | CLOCK PE CI UD BD J1 J2 J3 J4 Q1 Q2 Q3 Q4 | CK8K PING COU VCC GND GND GND GND GND CT0 CT1 CT2 CT3 |
| TI CD4029B | CLOCK PE CI UD BD J1 J2 J3 J4 Q1 Q2 Q3 Q4 | GND SHOW VCC GND GND CU0 CU1 CU2 CU3 DU0 DU1 DU2 DU3 |
| TI CD4029B | CLOCK PE CI UD BD J1 J2 J3 J4 Q1 Q2 Q3 Q4 | GND SHOW VCC GND GND CT0 CT1 CT2 CT3 DT0 DT1 DT2 DT3 |
Leave unconnected: CO of the counter tens and of the two display CD4029s.
Step 5. The display
Display (bus) from the palette. ⚙ in the top-left corner — width (8 is needed, and it is already set), the tag in the top-right — click until HEX. The pins are on the left, top to bottom — from the highest bit to the lowest.
| Part | Pin / action | Label / where |
|---|---|---|
| Display (bus) | width (⚙) / view bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 | 8 bits / HEX DT3 DT2 DT1 DT0 DU3 DU2 DU1 DU0 |
3.9. Check
- Switch the power on, press «Reset».
GOis off. - The display shows the distance in centimetres from the sensor’s nose to whatever is in front of the robot. Drag the robot with the mouse nose to a wall — the number changes: closer — smaller.
- Farther than 99 cm — the display shows 99: the counter does not count that far.
- Switch
GOon — the robot drives, and the number melts as it nears the wall. At the bump — 00 (if it came in at an angle, the bumper touches with its corner — the display shows a few centimetres), the lesson 2 manoeuvre, and after the turn the number is big again.
| Where the robot is (to wall) | Display |
|---|---|
| farther than 99 cm | 99 |
| 50 cm (250 canvas points) | 50 |
| 13 cm (65 points) | 13 |
| bumper pressed head-on | 00 |
5 canvas points — 1 cm; a background cell — 10 points, 2 cm.
Lesson 3 paste (Cmd+I)
Paste into a new project — it puts lessons 1–3 whole. The chips from robot-chips.json are needed.
# Lesson 3 — ultrasound: a ping, the echo is counted in centimetres (8 kHz = 1 cm per tick), the number on the display.# Paste: Cmd+I into a new project (puts lessons 1-3 whole).# ---------- 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)# ---------- ultrasound on the centre of the chassis ----------ustx us(TRIG=PING, ECHO=ECHO)mount(us, rb.c)# ---------- clocks for the steps and the centimetres ----------clock{rt=60}(CK60)clock{hz=1}(CK8K)# ---------- CD4017: step 0 — ping, 1 — echo, 2 — show ----------"TI CD4017B"(CLOCK=CK60, RESET=RESET, INH=GND, O0=PING, O2=SHOW)# ---------- CD4069: NOT of the 8 kHz clock, NOT full ----------"TI CD4069UB"(A=CK8K, G=NCK8K, B=FULL, H=NFULL, C=GND, D=GND, E=GND, F=GND)# ---------- CD4013: echo synced to the clock (second flip-flop — lesson 4) ----------"TI CD4013B"(S1=GND, R1=RESET, D1=ECHO, CL1=NCK8K, Q1=ECHOS, S2=GND, R2=GND, D2=GND, CL2=GND)# ---------- CD4081: 99 is full ----------"TI CD4081B"(A=CT3, B=CT0, J=X9, C=CU3, D=CU0, K=Y9, E=X9, F=Y9, L=FULL, G=GND, H=GND)# ---------- CD4011: count while echo and not full ----------"TI CD4011B"(A=ECHOS, B=NFULL, J=CIU, C=GND, D=GND, E=GND, F=GND, G=GND, H=GND)# ---------- CD4029 x 2: centimetre counter (units, tens) ----------"TI CD4029B"(CLOCK=CK8K, PE=PING, CI=CIU, UD=VCC, BD=GND, J1=GND, J2=GND, J3=GND, J4=GND, Q1=CU0, Q2=CU1, Q3=CU2, Q4=CU3, CO=COU)"TI CD4029B"(CLOCK=CK8K, PE=PING, CI=COU, UD=VCC, BD=GND, J1=GND, J2=GND, J3=GND, J4=GND, Q1=CT0, Q2=CT1, Q3=CT2, Q4=CT3)# ---------- CD4029 x 2: the number for the display (loaded on the show step) ----------"TI CD4029B"(CLOCK=GND, PE=SHOW, CI=VCC, UD=GND, BD=GND, J1=CU0, J2=CU1, J3=CU2, J4=CU3, Q1=DU0, Q2=DU1, Q3=DU2, Q4=DU3)"TI CD4029B"(CLOCK=GND, PE=SHOW, CI=VCC, UD=GND, BD=GND, J1=CT0, J2=CT1, J3=CT2, J4=CT3, Q1=DT0, Q2=DT1, Q3=DT2, Q4=DT3)# ---------- display: tens DT3...DT0, units DU3...DU0 ----------display{base=HEX, bits=8}(DT3, DT2, DT1, DT0, DU3, DU2, DU1, DU0)¿No te sale? Algo no funciona →