Sensors Every Beginner Robot Needs: Ultrasonic, IR, and Line Sensors Explained

ELEGOO HC-SR04 ultrasonic distance sensor five-pack

A robot without sensors is a motorized toy. It can move, but it has no idea what the world around it looks like, so it either follows a pre-programmed path or drives into a wall with total confidence. Sensors are what turn “moves” into “reacts” — and for beginner robots, three types do almost all of the work: ultrasonic distance sensors, IR obstacle sensors, and line sensors. Here is what each one actually does, how it does it, and which specific modules to buy.

Ultrasonic Sensors: Actual Distance, Measured in Sound

The HC-SR04 is the ultrasonic sensor for hobby robotics, and it works like a bat. Pull its trigger pin high for 10 microseconds and it fires a burst of eight sound pulses at 40 kHz — roughly double the highest pitch a human can hear, so your robot pings away in silence. The sound bounces off whatever is in front of it and comes back, and the sensor raises its echo pin for exactly as long as the round trip took. Your code times that pulse, then does one line of math: sound travels about 343 meters per second at room temperature, so distance in centimeters is the duration in microseconds times 0.0343, divided by two because the sound went out and back.

That gives you a real number, not just a yes/no — anywhere from about 2 cm out to 400 cm, with accuracy around 3 mm under good conditions. The catch is that the detection cone is narrow (under 15 degrees) and soft or angled surfaces can swallow or deflect the ping. If your readings ever go glitchy, we wrote up the usual causes in Why Your Ultrasonic Sensor Is Giving Bad Readings.

This post contains affiliate links. If you buy through them, this site earns a commission at no extra cost to you.

Buy these as a multi-pack. Individual HC-SR04s cost nearly as much as five, and having spares matters more with this sensor than most — a dropped robot tends to land on it. The ELEGOO five-pack is the one we tested and reviewed in depth in our HC-SR04 review, and it has been consistent unit to unit.

ELEGOO HC-SR04 ultrasonic sensor 5-pack
Distance Sensing
ELEGOO HC-SR04 Ultrasonic Module (5-Pack)

The standard robot rangefinder: real distance readings from 2 cm to 4 m, five units so a crash never ends the project.

Check Price on Amazon →
Try This:Build a parking sensor before you build a robot. Wire a passive buzzer to pin 8, read the HC-SR04 in a loop, and use tone() with a delay proportional to the distance — the closer your hand gets, the faster it beeps. It is the exact feedback loop your future obstacle-avoiding robot will run, minus the wheels.

IR Obstacle Sensors: Cheap Yes/No Detection

An IR obstacle module works on reflection too, but with infrared light instead of sound, and it answers a much simpler question: is something there or not? An IR LED shines forward; if an object is close enough, some of that light bounces back into a phototransistor next to it. An LM393 comparator on the board — a small chip that compares two voltages and snaps its output fully high or fully low depending on which is bigger — turns that faint analog reflection into a clean digital signal your board can read. A small potentiometer on the module sets the threshold, which in practice adjusts the trigger distance anywhere from about 2 cm to 30 cm.

Two things trip up beginners. First, the output is active low: the pin reads LOW when an obstacle is detected, not HIGH, so if your code seems backwards, it probably is. Second, this is not a distance sensor — it cannot tell you 12 cm versus 20 cm, only “within range” or not, and dark matte surfaces absorb IR well enough to sneak past it. We compared the two approaches head to head in Ultrasonic vs. Infrared Sensors for Obstacle Detection. The reason to use IR anyway: the modules cost about a dollar each, respond instantly, and you can ring a whole chassis with them.

HiLetgo IR infrared obstacle avoidance sensor module 10-pack
Obstacle Detection
HiLetgo IR Obstacle Avoidance Module (10-Pack)

Ten adjustable-range IR detectors for the price of lunch — enough to cover a robot’s blind spots and still have spares for the parts bin.

Check Price on Amazon →

Line Sensors: The Same Trick, Pointed at the Floor

A line sensor is an IR reflectance sensor aimed straight down. The classic part is the TCRT5000: an IR emitter running at 950 nm and a phototransistor side by side in one package, with a daylight-blocking filter so room lighting does not drown the signal. White floor reflects lots of IR back; black tape reflects very little. Read the difference and your robot knows whether it is on the line or off it. One detail worth knowing: the sensor measures reflectivity, not color — glossy black tape can bounce back more IR than matte white paper, which is why every line follower needs a calibration pass on its actual track.

The TCRT5000 works at very short range — roughly 0.2 mm to 15 mm — so it has to be mounted close to the floor, and most breakout modules give you both a comparator-cleaned digital output and the raw analog signal. Use the analog pin when you can: knowing how much of the sensor is over the line is what makes a line follower smooth instead of twitchy. That entire build, wheels included, is covered step by step in How to Build a Simple Line-Following Robot.

HiLetgo TCRT5000 infrared reflective line sensor module 10-pack
Line Following
HiLetgo TCRT5000 Reflective Sensor Module (10-Pack)

The standard line-following sensor with both digital and analog outputs. A line-follower array uses two to five of these, so a ten-pack is the right quantity.

Check Price on Amazon →

How They Fit Together on One Robot

These three are not competing options — they are a division of labor. A typical beginner robot puts the ultrasonic sensor up front for real distance readings, an IR module on each front corner to catch obstacles outside that narrow 15-degree cone, and a row of line sensors under the chassis if it needs to follow a track. All of them run happily on an Arduino’s 5V pins; if you are building around an ESP32 or another 3.3V board instead, read up on 5V vs. 3.3V logic levels before wiring the echo pin straight in.

Start with the ultrasonic sensor — it teaches the most per dollar, and a rangefinder pointed at your hand is the fastest way to feel the moment a pile of parts becomes something that can perceive. The other two packs can arrive in the same box. Between them, you have the sensory toolkit for nearly every robot on this site.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top