Ultrasonic Distance Measurement Using Arduino Uno (Step-by-Step Guide)

Ultrasonic Distance Measurement with Arduino HC-SR04 | MakeMindz

Ultrasonic
Distance Measurement

Measure real distances using sound waves — just like bats navigate in the dark! Build a live distance meter with Arduino UNO and the HC-SR04 sensor.

Section 01

What Will You Build?

A real-time distance meter using the HC-SR04 ultrasonic sensor and Arduino UNO. The sensor sends a sound pulse, listens for the returning echo, and calculates how far an object is — displayed live in the Serial Monitor.

📡
HC-SR04 Sensor

Sends 40 kHz ultrasonic pulses and times the returning echo.

🧠
Arduino UNO

Triggers the pulse and calculates distance using pulseIn().

📺
Serial Monitor

Displays distance in centimetres every 500 ms.

🖥️
Tinkercad Sim

Build and test the full circuit online — no hardware needed!

Section 02

Components Required

Only 4 components needed — perfect for beginners!

01
🔵 Arduino UNO×1
02
📡 HC-SR04 Ultrasonic Sensor×1
03
🟫 Breadboard×1
04
🔌 Jumper WiresSeveral
Section 03

How the Sensor Works

The HC-SR04 uses the time-of-flight principle — exactly like how bats navigate in the dark!

1
TRIG pin fires a pulse

Arduino sends a 10-microsecond HIGH pulse to the TRIG pin.

2
Sensor emits 40 kHz waves

The transmitter fires ultrasonic sound — inaudible to humans.

3
Sound bounces off an object

Waves reflect back when they hit any obstacle: wall, hand, box.

4
ECHO pin goes HIGH

ECHO pin stays HIGH for exactly the duration sound was travelling.

5
Arduino measures the time

The pulseIn() function returns the echo duration in microseconds.

6
Distance is calculated

Using speed of sound, Arduino converts time to distance in cm.

Distance Formula
Distance (cm) = (Time × 0.034) ÷ 2

Speed of sound ≈ 0.034 cm/µs  ·  Divide by 2 because sound travels there and back

HC-SR04 Ultrasonic Distance Measurement Circuit Diagram with Arduino UNO


Section 04

Circuit Connections

Only 4 wires needed — very simple to set up!

HC-SR04 Pin Connects To Arduino Pin Wire Colour
VCCPower5VRed
GNDGroundGNDBlack
TRIGTrigger outputPin 9Purple
ECHOEcho inputPin 10Blue
💡 Tinkercad tip: Hover over any pin to see its label. TRIG sends, ECHO receives — do not swap them!
Section 05

Step-by-Step Instructions

1
Open Tinkercad and Create a New Circuit

Go to tinkercad.com, sign in, click Circuits → Create new Circuit. Or use the pre-built link in Section 7!

💡 Sign in with a Google account — no email verification needed.
2
Add the Arduino UNO

Search "Arduino UNO" in the components panel and drag it onto the canvas.

3
Add the HC-SR04 Sensor

Search "Ultrasonic Distance Sensor" and drag it onto the breadboard. You will see 4 pins: VCC, TRIG, ECHO, GND.

⚠️ Don't swap TRIG and ECHO — TRIG is OUTPUT (sends), ECHO is INPUT (receives).
4
Connect the 4 Wires

Connect: VCC → 5V, GND → GND, TRIG → Pin 9, ECHO → Pin 10. Click any pin and drag to the Arduino to draw a wire.

💡 Click a wire and use the colour picker to colour-code your connections — makes debugging easier!
5
Enter the Code

Click Code in the top menu, switch to Text mode, clear any existing code, and paste the Arduino code from Section 6.

6
Section 06

The Arduino Code

Every line is commented so you understand exactly what it does.

ultrasonic_distance.ino
// ===== Ultrasonic Distance Measurement =====
// makemindz.com

int trigPin = 9;   // TRIG pin → Digital Pin 9
int echoPin = 10;  // ECHO pin → Digital Pin 10

long  duration;   // Echo travel time (microseconds)
float distance;   // Calculated distance in cm

void setup() {
  pinMode(trigPin, OUTPUT); // TRIG sends pulses
  pinMode(echoPin, INPUT);  // ECHO listens for return
  Serial.begin(9600);       // Start Serial Monitor
}

void loop() {

  // Step 1: Clear TRIG pin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Step 2: Send a 10µs HIGH pulse
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Step 3: Measure echo duration
  duration = pulseIn(echoPin, HIGH);

  // Step 4: Calculate distance
  // 0.034 cm/µs = speed of sound, ÷2 for round trip
  distance = duration * 0.034 / 2;

  // Step 5: Print to Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500); // Wait 500ms between readings
}
Key Function
🔑 pulseIn(pin, HIGH)

Measures how long a pin stays HIGH — returns duration in microseconds.

Key Function
🔑 Serial.print()

Sends text and values to the Serial Monitor for real-time debugging.

Serial Monitor Output — 9600 baud
Distance: 20.00 cm
Distance: 19.72 cm
Distance: 35.48 cm
Distance: 35.50 cm
Section 07

Try It Now!

Launch the Tinkercad Simulation

Open the pre-built circuit, start the simulation, and watch live distance readings appear in the Serial Monitor!

▶ Open in Tinkercad
Section 08

Learning Outcomes

After completing this project you will understand:

Working principle of ultrasonic sensors
Time-of-flight measurement
Using the pulseIn() function
Distance calculation with sound speed
Serial Monitor for debugging
Arduino INPUT and OUTPUT pins

Real-World Applications

🤖
Obstacle Detection Robot
🅿️
Smart Parking System
📏
Distance Measuring Tool
🚪
Automatic Door Opener
💧
Water Level Monitor

Troubleshooting Tips

⚠️
Wrong readings? Check that TRIG and ECHO are not swapped — TRIG is output, ECHO is input.
📐
Out of range? HC-SR04 works reliably between 2 cm and 400 cm. Beyond that, readings are unreliable.
🖥️
Serial Monitor blank? Make sure baud rate is set to 9600 — must match Serial.begin(9600) in your code.
Sensor not responding? Ensure VCC is connected to the 5V pin, not 3.3V.
Section 09

Test Your Knowledge!

Answer these questions to check what you have learned.

Q1. What does the TRIG pin do on the HC-SR04?
Q2. Why do we divide by 2 in the distance formula?
Q3. Which function measures the duration of the echo pulse?
Q4. What is the effective measurement range of the HC-SR04?

Comments

try for free