Turn the UNO into a measuring instrument: type the ruler distance and it prints the average, error and spread of 10 readings, ready for a spreadsheet.
| Part | How many |
|---|---|
| Wiring from Lesson 1 (the other parts can stay) | |
| Ruler or tape measure, flat card (yours) | 1 |
| Masking tape to mark distances (optional) |
You type the true distance from the ruler and press Enter. The sketch reads your digits one by one and builds the number: 2 then 0 makes 20.
It then takes 10 pings and prints one row: ruler_cm, average_cm, error_cm, min_cm, max_cm, spread_cm, echoes.
Error tells you whether the sensor reads too far or too near. Spread tells you how much it wobbles. A good sensor has both small.
Real sensors are often a little off in the same direction. Once you know the error, you could correct for it in code.
Every item below is in the kit. Not in the box: A ruler or tape measure and a flat piece of card (yours).
All 9 parts in one box, with this project and 8 more to build from them.
One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. a ruler or tape measure, a flat card, a spreadsheet (optional): included in the kit, ask us for it loose. Delivery is AED 20, free on orders AED 100+.
Unplug USB before you change any wire. Pin names are the labels printed on the board.
| Part / pin | Connect to | Note |
|---|---|---|
| No changes | Sensor as in Lesson 1 | TRIG pin 9 · ECHO pin 10 |
One thing at a time. Tick nothing off until it matches the diagram.
Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.
/*
DIY KIT - Arduino Ultrasonic Distance & Parking Learning Kit
Lesson 5 of 9: Calibrate Your Sensor
WIRING: the sensor from Lesson 1 (TRIG pin 9, ECHO pin 10).
The LEDs and buzzer can stay connected; this sketch does not use them.
HOW TO USE IT
1. Open the Serial Monitor at 9600 baud. Check the box at the bottom
right is set to "New Line".
2. Put a flat card at a ruler mark, for example 20 cm.
3. Type 20 and press Enter. The UNO takes 10 readings and prints one row:
ruler_cm, average_cm, error_cm, min_cm, max_cm, spread_cm, echoes
4. Repeat at 10, 20, 30, 50, 75 and 100 cm. Copy the rows into a spreadsheet.
*/
const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const int SAMPLES = 10; // readings per row
const unsigned long ECHO_TIMEOUT_US = 30000;
const int MIN_CM = 2;
const int MAX_CM = 400;
long typedNumber = 0; // the number being typed, digit by digit
long readOnceCm() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long us = pulseIn(ECHO_PIN, HIGH, ECHO_TIMEOUT_US);
if (us == 0) return 0;
long cm = us / 58;
if (cm < MIN_CM || cm > MAX_CM) return 0;
return cm;
}
// Take SAMPLES readings and print one spreadsheet row.
void measureAt(long rulerCm) {
long total = 0;
long lowest = 9999;
long highest = 0;
int echoes = 0;
for (int i = 0; i < SAMPLES; i++) {
long cm = readOnceCm();
if (cm > 0) {
total = total + cm;
echoes = echoes + 1;
if (cm < lowest) lowest = cm;
if (cm > highest) highest = cm;
}
delay(60);
}
Serial.print(rulerCm);
Serial.print(",");
if (echoes == 0) {
Serial.println("NA,NA,NA,NA,NA,0");
return;
}
long average = total / echoes;
Serial.print(average); Serial.print(",");
Serial.print(average - rulerCm); Serial.print(","); // error: + means it reads too far
Serial.print(lowest); Serial.print(",");
Serial.print(highest); Serial.print(",");
Serial.print(highest - lowest); Serial.print(","); // spread: how much it wobbles
Serial.println(echoes);
}
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(9600);
Serial.println("Lesson 5: Calibrate Your Sensor");
Serial.println("Put the card at a ruler mark, type the distance in cm and press Enter.");
Serial.println("ruler_cm,average_cm,error_cm,min_cm,max_cm,spread_cm,echoes");
}
void loop() {
while (Serial.available() > 0) {
char c = Serial.read();
if (c >= '0' && c <= '9') {
typedNumber = typedNumber * 10 + (c - '0'); // build the number one digit at a time
} else if (c == '\n' || c == '\r') {
if (typedNumber > 0) measureAt(typedNumber);
typedNumber = 0;
}
}
delay(1);
}
| In the code | What it does |
|---|---|
| typedNumber = typedNumber * 10 + (c - '0'); | Builds the number from your digits: "2" then "0" → 2 → 20. |
| measureAt(rulerCm) | Takes 10 readings, keeps the smallest and biggest, and prints one CSV row. |
| average - rulerCm | The error: positive means the sensor reads too far. |
| highest - lowest | The spread: how much the 10 readings wobble. |
| Problem | What to check |
|---|---|
| Nothing happens when I press Enter | Choose "New Line" in the box at the bottom right of the Serial Monitor and try again. |
| The row says NA and 0 echoes | Nothing came back: square the card up to the sensor and check the wiring from Lesson 1. |
| All the rows land in one spreadsheet column | Use Data → Split text to columns (Google Sheets) or Data → Text to Columns (Excel) with comma. |
| Nothing or strange symbols in the Serial Monitor | Set the speed in the corner of the Serial Monitor to 9600 baud. Close the Serial Plotter if it is open. |
Make a chart of error against distance in your spreadsheet. Does the error grow as the card gets further away?
+2 cm: the sensor reads 2 cm too far.
33 − 30 = 3 cm.
Calibration needs a reference we trust more than the sensor.
Most problems are one wire in the wrong hole. Check these first:
Open the troubleshooting table for this step →
Hi DIY KIT, I'm stuck on the Arduino Ultrasonic Distance & Parking Learning Kit. Lesson: __ Step number: __ What should happen: __ What happens instead: __ Error message (copied exactly): __ I'll attach a photo of my wiring.
Wire the HC-SR04 and read distances in the Serial Monitor. Compare them with a ruler to see how accurate the sensor really is. Do Start here first.
Find the sensor's limits: too close, too far and off to the side. The LEDs and running counts show good readings, "too close" and "no echo".
Why does the sensor sometimes miss things right in front of it? Fire 20 pings at flat, soft, round, thin and tilted targets and count how many echoes come back.
We use cookies that the shop needs to work — signing in, your cart, form security. We also measure how the shop is used so we can stock the right parts and fix what is broken. Choose “Only essential” and we will not load Google Analytics or the Meta pixel, and will not record your visit. Read more.