Wire the ultrasonic sensor, then read the distance to a book on your computer screen and check it against a ruler.
| Part | How many |
|---|---|
| UNO R3 + USB cable | 1 |
| 400-point breadboard | 1 |
| HC-SR04 distance sensor | 1 |
| Male-to-male jumper wires | 6 |
| A ruler or tape measure, and a book or flat card (yours) | 1 |
Nothing extra - just the kit and your computer.
One "eye" of the sensor sends a short click of ultrasound; the other listens for the echo. The UNO starts it with a 10-microsecond pulse on TRIG.
The sensor then holds ECHO high for as long as the sound took to get there and back. pulseIn() measures that time in microseconds.
Sound needs about 58 microseconds for every centimetre of distance, there and back, so time ÷ 58 = centimetres.
Each line on the screen is the average of 5 pings. One ping can be off; the average of five is steadier. The second number says how many of the 5 echoes came back.
Every item is in the kit — nothing extra to buy.
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 and a flat card or book (yours): 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 |
|---|---|---|
| UNO 5V | + rail (red line) | Powers the sensor |
| UNO GND | − rail (blue line) | Shared ground for everything |
| HC-SR04 VCC | + rail (5V) | Go by the labels printed on the sensor |
| HC-SR04 TRIG | UNO pin 9 | |
| HC-SR04 ECHO | UNO pin 10 | |
| HC-SR04 GND | − rail (GND) |
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 1 of 9: Measure Distance with Sound
WIRING (unplug the USB cable before you wire)
HC-SR04 VCC -> 5V (+ rail)
HC-SR04 TRIG -> UNO pin 9
HC-SR04 ECHO -> UNO pin 10
HC-SR04 GND -> GND (- rail)
WHAT SHOULD HAPPEN
Open Tools > Serial Monitor and set it to 9600 baud. Twice a second a
new line shows the distance to whatever is in front of the sensor, and
how many of the 5 pings came back. Hold a book in front of the sensor
and compare the number with a ruler.
*/
const int TRIG_PIN = 9; // we send the "click" from here
const int ECHO_PIN = 10; // the sensor answers on this pin
const int SAMPLES = 5; // pings per line - try 1 and see the numbers jump
const unsigned long ECHO_TIMEOUT_US = 30000; // give up after 30 ms (about 5 m there and back)
const int MIN_CM = 2; // closer than this the sensor cannot measure
const int MAX_CM = 400; // further than this is out of range
int lineNumber = 0;
// One measurement in centimetres, or 0 if no echo came back.
long readOnceCm() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); // a 10-microsecond pulse starts one measurement
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// pulseIn() times how long ECHO stays HIGH: the sound's trip there and back.
unsigned long us = pulseIn(ECHO_PIN, HIGH, ECHO_TIMEOUT_US);
if (us == 0) return 0; // no echo
long cm = us / 58; // sound needs about 58 microseconds per cm, there and back
if (cm < MIN_CM || cm > MAX_CM) return 0;
return cm;
}
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(9600);
Serial.println("Lesson 1: Measure Distance with Sound");
Serial.println("line, distance_cm, echoes (out of 5)");
}
void loop() {
long total = 0;
int echoes = 0;
for (int i = 0; i < SAMPLES; i++) {
long cm = readOnceCm();
if (cm > 0) { // only count pings that came back
total = total + cm;
echoes = echoes + 1;
}
delay(60); // let the last echo die away before the next ping
}
lineNumber = lineNumber + 1;
Serial.print(lineNumber);
Serial.print(", ");
if (echoes > 0) {
long average = total / echoes; // the average of the pings that came back (whole cm)
Serial.print(average);
} else {
Serial.print("no echo");
}
Serial.print(", ");
Serial.println(echoes);
delay(200);
}
| In the code | What it does |
|---|---|
| const int TRIG_PIN = 9; | Names for the two sensor pins. If you ever move a wire, change only this number. |
| readOnceCm() | Our own function: one ping. It sends the 10 µs pulse, times the echo with pulseIn(), divides by 58 and returns centimetres - or 0 if no echo came back. |
| ECHO_TIMEOUT_US = 30000 | pulseIn() gives up after 30 ms. Without it the program could wait a long time when nothing is in front. |
| for (int i = 0; i < SAMPLES; i++) | Repeats the ping 5 times, adding up the ones that came back. Then it prints total ÷ echoes: the average. |
| Problem | What to check |
|---|---|
| It always says no echo | Unplug USB. Check VCC to the + rail, GND to the − rail, TRIG to pin 9 and ECHO to pin 10 - by the labels on the sensor, not their position. Point it at a big flat object 20-50 cm away. |
| The number is 0 or "no echo" even close up | Nothing may be in the beam: hold the book square to the sensor, not tilted. Check ECHO is on pin 10 and TRIG on pin 9, not swapped. |
| The distance is always a few cm too big | Measure from the front of the sensor's eyes, not the back of the board. Small errors are normal - Lesson 5 measures them. |
| 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. |
| The upload fails | See Start here, step 6: "If the upload fails". |
Measure the same book at 30 cm five times, moving it away and back each time. How far apart are your five numbers?
It uses ultrasound - 40 kHz, too high for human ears.
1160 ÷ 58 = 20 cm.
The average of several pings is steadier than one ping.
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.
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".
Type the ruler distance, get 10 readings back: average, error, minimum, maximum and spread. Paste the rows into a spreadsheet and find your sensor's accuracy.
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.