Practical Bench / Motors & Automotive · Beginner · 75 min
Reverse parking sensor and accuracy test
Build an ultrasonic distance sensor with median filtering and a variable-rate beeper, then measure its accuracy against a ruler and correct it by calibration.
Why it matters
Parking sensors in cars are ultrasonic range finders with a beeper that speeds up as the obstacle gets closer. Build one and find out how accurate it really is.
Toy or everyday hack
Mount the sensor on a toy car and let it roll towards a wall
Safety
- Keep the buzzer away from your ear when testing.
- Do not test on a moving real vehicle. Use the toy car and a wall.
- Ultrasound is outside hearing range and safe at this power.
Parts list
| Part | Qty | Note |
|---|---|---|
| Arduino Uno or compatible | 1 | |
| HC-SR04 ultrasonic sensor | 1 | 5 V part: use an Uno, or a level divider on ECHO for an ESP32 |
| Buzzer (active or passive) | 1 | |
| Toy car (optional) | 1 | Used as the moving vehicle |
| Flat board or book as target | 1 | About 20 cm square |
| Measuring tape or ruler | 1 | Longer than 1.5 m |
Tools and equipment
Connections
| From | To | Note |
|---|---|---|
| HC-SR04 VCC / GND | Arduino 5 V / GND | |
| HC-SR04 TRIG | Arduino D9 | |
| HC-SR04 ECHO | Arduino D10 | |
| Buzzer + | Arduino D8 | Buzzer − to GND |
What you should see
At 20 to 150 cm on a flat hard target, a typical module is within about a centimetre or two, if the temperature in the speed-of-sound formula is right. Below 2 to 3 cm it fails, and soft or angled targets give weak echoes and large errors. Speed of sound changes by about 0.6 m/s per degree Celsius.
Step by step
- Wire the sensor and the buzzer, then upload the sketch. Set TEMP_C in the sketch to the room temperature.
- Point the sensor at a flat board. Move the board slowly: the beeps should speed up as it gets closer and become continuous under 20 cm.
- Calibration: place the board at each true distance in the table, measured with the ruler from the sensor face. Read the Serial Monitor value and write it down.
- Try a soft target (jacket, cushion) and an angled target (30°) at 50 cm. Note what happens.
- Enter your numbers in the logbook. The page calculates the error at each distance and fits a straight line.
- If the line shows a consistent gain or offset error, apply the correction (measured = a × true + b, so true = (measured − b) / a) in the sketch.
Challenge: Add a second sensor on the toy car and decide which one the buzzer should follow (the closest one). Then measure how fast the toy car approaches using two readings 100 ms apart.
Code (Arduino Uno (or compatible), Arduino IDE)
Check pin numbers against your board. Test the sketch on your own hardware before a class demo.
// Practical 7 - Reverse parking sensor (Arduino Uno + HC-SR04 + buzzer)
const uint8_t TRIG = 9, ECHO = 10, BUZZ = 8;
const float TEMP_C = 25.0; // set to your room temperature
const float CM_PER_US = (331.3 + 0.606 * TEMP_C) / 10000.0 / 2.0; // speed of sound, one-way
float readCm() {
digitalWrite(TRIG, LOW); delayMicroseconds(3);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
unsigned long us = pulseIn(ECHO, HIGH, 30000UL); // 30 ms timeout = about 5 m
return us == 0 ? -1.0 : us * CM_PER_US;
}
float median5() { // median rejects single bad echoes
float v[5];
for (int i = 0; i < 5; i++) { v[i] = readCm(); delay(30); }
for (int i = 1; i < 5; i++) { float k = v[i]; int j = i - 1;
while (j >= 0 && v[j] > k) { v[j + 1] = v[j]; j--; } v[j + 1] = k; }
return v[2];
}
void setup() { Serial.begin(115200); pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); pinMode(BUZZ, OUTPUT); }
void loop() {
float d = median5();
Serial.println(d);
if (d < 0 || d > 150) { noTone(BUZZ); return; } // nothing close
if (d < 20) { tone(BUZZ, 2000); return; } // solid tone = STOP
unsigned gap = map((long)d, 20, 150, 80, 800); // closer = faster beeps
tone(BUZZ, 2000, 60); delay(gap);
}Readings logbook
Type your measured values. Calculated columns and the analysis update as you type, and your entries are saved in this browser.
If it does not work
| Symptom | Likely cause and fix |
|---|---|
| Reading is always -1 | No echo: check ECHO wiring, target too soft or too far, or the sensor powered from 3.3 V. |
| Reading jumps randomly | Single bad echoes from a second surface. The median filter helps; increase the delay between pings. |
| Consistent offset | Wrong temperature setting or distance measured from the wrong point. Measure from the sensor face. |
| Does not see thin objects | The beam is a cone of about 15°. Thin poles can be missed. This is why real cars use several sensors. |