Skip to main content
Robotics 1 min read

Building Autonomous Obstacle-Avoiding Robots with Ultrasonic Sensors

Sonar echolocation calculation: time of flight, sound speed conversion, and decision branching in autonomous rovers.

NA
Naveen Kumar P
October 28, 2026
Building Autonomous Obstacle-Avoiding Robots with Ultrasonic Sensors

The Principles of Sonar Echolocation

Bats and dolphins navigate murky waters and dark caves by emitting high-frequency ultrasonic clicks and listening for reflections. The HC-SR04 ultrasonic distance sensor operates on the exact same acoustic principle.

Calculating Distance via Speed of Sound

The sensor sends a 10-microsecond trigger pulse, emitting eight 40kHz sonic burst cycles. It measures the duration (time in microseconds) until the acoustic wave bounces back to the receiver pin:

Distance (cm) = (Travel Time × 0.0343 cm/μs) / 2

We divide by 2 because the acoustic pulse travels to the obstacle and returns back.

Reactive Navigation State Machines

Students write state machine logic for their robot car:

  • If distance > 25 cm: Drive forward at 80% throttle.
  • If distance ≤ 25 cm: Stop motors, rotate servo sensor to scan left and right, evaluate which path has greater clearance, and execute a 90-degree pivot turn.
Tags: #Robotics #Ultrasonic Sensor #Autonomous #Code
NA

Naveen Kumar P

Senior STEM Mentor & Robotics Engineer at STEMix Academy Kovilpatti. Passionate about empowering school students with practical hands-on engineering, microcontrollers, and artificial intelligence.

Learn more about our team

Related Articles

View More in Robotics
STEMix Support
Hi! I'm your STEMix assistant. How can I help you today?

Please provide your details so our team can reach out to you.