Robil
Meta to cut 10,000 jobs in second...
Elevating Your Office Attire...
Beauty and Wellness Secrets...
31°C, New York
Robil

Weekly Updates

Let's join our newsletter!

Do not worry we don't spam!

Robil

Using Ultrasonic Sensors for Obstacle Detection

Why sound is a sensible first sensor

If you have built a small wheeled robot and watched it charge cheerfully into the nearest skirting board, you already know the problem: motors are easy, knowing where you are is hard. Before you reach for cameras or laser scanners, an ultrasonic module is the cheapest, friendliest way to give your robot a sense of the space around it. It works in the dark, it does not care about the colour of your wallpaper, and it costs less than a decent sandwich.

The principle is beautifully simple. The module fires a short burst of high-frequency sound — well above what you or the cat can hear — and waits for the echo. Because sound travels at a known speed, the delay tells you how far away the object is. That is it. No lenses to focus, no libraries of trained images, just a stopwatch and some arithmetic.

How the module turns a ping into a number

The common four-pin module you will find in most UK hobby shops has a transmitter, a receiver, and a small amount of supporting circuitry. You drive the trigger pin high for about ten microseconds, the board sends out a chirp at roughly 40 kHz, and the echo pin goes high for exactly as long as it takes the sound to travel out and back.

The maths is worth committing to memory. Sound travels at about 343 metres per second at 20°C, which works out at roughly 29 microseconds per centimetre in one direction. Because the pulse makes a round trip, divide the measured time in microseconds by 58 to get centimetres, or by 148 to get inches. Most microcontroller libraries hide this, but knowing it helps enormously when the readings look odd.

A few practical numbers to keep in mind:

  • Reliable range: about 2 cm to 2 m in a typical home. The quoted 4 m figure assumes a large, flat, hard target.
  • Beam angle: roughly a 15° cone, widening with distance. It is not a laser pointer.
  • Update rate: at least 50 ms between pings, or you may hear the previous echo and get a false short reading.
  • Temperature drift: the speed of sound changes by about 0.6 m/s for every degree Celsius, so a cold garage in February reads slightly differently to a warm kitchen.

Getting your first reading right

Wire the module to your board, power it from 5 V if it expects 5 V, and give the echo pin a voltage divider if your microcontroller runs at 3.3 V logic. Then write the simplest possible loop: trigger, measure the pulse, convert to centimetres, print it. Wave your hand in front of the sensor and watch the numbers move. Once you have that working, you have a robot that can feel.

Two habits will save you hours. First, always use a timeout when measuring the pulse — around 25 milliseconds — so a missing echo does not freeze your whole program. Second, take the median of three readings rather than trusting a single one. A single stray ping from a nearby hard surface can be wildly wrong, but the middle value of three is usually honest.

What fools an ultrasonic sensor in a British home

Ultrasonic distance sensing is not magic, and domestic interiors are full of things designed to absorb sound. Knowing the failure modes is what separates a robot that works on the kitchen floor from one that works everywhere.

  • Soft furnishings. Curtains, sofas, duvets and thick carpets swallow the ping. Your robot may drive straight through a curtain because the sensor genuinely cannot see it.
  • Angled surfaces. A ping hitting a wall at a steep angle bounces away rather than back. Flat obstacles facing you are easy; slanted ones are invisible.
  • Narrow or thin objects. Chair legs and table legs are often too thin to return a strong echo at range.
  • Multiple sensors. Two modules pinging at once will hear each other. Fire them alternately, never simultaneously.
  • Electrical noise. Brushed motors spray interference. Keep sensor wiring away from motor cables and add a capacitor across the motor terminals.

Mount the module at a sensible height — around 15 to 20 cm from the floor is a good starting point — and tilt it very slightly upward. Pointing it at the floor will simply measure the carpet, which is not useful information.

Turning distance into behaviour

Raw numbers are only interesting once they change what your robot does. A simple and surprisingly effective approach is to divide the space in front of you into three zones: stop, slow and clear. Below about 15 cm, stop and turn. Between 15 and 40 cm, slow down and prepare to turn. Beyond 40 cm, carry on.

Add hysteresis, or your robot will dither at the boundary. Rather than switching at exactly 15 cm, stop at 15 cm but only resume when the path clears past 25 cm. This single trick makes movement look deliberate rather than twitchy.

If you want more than one direction of vision, mount the module on a small servo and sweep it left to right, pausing briefly at each step to take a reading. Ten or twelve positions across 180° gives you a crude map of the room and enough information to choose the most open direction. Pause the motors while you ping, though — their vibration shows up in the readings.

Finally, treat ultrasonics as one sense among several. A pair of cheap microswitches on the front bumper costs pennies and catches everything the sound misses. Between them, you have a robot that can navigate a hallway without bruising your ankles.

author
Emily Clarke

Robil shares practical, down-to-earth guidance on home robotics and automation for uk beginners for readers across the UK.

Leave A Comment
Popular Guides