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.
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:
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.
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.
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.
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.
Leave A Comment