
If you’re building anything that has to avoid running into things — a robot car, a rover, a parking-alert gadget — you hit this fork in the road almost immediately. Ultrasonic or infrared? Both cost a couple of dollars, both connect to an Arduino in about four wires, and both get marketed with the same phrase: “obstacle detection.” They are not substitutes for each other. They fail in completely different ways, and that difference is the entire decision.
The short version: reach for ultrasonic when you need to know how far away something is, and reach for infrared when you just need to know that something is there, fast and cheap. Here’s what’s actually happening inside each one, and where each will embarrass you in front of people.
They Aren’t Measuring the Same Thing at All
An ultrasonic sensor is a miniature sonar rig. The HC-SR04 — the two-eyed module bolted to the front of every beginner robot on the internet — sends out a burst of eight pulses at 40 kHz when you hold its Trig pin HIGH for 10 microseconds. That frequency is well above human hearing, which tops out around 20 kHz, so the whole thing is silent to you. The sensor then listens for the echo and holds its Echo pin HIGH for exactly as long as the round trip took. Your code times that pulse and does arithmetic: sound moves about 343 m/s at room temperature, which works out to roughly 58 microseconds per centimeter of round-trip travel. Halve it, because the sound went out and came back.
Notice what’s missing from that description: any mention of what the object looks like. Sound doesn’t care whether the wall is white, black, glossy, or matte. That’s ultrasonic’s superpower.
Infrared sensors work on reflected light instead, and two very different parts both get sold under the label “IR sensor”:
- Digital obstacle modules (the FC-51 / HW-201 style board) — an infrared LED, a photodiode, and an LM393 comparator chip. The LED throws IR light forward, the photodiode measures how much comes back, and the comparator flips a single output pin LOW once the reflection crosses a threshold you set with the onboard trimmer potentiometer. It’s a yes/no answer. No distance, ever.
- Analog distance sensors (the Sharp GP2Y0A21YK0F and its siblings) — these use triangulation. An IR beam goes out, and a position-sensitive detector inside measures where on its little strip the reflection lands. Closer object, steeper angle, different landing spot. That gets converted into a varying output voltage, which is a real distance measurement.
Ultrasonic: Color-Blind in the Best Way, Deaf to Soft Things
The HC-SR04’s spec sheet is legitimately impressive for a part that costs about two dollars: 2 cm to 400 cm of range, accuracy down to about 3 mm, running on 5 V at roughly 15 mA. Its effective beam is a cone of about 15 degrees, which is narrow enough to be directional but wide enough that you’re not aiming a laser pointer.
The failure modes are where it gets interesting. Soft and porous surfaces absorb sound instead of reflecting it. Point an HC-SR04 at a couch cushion, a heavy curtain, a wool sweater, or a cat, and you may get no echo at all — the sensor reports its timeout value and your robot cheerfully drives into the sofa. Angled surfaces are just as bad. Sound reflects off a hard flat surface like a billiard ball off a rail, so anything tilted more than roughly 15 degrees away from perpendicular bounces your ping off into the room instead of back at the receiver. A half-open door edge-on is a classic invisible obstacle.
It’s also slow, by microcontroller standards. The datasheet asks for at least 60 ms between trigger pulses so the previous ping’s echoes have died down, which caps you at about 16 readings per second. That’s plenty for a rover trundling along at walking pace and not nearly enough for anything fast. One more wrinkle: the speed of sound drifts about 0.17% per degree Celsius, so a sensor calibrated in a cold garage reads slightly long in a warm room. Under a meter that’s a rounding error; at four meters it’s real.
This post contains affiliate links. If you buy through them, this site earns a commission at no extra cost to you.
Buy these in a multipack, because you will eventually want one facing forward and one facing each side — and because a single bent pin shouldn’t stall a build. ELEGOO’s are consistent enough that swapping one for another doesn’t change your calibration, which is not true of every no-name batch. We put these through a longer test in our HC-SR04 review.
Check Price on Amazon →Infrared: Fast and Cheap, Until the Sun Comes Out
The FC-51-style digital module is the cheapest useful sensor in hobby robotics, and it is very good at exactly one job: telling you something is close. Detection range adjusts from roughly 2 cm to 30 cm with the onboard potentiometer, the detection cone is about 35 degrees, it runs anywhere from 3 V to 6 V, and the output pin goes LOW the instant it sees a reflection. Reading it is a single digitalRead() — no timing loops, no math, no library. Because there’s no 60 ms echo-settling delay, you can poll it as fast as your loop runs.
What it can’t tell you is how far. “Something is within the threshold you dialed in” is the whole output. And that threshold has to be re-tuned whenever your lighting or your target surface changes, which is a real chore on a robot that moves between rooms.
At roughly a dollar apiece these are effectively disposable, which is the right mindset — you want several, ringing the chassis, not one precious sensor. HiLetgo’s boards have the LM393 and the adjustment pot laid out sensibly with a status LED that lights on detection, so you can tune the threshold by eye without writing a line of code first.
Check Price on Amazon →If you want actual distance out of infrared, you need the analog kind. The Sharp GP2Y0A21YK0F covers 10 cm to 80 cm and outputs a voltage you read on an analog pin, updating about 26 times a second — noticeably quicker than ultrasonic’s 16. The catch is that the output is nonlinear: voltage doesn’t rise smoothly with distance, it follows roughly a constant of 27 V·cm divided by the output voltage. So you don’t do a simple map() call. You either implement that division or build a lookup table from measurements you take yourself. The other catch is the near end — below about 10 cm the voltage curve doubles back on itself, so a very close object reads the same as a moderately far one. If something can get that close, pair it with a sensor that handles short range.
The one to use when ultrasonic keeps missing your soft or angled targets but you still need a real number. Draws about 30 mA average and wants 4.5—5.5 V, so give it a clean supply — these are noisy little things and a shared, sagging rail shows up as jitter in your readings. Averaging five samples in code cleans up most of the rest.
Check Price on Amazon →The Two Things That Kill Infrared
Sunlight. The sun is a floodlight of infrared. Take an IR sensor outdoors on a bright day and the receiver saturates — it’s already seeing more IR than any reflection could add, so it goes effectively blind and reports nonsense. Indoor LED and fluorescent lighting almost never causes trouble, but if your robot is going out on a patio, IR needs shielding, a shade hood, or replacing.
Dark surfaces. Black objects absorb infrared the same way they absorb visible light. Expect a 30—50% cut in maximum range against dark targets, which means the black chair leg you calibrated for at 25 cm may not register until 12 cm. Glossy black plastic is worse still, because it reflects specularly — the light bounces away at an angle instead of scattering back.
Ultrasonic is immune to both of these. Infrared is immune to soft fabric and angled surfaces. They are, almost perfectly, each other’s blind-spot coverage.
One Wiring Warning Before You Buy
Both the standard HC-SR04 and the Sharp analog sensor are 5 V parts, and the HC-SR04’s Echo pin sends out a full 5 V signal. On an Arduino Uno that’s fine. On an ESP32 or a Raspberry Pi Pico, whose pins are rated for 3.3 V, feeding 5 V straight into an input is how you damage a board. A two-resistor divider on the Echo line solves it for pennies — we cover the whole issue in 5V vs. 3.3V logic levels. There are also HC-SR04P variants that run on 3 V to 5.5 V natively if you’d rather buy the problem away.
What Good Robots Actually Do: Use Both
Once you’ve seen the failure modes side by side, the layout suggests itself. Put the ultrasonic sensor forward-facing as the primary “how far to the wall” input, since it handles the long range and doesn’t care about color. Ring the chassis with a few cheap digital IR modules as close-range bumpers — they catch the soft and angled things ultrasonic misses, and they respond instantly, which matters when the obstacle is already 8 cm away. Point another IR module down at the floor and you’ve got a cliff detector that keeps the robot off the stairs, the same reflectance principle behind our line-following robot build.
That entire arrangement costs under fifteen dollars and is dramatically more reliable than any single sensor at any price. Redundancy across different physical principles is the actual lesson here — two sensors that fail at the same things aren’t redundancy, they’re just two sensors.
Quick Verdict
- Need a distance number, indoors or out, on hard surfaces? Ultrasonic. Start here if you’re only buying one.
- Need a fast yes/no bumper for a few cents? Digital IR module. Buy a ten-pack.
- Need distance where ultrasonic keeps missing soft or angled targets? Sharp analog IR, with the nonlinear math handled in code.
- Working outdoors in sunlight? Ultrasonic, and don’t fight it.
- Detecting black or dark objects? Ultrasonic again — IR range collapses.
- Building something you want to actually work? One ultrasonic in front, three or four IR modules around the edges.