Robot Sensors Explained: Ultrasonic, Infrared, Encoders, IMUs and Cameras
ContentsUltrasonic: How far away is a nearby surface?
Start by writing the question the robot needs answered. “Is there something in front?” calls for a different sensor from “How much did the wheel turn?” No sensor measures everything, and even a plausible reading can be wrong. Here are five common inputs and a simple bench check for each. Keep the motors off while you explore the readings.
Ultrasonic: How far away is a nearby surface?
An ultrasonic module sends a sound pulse and listens for a returning echo. The echo's round-trip time is used to estimate distance. A target at an angle can reflect the pulse away from the receiver, and the module has a specified usable range. A missing echo is not proof that the path is clear. Read the Adafruit ultrasonic module guide for a concrete timing and wiring example; check your own module's signal voltage before connecting it.
Bench check: Hold a flat card at several known distances, then angle it. Record what your program returns when it gets no echo. See the ultrasonic obstacle avoidance build for the next step.
Infrared reflectance: Is the floor light or dark?
A reflectance sensor shines infrared light and measures how much comes back. A line follower uses the difference between its line and background; that is not the same as measuring distance. Output depends on mounting height and surface properties. Pololu's reflectance sensor guide describes the effects of sensor distance and motor noise.
Bench check: Hold the sensor at its intended height and read it over the line and background under the lighting you will use. If the readings overlap, adjust the mounting or material before picking a threshold. The line-following robot build puts this into context.
Encoder: How much did the shaft turn?
An incremental encoder produces signal changes as a shaft turns. A two-channel quadrature encoder also lets software infer direction from the order of the changes. Check whether the manufacturer specifies counts per motor-shaft or output-shaft revolution and how your code counts edges. The Pololu gearmotor encoder documentation illustrates this distinction. Wheel rotation can estimate travel, but a spinning wheel on a slippery floor is not evidence the robot advanced.
Bench check: With motor power off and encoder power on, mark the wheel, turn it one revolution by hand if the mechanism permits it, then turn it back. Look for repeatable counts and opposite directions. Don't force a gearbox that cannot be back-driven.
IMU: Is the robot turning, tilting or accelerating?
A typical six-axis inertial measurement unit combines an accelerometer and gyroscope. The gyro reports angular rate; the accelerometer responds to gravity as well as motion. The Bosch BMI270 documentation is one example of these two sensing functions in the same part. Integrating a gyro to get heading can accumulate error; an IMU is not a position sensor.
Bench check: Log a still sample, then tilt the board and rotate it slowly around one labelled axis. Check which axis changes and whether the sign matches the mounting diagram.
Camera: What is in view?
A standard camera gives a grid of pixels, not a distance or object label. Software has to extract the feature you care about. Lighting, blur and lens distortion can complicate that step; OpenCV's camera calibration tutorial explains distortion and how calibration addresses it.
Bench check: Preview the image under the lighting the robot will actually use. Move the target near the frame edge and through a shadow before relying on its appearance. For detecting a line under a small robot, a reflectance sensor is usually a simpler starting point than building a vision pipeline.
Pick one sensor and define a failure response
Write down what a useful reading looks like, how you know it is recent, and what the robot should do when it is missing or implausible. For a first obstacle demo, choose a low-speed test in a clear area and make an invalid reading stop the robot rather than telling it to drive on. Save more complex sensor combinations until one input is behaving predictably.


