Echolocation: Ultrasonic Distance Sensors (HC-SR04)
Track 6: Robotics, Physical Computing & Autonomous AI
Bats fly through pitch-black caves at 30 miles per hour without touching a single stalactite by screaming ultrasonic shouts and listening for the echo. Today, we equip our robot with bat echolocation!
Simulate HC-SR04 sonar with draggable physical target and live centimeter readout.
• Search components for 'Ultrasonic Distance Sensor (4 pin)'.
• Connect VCC to 5V (or 3V), GND to GND, TRIG to Pin 0, ECHO to Pin 1.
• Click the sensor during simulation to drag the test obstacle target.
The Big Idea: Ultrasonic Waves, Speed of Sound, Pulse Timing, and Distance Math
Explore the essential building blocks for this lesson:
Concept Watch: How Ultrasonic Sensors Work (HC-SR04 Deep Dive)
What to Look For:
The sensor features two metallic cylinders: one is a miniature speaker sending 40kHz sound bursts (Trigger), and the other is a microphone waiting for the echo (Echo).
Sound travels through air at 343 meters per second (about 0.0343 cm per microsecond). By timing how long the echo took, dividing by 2 gives exact distance!
Ultrasonic sensors work best on flat hard surfaces from 2cm to 400cm. Soft curtains absorb the sound, and sharp angles can bounce the echo away.
Guided Co-Pilot Activity
Follow the three steps in order. Stuck? Ask your mentor for a hint, not the answer.
Open the simulator and wire Trigger to Pin 0 and Echo to Pin 1.
Write the 10 microsecond trigger pulse and calculate distance using duration * 0.0343 / 2.
Slide the virtual target back and forth in the simulator to see centimeters update in real time.
Python Snippet
from microbit import * import machine # Ultrasonic Distance Calculation in Centimeters # Trigger Pin: Pin 0, Echo Pin: Pin 1 def get_distance_cm(): pin0.write_digital(0) sleep_us(2) # Send 10 microsecond ultrasonic burst pin0.write_digital(1) sleep_us(10) pin0.write_digital(0) # Measure duration echo pin stays HIGH in microseconds duration = machine.time_pulse_us(pin1, 1, 30000) if duration < 0: return 999 # Timeout / nothing in range # Distance = (time * speed of sound 0.0343 cm/us) / 2 (round trip) distance_cm = (duration * 0.0343) / 2 return round(distance_cm, 1) while True: dist = get_distance_cm() display.scroll(str(int(dist)) + "cm", delay=60) sleep(100)
[Echo Pin1] Time-of-Flight duration = 1420 microseconds
[Distance Math] (1420us * 0.0343 cm/us) / 2 = 24.4 cm
Checkpoint Challenge
Demonstrate your understanding before starting your solo quest:
Why do we divide the ultrasonic travel time by 2 when calculating obstacle distance?
Your Solo Quest
Choose your achievement level and take the wheel:
0 / 350 XP
Simulate an ultrasonic sensor and print accurate centimeter measurements to the console.
Build an automatic collision warning: display a smiley face when distance > 30cm, and a skull when < 10cm.
Add an auditory backup beeper that beeps faster the closer an obstacle gets.
Lesson Summary & Practice
Celebrate your progress and prepare for your next level:
60-Second Show and Tell
Present your screen to your mentor and explain:
• What did you build today?
• Which challenge did you solve?
Take-Home Project: Ultrasonic Tape Measure & Parking Assistant