Analog, Digital and Noisy Readings
Learn how robots read digital and analog sensors, convert voltages into numbers, set thresholds and deal with noise using averaging and calibration.
Welcome back!
Step 1 of 11Learn
Same sensor, different numbers
Lena's line-following robot worked perfectly in the classroom. At the competition, under bright stadium lights, it drove straight off the track. Her sensor readings had shifted: on the classroom floor, black tape read about 200 and white floor about 800; under the bright lights, the white area read over 900 and the black tape around 450. Her program said "anything below 400 is black," so the robot no longer saw the line at all.
Lena's code wasn't wrong in the classroom. Her mistake was assuming sensor readings are fixed facts. In reality, sensors give raw signals that depend on conditions: lighting, temperature, battery level, the surface and even the individual sensor. Turning those raw signals into trustworthy information is one of the most practical skills in robotics.
To get there, you need to know how a controller reads sensors in the first place. There are two kinds of signals:
- Digital signals have only two states: on or off, HIGH or LOW, 1 or 0. A bump switch is either pressed or not.
- Analog signals vary smoothly across a range. A light sensor might output any voltage between 0 and 5 volts depending on brightness.
Most microcontrollers are digital at heart, so analog signals must be converted into numbers first.
Step 2 of 11Practice · Sort into groups
Sort these sensor outputs: digital or analog?
Tap an item, then tap its group (or drag it there).
Step 3 of 11Learn
From voltage to number: the ADC
An analog-to-digital converter (ADC) measures a voltage and turns it into a whole number. The range of numbers depends on the ADC's resolution, measured in bits.
A common hobby microcontroller has a 10-bit ADC. Ten bits can represent 210 = 1,024 different values, numbered 0 to 1023. If the ADC's reference is 5 volts:
- 0 V reads as 0
- 5 V reads as 1023
- 2.5 V reads as about 512
Each step is 5 V ÷ 1,024 ≈ 4.9 millivolts. Changes smaller than that are invisible to the controller. A 12-bit ADC has 4,096 steps and can notice changes about four times smaller.
To convert a reading back to volts: voltage = reading ÷ 1023 × 5 V (for a 10-bit ADC with a 5 V reference). A reading of 307 means about 307 ÷ 1023 × 5 ≈ 1.5 V.
Usually you don't care about volts but about what they mean: distance, brightness, temperature. Many programs map the raw range onto a useful range. If a light sensor reads 200 on black and 800 on white, you might map that onto 0 to 100% brightness. The formula for mapping is: percent = (reading − low) ÷ (high − low) × 100. A reading of 500 maps to (500 − 200) ÷ 600 × 100 = 50%.
Step 4 of 11Practice · Diagram
A 10-bit ADC with a 5 V reference
The converter maps 0 to 5 volts onto whole numbers from 0 to 1023.
A light sensor outputs 2.5 V. Tap the reading the ADC will give.
Tap the shape that answers the question, then press Check.
- 0 V
Reading 0.
- 1.25 V
About a quarter of the range: reading near 256.
- 2.5 V
Half the reference voltage: reading near 512.
- 3.75 V
Three quarters: reading near 767.
- 5 V
The maximum reading, 1023.
Step 5 of 11Try the calculator
ADC reading converter
Convert a raw ADC reading into volts and into a percentage between your calibrated low and high readings.
- Number of steps
- 1,024
- Voltage
- 2.44 V
- Brightness
- 50 %
Step 6 of 11Practice · Work it out
A 10-bit ADC with a 5 V reference reads 614. About what voltage is that, in volts? (Use reading ÷ 1023 × 5.)
Step 7 of 11Learn
Thresholds, noise and calibration
Real sensor readings jitter. A distance sensor pointed at a still wall might report 51, 49, 52, 50, 48 cm. That random variation is called noise, and it comes from electrical interference, vibration, tiny changes in the environment and the limits of the sensor itself.
Three techniques tame it:
Averaging. Take several readings quickly and use their average. Five readings of 51, 49, 52, 50 and 48 average to exactly 50. A moving average keeps the last few readings and updates the average each time a new one arrives, smoothing the signal at the cost of reacting slightly later.
Thresholds with a gap. To decide black or white, pick a threshold halfway between calibrated values: with black at 200 and white at 800, use 500. If the reading jitters around the threshold, the robot may flicker between decisions. A fix is hysteresis: switch to "black" below 450 but only back to "white" above 550.
Calibration. Instead of hard-coding numbers, measure them in the real conditions. Many competition robots start with a calibration routine: sweep the sensors over the line and the floor, record the lowest and highest readings, and compute the threshold automatically. That's exactly what would have saved Lena's robot under the stadium lights.
Step 8 of 11Practice · Chart
Noisy readings vs a running average
A distance sensor aimed at a wall 50 cm away. Averaging the last three readings smooths the jitter, at the cost of reacting a little later.
Tap the reading where the raw value was farthest from the true 50 cm.
Tap the point that answers the question, then press Check.
| Reading number | Raw reading | Average of last 3 |
|---|---|---|
| 1 | 51 cm | 51 cm |
| 2 | 49 cm | 50 cm |
| 3 | 52 cm | 50.7 cm |
| 4 | 50 cm | 50.3 cm |
| 5 | 48 cm | 50 cm |
| 6 | 53 cm | 50.3 cm |
| 7 | 46 cm | 49 cm |
| 8 | 50 cm | 49.7 cm |
| 9 | 51 cm | 49 cm |
| 10 | 49 cm | 50 cm |
Step 9 of 11Note
Step 10 of 11Practice · Work it out
A line sensor reads 180 on the black line and 860 on the white floor after calibration. What threshold sits exactly halfway between them?
Step 11 of 11Practice · Choose one
Lena's robot fails under brighter lights because readings shifted upward. What is the most robust fix?
Lesson complete!