STEMulus STEMulus Kids Tech
Track Track 6: Robotics, Physical Computing & Autonomous AI • Young Roboticists (Ages 8-11) • Lesson 8
Portal
Lesson 8 Young Roboticists (Ages 8-11)

Echolocation: Ultrasonic Distance Sensors (HC-SR04)

Track 6: Robotics, Physical Computing & Autonomous AI

The Mission Hook

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!

Mission Launch
Predict
Predict & Hypothesize: In physical computing and robotics for Echolocation: Ultrasonic Distance Sensors: How does the microcontroller translate digital code for Ultrasonic Waves, Speed of Sound, Pulse Timing, and Distance Math into precise physical action without jittering or stalling?
Mission Toolkit: Required Course Software & Web Platforms
Interactive Rangefinder Sandbox Free Student Sandbox
Tinkercad Ultrasonic Sonar Tinkercad Ultrasonic Sonar

Simulate HC-SR04 sonar with draggable physical target and live centimeter readout.

Setup Checklist:
• 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.
Launch & Configure Tinkercad Ultrasonic Sonar
Use Arrow Keys or Space to advance • Press 'T' or 'S' for Tutor Cockpit

Concept The Big Idea: Ultrasonic Waves, Speed of Sound, Pulse Timing, and Distance Math

Explore the essential building blocks for this lesson:

Video Concept Watch: How Ultrasonic Sensors Work (HC-SR04 Deep Dive)
2:20 Micro-Video Watch on YouTube
Video not loading? Open it directly: Watch on YouTube ↗
Key What to Look For:
• A 10-microsecond trigger pulse initiates eight 40kHz sonic burst cycles.
• Echo pin output duration directly corresponds to time-of-flight through the air.
• Target material density, angle of incidence, and ambient temperature affect accuracy.
The HC-SR04 Sensor The HC-SR04 Sensor

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).

Speed of Sound Math Speed of Sound Math

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!

Blind Spots & Echo Deflections Blind Spots & Echo Deflections

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.

Build Guided Co-Pilot Activity

Follow the three steps in order. Stuck? Ask your mentor for a hint, not the answer.

1
Wire & Inspect the Virtual Circuit

Open the simulator and wire Trigger to Pin 0 and Echo to Pin 1.

Action: Open the hardware simulator, check pin wiring, power rails, and common ground
2
Flash the Microcontroller Code

Write the 10 microsecond trigger pulse and calculate distance using duration * 0.0343 / 2.

Action: Type the MicroPython control logic, sensor thresholds, and safety limits
3
Simulate, Measure & Tune

Slide the virtual target back and forth in the simulator to see centimeters update in real time.

Action: Run the simulation, adjust virtual sensor sliders, and verify actuator output
Code 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)
Robotics Hardware & Telemetry Console
BBC micro:bit v2 / ESP32 Simulator
>>> flash --target microbit_v2 main.py
[HC-SR04 Sonar] Trigger Pin0: 10us ultrasonic burst @ 40kHz
[Echo Pin1] Time-of-Flight duration = 1420 microseconds
[Distance Math] (1420us * 0.0343 cm/us) / 2 = 24.4 cm
Hardware Simulation Verified (Exit 0) Bus: 3.3V Logic / Common GND

Checkpoint Checkpoint Challenge

Demonstrate your understanding before starting your solo quest:

Question Why do we divide the ultrasonic travel time by 2 when calculating obstacle distance?

Solo Quest Your Solo Quest

Choose your achievement level and take the wheel:

Interactive Quest Checklist (Click to Claim):
XP 0 / 350 XP
Bronze
Bronze • +50 XP
First Step

Simulate an ultrasonic sensor and print accurate centimeter measurements to the console.

Silver
Silver • +100 XP
Skill Practice

Build an automatic collision warning: display a smiley face when distance > 30cm, and a skull when < 10cm.

Gold
Gold • +200 XP
Creative Challenge

Add an auditory backup beeper that beeps faster the closer an obstacle gets.

Wrap Up Lesson Summary & Practice

Celebrate your progress and prepare for your next level:

Show and Tell 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 Take-Home Project: Ultrasonic Tape Measure & Parking Assistant
25 Mins • Independent Practice
The Challenge: Build a parking distance assistant: 3 green LEDs for far, 2 yellow for medium, and 1 flashing red with buzzer when within 5cm!
1
Build: Create 3 distance zones using if-elif-else logic.
2
Self-Test: Trigger the buzzer when distance drops below 10cm.
3
Turn In: Upload your distance zone table and code to Google Classroom.
Lesson Completed!