srinivas.siteApplied RoboticsUnit 04

Unit 04 · Phase 1 · Electrical foundations

Sensors, calibration and noise

Mission: Why did the reading jitter? Calibrate a distance sensor against references you trust, and say what calibration fixes and what it doesn’t.

The interactive version of this unit needs JavaScript. Below are its steps as text.

Steps

1. Ping the wall

The Unit 01 rover is parked facing a wall about half a metre away. On its nose is an ultrasonic distance sensor: one can sends a click of sound, the other listens. Press Send one ping.

Why it matters: A sensor converts a physical quantity, here distance, into a signal the controller can read. This sensor’s signal is a time: how long its sound pulse took to reach the wall and return. Sound is far too fast to watch, so the stage slows it right down. Turning that time into centimetres is the next step.

2. From echo time to centimetres

Press Convert the echo. Watch the path: the sound goes to the wall and comes back, so it covers the distance twice.

Why it matters: This is time of flight: time a signal, then use its speed to get a distance. The ÷ 2 is there because the echo time covers the trip there and back. The 343 m/s is the speed of sound in air at about 20 °C, and it changes with temperature. Every reading from this sensor passes through this conversion.

3. Ten pings at a still wall

Nothing on the bench moves. Press Take 10 readings and watch the dot plot fill: one dot per reading, stacked where readings land on the same value.

Why it matters: Noise makes repeated readings differ even when the target stays still: small changes in the echo, moving air, the timer’s resolution. The mean is the sum divided by the count, and the range is the largest minus the smallest. This is why the reading jitters, the question Unit 03 left. Note the axis: it is zoomed to a few centimetres so the jitter is visible.

4. Average more: a steadier mean

Press Take 60 more. They arrive in six groups of ten, and each group’s mean appears as an orange diamond above the dots.

Why it matters: Averaging doesn’t make any single reading less noisy, but random ups and downs partly cancel in a mean. So the mean of ten readings jitters less than one reading does. The cost is time: ten pings take ten times as long. Keep one question in mind: every mean sits near 54 cm, but is the wall really there?

5. Check against a tape measure

Lay a tape measure from the rover’s front bumper to the wall: a reference distance. Press Lay the tape, then compare the reference (green line) with the readings and their mean (blue).

Why it matters: A reference is a measurement you trust more than the one you are testing, here a tape. The error is reading − reference, and a bias is the part of the error that every reading shares. Unit 01’s what-if added +30 cm to every reading: tidy dots, all wrong, and this is the same thing at a smaller size. More pings can’t reveal a bias; only a reference can.

6. Three reference distances

Press Measure at 20, 50 and 100 cm. The wall moves to each taped distance, the sensor pings ten times, and the new chart on the right plots error (reading − reference) against the reference.

Why it matters: An error that is the same at every distance is an offset; one that grows in proportion to the distance is a scale error. This sensor shows both, for example cans set back behind the bumper (offset) and code that assumes 343 m/s in a cold room (scale). Three references are enough to tell the two apart. The vertical scatter in each cloud is still the noise.

7. Fit the calibration line

Press Fit the line. The model finds the straight line that best matches the three mean readings: reading ≈ scale × reference + offset. On the error chart it appears as a sloping line.

Why it matters: Calibration estimates the relationship between readings and references, here a straight line with a scale and an offset. This fit is called least squares: it picks the line with the smallest total of squared misses. The means still carry a little noise, so the estimate lands near the truth, not on it. More references and more samples make it better.

8. Apply the calibration

Press Calibrate the readings. Each reading becomes (reading − offset) ÷ scale, and the dots move on the error chart.

Why it matters: Calibration removed the bias, the offset and scale every reading shared. It did not remove the noise: the scatter in each cloud is unchanged. That answers this unit’s video question: calibration fixes the shared error, averaging tames the random one, and neither can do the other’s job.

9. Tilt the wall

The wall is back at 50 cm. Press Tilt the wall 40° and ping it ten times. Watch where the sound goes.

Why it matters: An ultrasonic sensor hears an echo only if some of its sound comes back toward it. A steep, smooth target reflects the sound away, like light off a mirror. Missing or wildly long readings are a geometry problem, not noise to average away, and calibration can’t fix them. Point the sensor square to the target, and treat sudden jumps as suspect.

10. Past the range limit

Press Move the wall to 4.5 m. The stage’s tape ends at 1.2 m, so watch the wall leave the stage and read the readout.

Why it matters: Every sensor has a stated range; typical hobby ultrasonic modules quote about 2 cm to 4 m, and the real limit depends on the target. Beyond it the echo is too weak, and very close up the module can’t separate its own click from the echo. Good code treats “no echo” as missing, never as 0 cm. Soft or angled targets shrink the useful range further.

11. Worked example: three distances, ten each

Press Show the worked results. Each distance lights up in turn on the error chart while the readout gives its mean error and spread. The full table is in the Notebook tab.

Why it matters: This is the pass evidence: ten samples at each of three measured distances, reported as mean error and spread. Mean error speaks about bias, and spread speaks about noise. Report both with units and you can say what calibration would fix and what it wouldn’t.

12. Your bench: three distances, ten each

Press Show the bench setup to see how to place the tape, target and sensor. Then open the Notebook tab and write your prediction before you measure anything.

Why it matters: Your readings are the evidence; the stage only shows what to look for. Measure the reference from a point you name and keep, such as the sensor’s front face, and keep the target flat and square. Save ten samples at each distance before calculating anything. Then try one investigation: a tilt, a soft or shiny surface, or a distance near the range limit.

Go deeper

Math: mean, range, scale and offset

mean           x̄ = (x₁ + x₂ + … + xₙ) / n
range          R = largest − smallest
error          e = reading − reference
model          reading ≈ scale × true + offset
calibrated     true ≈ (reading − offset) / scale

least-squares line through points (rᵢ, mᵢ):
  scale  = Σ (rᵢ − r̄)(mᵢ − m̄) / Σ (rᵢ − r̄)²
  offset = m̄ − scale × r̄

Time of flight

distance = speed of sound × echo time / 2
speed of sound ≈ 331.3 + 0.606 × T  m/s   (T in °C)
Example: 2,915 µs at 343 m/s → 343 × 0.002915 / 2 = 0.500 m = 50.0 cm

The bench sketch

Arduino-style C++ for a common 4-pin ultrasonic module. It prints one raw reading per line, and “no echo” instead of 0 when nothing comes back. Check your module’s voltage and pins first.

// Unit 04 bench sketch (Arduino-style C++), for a 4-pin ultrasonic module (VCC, TRIG, ECHO, GND)
// on a 5 V board. Prints one raw reading in cm per line; check your module's datasheet first.
const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const float SOUND_M_S = 343.0;          // about 20 °C; write your room temperature in the notebook

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  digitalWrite(TRIG_PIN, LOW);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(TRIG_PIN, HIGH);         // a 10 µs trigger pulse starts one ping
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  unsigned long echo_us = pulseIn(ECHO_PIN, HIGH, 30000UL);  // give up after 30 ms
  if (echo_us == 0) {
    Serial.println("no echo");          // missing, never 0 cm
  } else {
    float cm = SOUND_M_S * (echo_us / 1e6) / 2.0 * 100.0;
    Serial.println(cm, 1);
  }
  delay(100);                            // let old echoes die away before the next ping
}

Spread, standard deviation and uncertainty

Range is easy to compute but grows as you take more samples, because extremes become more likely. The standard deviation is a steadier measure of spread, and the mean of n readings jitters roughly as the standard deviation divided by √n. A full uncertainty statement also includes the reference’s own resolution and any bias you could not remove.

Model limits

Public video question

What does calibration fix, and what does it not fix?

Watch and read