Sensors: Seeing the World
Robotics Fundamentals
Course 1 · Chapter 5 · Sensors: Seeing the World
Robotics Fundamentals 1 described the loop at the heart of every robot: sense, think, act. Robotics Fundamentals 3 and 4 were the “act” half, motors, wheels and the geometry that turns wheel speeds into motion, and they ended with a robot that keeps a running guess of where it is (odometry) but has no idea what is around it. This chapter is the “sense” half. A sensor is the robot's only window on the world, and it is a narrow and smeared one: every sensor measures one thing, over a limited range, with some noise, some delay and some bias. A robot designer's skill is knowing what each sensor really tells you, what it cannot tell you, and how to clean up what it says. We follow a measurement from the physics to the number in the program, look at the numbers that describe a sensor (its specifications), meet the common robot sensors (distance, line, bump, light, current, camera), and then cover the three things that make sensor data usable: filtering noise, calibration, and working out how large the remaining error is.
Ranges, beam angles, resolutions, response times and prices in this chapter are typical or approximate values from memory, for the kind of low-cost parts used on small robots; they vary a lot between models, so always read the datasheet of the part you actually buy. The formulas (time of flight, similar triangles, the statistics of averaging, error propagation) are standard and exact for the idealised models described.
1. From physical quantity to number
A computer can only work with numbers, and the world is made of light, sound, force and heat. A sensor is a chain of conversions, and knowing the chain tells you where each error can creep in. The first link is the transducer, a part whose electrical behaviour changes with the physical quantity: a light-dependent resistor's resistance falls as light rises, a microphone-like ultrasonic element produces a voltage when sound arrives, a photodiode passes a current in proportion to light, a switch opens or closes. The rest of the chain turns that change into a clean voltage and then into a number.
The electronics of the middle links is the subject of Basic Electronics 3 (voltage dividers, which turn a resistance change into a voltage), Basic Electronics 7 (signal conditioning: amplifying a small signal and removing high-frequency noise) and Basic Electronics 10 (analogue-to-digital conversion). Here we only need the arithmetic of the last step. An n-bit analogue-to-digital converter (ADC) with reference voltage Vref turns the input into a whole number, and its smallest step is the least significant bit (LSB).
2. Sensor specifications
A datasheet describes a sensor with a handful of terms. Learning to read them is the quickest way to avoid buying the wrong part.
| Specification | Meaning | Example (typical, approximate) |
|---|---|---|
| Range | Smallest and largest value it can measure. Many sensors also have a minimum range, below which they fail | Ultrasonic 2 cm to 4 m |
| Resolution | The smallest change in the quantity that changes the output. Set by the ADC or by the sensor's own steps. Not the same as accuracy | 0.3 cm for an ultrasonic module; 4.88 mV for a 10-bit ADC at 5 V |
| Accuracy | How close a reading is to the true value, including any consistent offset (bias) and the random noise | ±3 mm plus 1 % of the reading |
| Precision (repeatability) | How tightly repeated readings of the same true value cluster: the random spread. A precise sensor can still be consistently wrong | Standard deviation 0.15 cm |
| Noise | Random variation in the output that does not come from the quantity. Quoted as a standard deviation (σ) or a peak-to-peak value | σ = 0.5 cm at 1 m |
| Response time | How long the output takes to follow a change: the time to reach 63 % (one time constant) or 90 % of a step | An LDR may take tens of milliseconds to respond (typical); a photodiode microseconds |
| Sample (update) rate | How many new readings per second the sensor can supply | About 15 to 25 Hz for an ultrasonic sensor; 10 Hz for a typical 2D lidar rotation |
| Field of view (beam angle) | The cone or width the sensor sees. A wide beam gives coverage but poor direction; a narrow beam the reverse | Ultrasonic about 15 degrees each side of the axis; infrared a few degrees |
| Linearity and sensitivity | Whether the output is proportional to the input, and how much output you get per unit of input | Infrared triangulation is strongly nonlinear (section 3) |
Accuracy versus precision
The two words are often swapped, but they describe two different faults. Bias (a systematic error) makes a sensor inaccurate; noise (a random error) makes it imprecise. They need different cures: calibration removes bias, averaging reduces noise (sections 9 and 10). Here are ten readings from each of two imaginary sensors measuring a true value of 100.0.
Sample rate and the robot's speed
A robot moves between readings. The distance it travels between two readings is v / f (speed over update rate). At 0.5 m/s with a 15 Hz sensor, that is 0.5 / 15 = 3.3 cm. Add the delay of any filter (section 9) and the time the program takes, and the robot may be 10 cm closer to a wall than its last number says. When you choose a sensor, ask whether it updates fast enough for the speed you want, and whether the robot can stop within the distance at which it first sees an obstacle (stopping distances come from the motor and wheel figures of Robotics Fundamentals 3, and Robotics Fundamentals 7 deals with the control loop's own delay).
3. Distance sensors
Ultrasonic: time of flight of sound
An ultrasonic sensor sends a short burst of sound at about 40 kHz (too high to hear), waits for the echo, and measures the time. Sound travels out to the object and back, so the distance is half of the speed times the time. The speed of sound in air depends on temperature T (in degrees Celsius), by a good approximation:
The wide beam is both the strength and the weakness of the ultrasonic sensor. It is a good “is something in front?” detector, since a thin pole will still reflect some sound, but it cannot tell you where across the beam the object is. Know the usual troubles before you trust one:
- Soft and angled surfaces. Fabric and foam absorb the sound; a smooth flat wall at a steep angle (more than about 20 or 30 degrees from square-on, approximate) reflects the sound away like a mirror, and the sensor sees nothing, or a longer echo that went off a second wall.
- Crosstalk. If several sensors fire at once, each hears the others' echoes. Fire them in turn.
- Minimum range. The same element transmits and listens, so for a short time after the burst it cannot hear: a blind zone of a few centimetres.
- Temperature. Section 11 shows how much error a wrong assumed temperature makes, and how to remove it.
Infrared triangulation
An infrared (IR) distance sensor of the triangulation type has an infrared emitter and a position-sensitive detector side by side, behind a lens, a fixed distance b (the baseline) apart. The emitter's narrow beam hits the object; the reflected light comes back to the lens at an angle that depends on the distance, and lands on a spot on the detector that moves as the object moves. Similar triangles turn the spot's position x into the distance.
The output voltage therefore follows roughly V ≈ k / d (typical sensors: a curve like 12 / d with d in centimetres and V in volts, rising to a peak at a minimum distance and then falling again closer in, so readings below the minimum range are wrong, not just inaccurate). With a 10-bit ADC at 5 V this nonlinearity decides the resolution:
Triangulation sensors have a narrow beam (they can find a thin pole or a table edge) and a faster update than ultrasound (tens of milliseconds, typical), and they fail in direct sunlight and on very dark, shiny or transparent surfaces. A simpler relative, the IR reflective sensor (an emitter and a phototransistor pointing the same way), measures how much light bounces back, which depends on the surface as much as on the distance; use it to detect the presence of something (a line, a table edge) rather than to measure a distance.
Laser time of flight
Light travels about 3 × 108 m/s, a million times faster than sound, so timing a pulse of light to centimetre accuracy needs a stopwatch of picosecond resolution: a distance of 1 cm is a round trip of 2 × 0.01 / (3 × 108) = 66.7 ps. Small laser sensors do this with specialised detectors that build up a histogram of arrival times from many pulses, or with the phase-shift method: modulate the light at frequency f and measure the phase delay φ of the returning light.
Typical small laser time-of-flight modules reach about 2 m indoors (approximate, less in sunlight and on dark targets) with millimetre-level resolution, a narrow beam (a few degrees to a few tens of degrees depending on the model) and a digital interface. Glass and mirrors confuse them, as they do the infrared sensors.
Lidar
Lidar (light detection and ranging) is a laser distance sensor on a rotating or scanning mount, so it measures distance in many directions. A common low-cost 2D unit spins a single ranging element and reports a ring of readings, each an angle θ and a range r, about 5 to 10 times a second. The points convert to a map in the robot's frame by the usual polar-to-Cartesian formulas, and the spacing between neighbouring points grows with range.
A 2D lidar sees a single horizontal slice, so it misses a table top above it and a step below it; 3D lidar and depth cameras add the missing dimension at higher cost and data rate. Lidar is the sensor of choice for mapping a room (Robotics Fundamentals 8 returns to mapping and localisation).
| Distance sensor | Range (typical) | Beam | Resolution | Strengths | Weaknesses |
|---|---|---|---|---|---|
| Ultrasonic | 2 cm to 4 m | Wide, about 15 degrees each side | About 0.3 cm | Cheap; works on most surfaces including glass; unaffected by light | Wide beam; soft and angled surfaces missed; slow; depends on temperature |
| IR triangulation | About 10 cm to 80 cm (varies by model) | Narrow | Fine close, coarse far (1 over d) | Cheap; narrow beam; fast | Nonlinear; sunlight and surface colour matter; short range |
| IR reflective | Under 5 cm | Narrow | Not for distance | Very cheap; ideal for lines and edges | Depends on surface reflectance |
| Laser time of flight (single point) | About 3 cm to 2 m (small modules) | Narrow | About 1 mm | Accurate; compact; digital output | Sunlight and dark or transparent targets; costs more |
| 2D lidar | About 0.15 m to 12 m (low-cost) | 360 degree scan, narrow per point | Millimetres to centimetres, points spaced by r dtheta | Maps a whole plane; long range | Costly; single slice; moving parts; data rate |
4. Line sensors
A line-following robot needs to know where a dark line (typically black tape on a pale floor, about 19 mm wide, approximate) lies under it. The standard device is a reflectance array: a row of IR reflective sensors (an emitter and a phototransistor each), a few millimetres above the floor, spaced across the robot's width. A pale floor reflects a lot of light (a low or a high reading depending on the circuit), and a black line absorbs it. Each sensor yields an analogue value; the program turns the whole row into a single number: the position of the line.
Threshold and normalisation
Raw values differ between sensors (their gains and mounting heights differ) and between floors, so first calibrate each sensor: sweep the array over both the line and the floor and record each sensor's lowest value loi (white) and highest hii (black). Then rescale every reading to a common 0 to 1 scale, wi = (rawi − loi) / (hii − loi) (limited to 0 and 1). A simple “line or not” decision compares wi with a threshold, usually 0.5, halfway between the floor and the line.
The weighted average line position
Let sensor i sit at position xi across the bar (zero at the centre, positive to the right). If you only use the on or off decisions, the position can only take values at the sensors, a resolution equal to the spacing. Using the full analogue weights gives a much finer, continuous answer, the weighted average (a centre of mass of the dark pixels):
The robot steers to drive that position to zero. With the sign convention of Robotics Fundamentals 4, where counter-clockwise turning is positive, a line to the right (positive x) needs a clockwise turn. How hard to steer for a given error is the job of the control loop in Robotics Fundamentals 7, and Robotics Fundamentals 8 builds the full line-following behaviour. The array is best mounted ahead of the wheels: the further ahead it sits, the earlier it sees the line curve away, although a long lever arm also makes a hunting (oscillating) robot swing more.
Here is the calculation as short, hardware-neutral Python-style pseudocode. The sensor reading and the steering are left to Programming for Robotics.
5. Bump and limit switches
The simplest sensor is a switch. A bump switch (a micro-switch with a lever, or a whisker) tells a robot that it has touched something; a limit switch tells a mechanism that it has reached the end of its travel (an arm joint, a gantry axis, a gripper closed). Both are digital and cheap, and both are the last line of defence, which is why they are worth fitting even on a robot full of cleverer sensors.
- Wiring. A switch needs a pull-up (or pull-down) resistor so the input has a defined level when the switch is open (a pull-up is a resistor from the input to the supply). The common arrangement is a pull-up with the switch to ground: open reads high, pressed reads low.
- Fail-safe wiring. A switch that is normally closed (pressed means open) makes a broken wire look the same as a pressed switch, so a fault stops the robot rather than leaving it blind. For safety-related limits, this is the better choice.
- Bounce. The metal contacts rattle for a few milliseconds when they close (typically 1 to 10 ms, approximate), making a burst of on and off readings. Debounce in hardware (a small capacitor) or in software: accept a change only if the new level has been stable for about 10 to 20 ms.
- Placement. A bump switch only reports after contact, so it tells you about an obstacle after the robot has hit it; at speed it is a collision detector, not an avoidance sensor. A limit switch should be set so that the mechanism stops before it hits its hard stop, with some travel to spare for the time it takes to brake.
6. Light sensors
Light sensors turn brightness into a voltage. A light-dependent resistor (LDR) is cheap and slow, with a response of tens of milliseconds and a strongly nonlinear resistance; a photodiode or phototransistor is fast and more linear, giving a current in proportion to the light. Section 1 worked an LDR in a divider: the output is higher in bright light (when the LDR is on the top of the divider), and the divider turns resistance into a voltage that the ADC reads.
- Uses. Detecting day or night, seeking a light source (two sensors pointing slightly apart; steer toward the brighter), reading a surface's brightness (the line and edge sensors above), and as the receiver in infrared beam-break and remote-control links.
- Ambient light. Room lights, sunlight and shadows change the reading. Sensors that detect the robot's own infrared modulated at a known frequency (and ignore everything else) are much more robust; a simple software version takes one reading with the emitter on and one with it off, and uses the difference.
- Colour. A colour sensor is a small group of photodiodes behind red, green and blue filters, read as three numbers; the same lighting caveats apply, so calibrate against white and black.
7. Current sensing, briefly
A robot can feel its own motors by measuring the current they draw. A motor under load draws more current (Robotics Fundamentals 3), and a stalled motor draws the maximum, so the current tells you about load, stall, a jammed gripper and the state of the battery. The usual sensor is a shunt resistor: a small resistor, a fraction of an ohm, in series with the motor. The current I causes a voltage V = I R across it, which is tiny, so an amplifier (Basic Electronics 7) raises it to a range the ADC can read.
8. Cameras, briefly
A camera is a sensor with hundreds of thousands of “pixels” instead of one, so it can see what the other sensors cannot: colour, shape, texture, text, the difference between a ball and a wall, and many objects at once. Cameras are now cheap, and are why vision-based robots can do things that were impossible with distance sensors alone. But they are hard to use, for four reasons.
- Data volume. A 640 by 480 colour image at 30 frames a second is 640 × 480 × 3 bytes × 30 = 27,648,000 bytes a second (about 27.6 MB/s) before any processing. A small controller cannot cope; it needs a more powerful processor, or a camera module that does some of the work itself.
- Depth is lost. A single camera records a flat projection; a distant large object and a near small one can look the same. If the size of an object is known, distance follows from similar triangles, d = f W / w (focal length f in pixels, real width W, width in the image w in pixels). For f = 600 pixels and a 0.10 m wide object seen 60 pixels wide, d = 600 × 0.10 / 60 = 1.00 m; but a one-pixel error in w (59 or 61 pixels) gives 1.017 m or 0.984 m, so the error is about 1.7 % per pixel and grows as the object shrinks. Two cameras (stereo) or a depth sensor measure depth directly.
- Lighting. The same object looks different in sunlight, shade and lamp light; algorithms that work in the lab can fail outdoors.
- Interpretation. A distance sensor gives you a distance. A camera gives you an array of numbers, and turning it into “there is a red ball two metres to the left” takes software (computer vision), which is outside the scope of this course.
9. Noise and filtering
Real readings jump about. Part of it is noise: small random variation, often well described by a bell-shaped (Gaussian) spread with a standard deviation σ. Part is outliers: occasional wild values (an ultrasonic echo off the wrong wall, a missed pulse reading zero, a spike from a motor). Filters reduce both, and different filters suit each kind. Every filter makes the same trade: less noise, but more delay.
Averaging many independent readings
If you take N readings with independent random errors each of standard deviation σ, their mean has a smaller spread, the standard error:
Filters for a stream of readings
| Filter | Formula | Good for | Cost |
|---|---|---|---|
| Moving average of N | Mean of the last N readings | Smoothing random noise; simple | Delay of (N − 1) / 2 samples; smears an outlier over N readings; stores N values |
| Median of N (N odd) | The middle value of the last N sorted | Removing outliers and spikes, while keeping sharp edges | Less smoothing of ordinary noise; a little more computation |
| Exponential filter | yk = yk−1 + α (xk − yk−1), 0 < α ≤ 1 | Smoothing with almost no memory or computation: one stored number | Delay grows as α falls; an outlier decays away gradually, never vanishes at once |
The exponential filter (also called an exponential moving average or EMA) is the digital version of the simple RC low-pass filter of Basic Electronics 4. The factor α says how much of each new reading to trust: α = 1 passes the readings unchanged, and a small α smooths a lot and reacts slowly. After a step change in the input, the output covers a fraction 1 − (1 − α)n of the step in n samples; it reaches 63 % after about 1 / (−ln(1 − α)) samples, roughly (1 − α) / α as a rule of thumb, which with a sample interval dt gives a time constant of about dt (1 − α) / α.
Worked example: one outlier
An ultrasonic sensor measures a wall 100 cm away. Twelve consecutive readings contain one wild echo (130.0). The table compares a mean of the last three readings (MA3), a median of the last three (Med3) and an exponential filter with α = 0.25 (started from the first reading).
The three filters as short, hardware-neutral Python-style pseudocode:
10. Calibration
Calibration removes the systematic part of a sensor's error by measuring it against known true values. If a sensor reads in a straight-line way, reading = gain × true + offset, only two numbers need finding, and the inverse turns a reading back into the true value.
- Average the calibration readings. A two-point calibration puts the noise of two readings straight into the gain and the offset, so average 50 or 100 readings at each point (section 9: the noise falls as 1 over the square root of N) and place the points near the two ends of the range you use.
- More points, and a check point. A line through several points, fitted by least squares (the line that minimises the sum of squared errors), averages their noise; and a point not used in the fit shows whether the straight-line model is any good. If it is not, use a curve or a lookup table.
- Calibrate in a linear variable. The infrared sensor of section 3 is nonlinear in distance but close to linear in 1 / V (since V ≈ k / d), so fit d against 1 / V as a straight line.
- Line and light sensors are calibrated in the same way, with the floor and the line as the two known “true” points: section 4's loi and hii are a two-point calibration of each sensor.
- Calibrations expire. Temperature, supply voltage, lighting and wear change a sensor, so recalibrate when conditions change, or compensate (section 11).
11. Error propagation
A measured distance is computed from other measured quantities, each with its own error. Error propagation estimates the error in the result. For a product or quotient, the relative errors of independent inputs add in quadrature (square, add, square root); for a sum or difference, the absolute errors do.
12. Choosing a sensor
Start from the job, not the sensor. Ask what the robot needs to know, how accurately, how fast, over what range, and in what environment (light, dust, floor, other robots), and what the data will cost in wiring, processing and money.
| Job | Good choices | Trade-offs |
|---|---|---|
| Is something in front of me? (obstacle detection) | Ultrasonic; laser time-of-flight; IR triangulation | Ultrasonic is wide and cheap but misses angled and soft surfaces; laser and IR are narrow and may miss a thin object between beams; none sees a glass door reliably |
| Follow a line | IR reflectance array (5 to 8 sensors); a camera for complex tracks | The array is cheap and fast but sensitive to ambient light and floor; a camera is flexible but needs processing |
| Detect a table edge or step (cliff) | Downward IR reflective sensor | Very cheap; short range; fooled by dark or shiny floors, so test on the real floor |
| Measure distance to a wall to a few millimetres | Laser time-of-flight; IR triangulation at close range | Higher cost; sunlight and surface colour affect them; ultrasonic is cheaper but only good to a few millimetres at best (typical) |
| Map a room | 2D lidar (a camera plus software is an alternative) | Costly, with a high data rate; sees one plane only |
| Know that I have hit something | Bump switch or whisker | Very cheap and certain, but reports only after contact |
| Know a mechanism has reached its end | Limit switch (normally closed wiring); hall or optical end-stop | Switches wear; optical sensors need clean surroundings |
| Know it is day or night, or find a light | LDR; photodiode; phototransistor | An LDR is cheap but slow and nonlinear; a photodiode is faster and more linear but needs a better circuit |
| Detect a stalled motor or a jammed gripper | Current sensing (shunt and amplifier) | Costs a resistor, an amplifier and an ADC channel; the shunt wastes some power and the signal is noisy |
| Recognise objects, colours or markers | Camera (with software) | Richest data but heavy processing, lighting dependence and cost |
Two more rules of thumb. First, combine sensors so that each covers the other's weakness: an ultrasonic sensor for wide coverage with an infrared for a narrow check, a bump switch under everything as a last resort. Second, measure on your own robot, floor and lighting before designing the behaviour around a number. Robotics Fundamentals 6 adds the sensors that measure the robot itself (encoders, gyroscopes, accelerometers) and how to combine their data with those from this chapter.
13. Challenges
Quick reference
- Chain: physical quantity, transducer, conditioning, ADC, software. LSB = Vref / 2n; counts = floor(V / Vref × 2n).
- Specifications: range, resolution (not accuracy), accuracy (bias plus noise), precision (spread), noise, response time, field of view, sample rate. Bias is cured by calibration, noise by averaging.
- Ultrasonic: d = v t / 2; v = 331.3 + 0.606 T m/s; 58.2 µs per cm at 20 °C; wide beam; soft and angled surfaces fail.
- IR triangulation: d = b f / x; output about k / d; error grows as d2; resolution is coarse far away.
- Laser time of flight and lidar: d = c t / 2 (1 cm is 66.7 ps); phase shift d = c φ / (4 π f), range c / (2f); lidar x = r cos θ, y = r sin θ, point spacing r dθ.
- Line sensors: normalise (raw − lo) / (hi − lo); position = Σ w x / Σ w; lost if Σ w is small.
- Switches: pull-up resistor, debounce 10 to 20 ms, normally closed wiring for fail-safe limits. Current sensing: V = I R, P = I2R, amplify then read.
- Camera: rich but heavy data (27.6 MB/s for 640 × 480 colour at 30 frames a second); depth needs two views or a known size, d = f W / w.
- Filters: mean, moving average (delay (N − 1) / 2 samples), median (kills outliers), exponential y += α (x − y); σ(mean) = σ / √N.
- Calibration: g = (t2 − t1) / (r2 − r1), true = g (r − r1) + t1; average the readings, use more points and a check point; fit a linear variable.
- Error propagation: relative errors of a product add in quadrature; temperature error is proportional to range, timing error is constant; fix the dominant error first.
Next, Robotics Fundamentals 6 turns the sensors on the robot itself: position, orientation and feedback. Wheel encoders, which count the turns the odometry of Robotics Fundamentals 4 depends on; inertial units with gyroscopes, accelerometers and compasses that measure how the robot is turning and tilting; why those readings drift, and how to combine several imperfect sensors (sensor fusion) in plain words; dead reckoning; and why GPS is not the answer indoors. The filters, calibration and error estimates from this chapter will be used all the way through.