What we are making: a machine that measures how far away something is, in
centimetres — and then we check whether it is telling the truth.
Photo: ultrasonic sensor pointed at a book held at a measured distance, ruler visible
The idea
The two silver cylinders are a speaker and a microphone. The sensor chirps — far too high
for you to hear — and then listens for the echo coming back.
Sound travels at a known speed. If the board times how long the echo took, it can work out how
far the sound travelled. This is what bats do, and what a ship's sonar does.
💡 Tip
The number you get is already in centimetres, not 0 to 1023. That is because the sensor does the calculation itself before answering. Different sensors speak in different units — always check which.
Checking whether a sensor is honest
Today you do something engineers do constantly and beginners never do: you check the sensor
against something you already trust. A ruler.
If the ruler says 30cm and the sensor says 30cm, good. If the sensor says 34cm, you have just
learned something important about your machine — before it mattered.
Wire it up
1
Ultrasonic sensor
4
Jumper wires
1
Ruler or tape measure
Four pins, four wires. Trig sends the chirp, Echo listens for it coming back.
Push the sensor into the top of the board so all four pins are in their own columns.
VCC → 5V, with a red wire.
Trig → pin 10, with a blue wire. This is the one that sends the chirp.
Echo → pin 11, with a green wire. This is the one that hears it come back.
GND → GND, with a black wire.
⚠ Careful
Trig and Echo are easy to swap by accident, and if you do the sensor will read 0 forever. Check the tiny printed labels on the sensor itself, not the colour of the wire.
Pins used10115VGND
The code
Your program
when the board starts up
forever
print (ultrasonic distance trig (10) echo (11))
wait (0.3) seconds
📷
images/s7-mblock-distance.png
Screenshot: mBlock ultrasonic program with the serial monitor showing distances in cm
Try it — against a ruler
Lay the ruler flat, starting at the front of the sensor.
Hold a book upright at exactly 10cm. Read what the sensor says.
Do the same at 30cm, then at 60cm.
Now find its limits: bring the book slowly closer until the reading stops making sense, then walk it backwards until it gives up.
🔮 Try this
Point it at something soft — a jumper, a curtain — and then at something hard and flat. Soft things swallow the echo. A sensor that works perfectly against a book can be useless against a person in a woolly coat.
If it goes wrong
What you see
Why
What to do
Always reads 0
Trig and Echo are swapped. This is the most common fault.
Read the labels printed on the sensor board and match them to pins 10 and 11.
Reads a huge number all the time
Nothing is coming back, so it waits and gives up.
Point it at a flat wall about 30cm away. Make sure nothing is right in front of it.
Readings jump about by several cm
The surface is at an angle, so the echo bounces away sideways.
Hold the book square to the sensor, not tilted.
Works at 30cm but not at 3cm
It is too close — the echo comes back before it has finished chirping.
That is a real limit of the sensor, not a fault. Note it down and design around it.
What you learned
Some sensors do their own calculation and answer in real units.
You check a sensor by comparing it against something you already trust.
Every sensor has a range where it works and limits where it does not.
What you point it at changes the answer.
Challenge optional — only if you finish early
Measure the same distance ten times and write down all ten readings. Are they identical?
Find the closest and furthest your sensor can measure at all.