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.
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.
Sends 40 kHz ultrasonic pulses and times the returning echo.
Triggers the pulse and calculates distance using pulseIn().
Displays distance in centimetres every 500 ms.
Build and test the full circuit online — no hardware needed!
Components Required
Only 4 components needed — perfect for beginners!
How the Sensor Works
The HC-SR04 uses the time-of-flight principle — exactly like how bats navigate in the dark!
Arduino sends a 10-microsecond HIGH pulse to the TRIG pin.
The transmitter fires ultrasonic sound — inaudible to humans.
Waves reflect back when they hit any obstacle: wall, hand, box.
ECHO pin stays HIGH for exactly the duration sound was travelling.
The pulseIn() function returns the echo duration in microseconds.
Using speed of sound, Arduino converts time to distance in cm.
Speed of sound ≈ 0.034 cm/µs · Divide by 2 because sound travels there and back
Circuit Connections
Only 4 wires needed — very simple to set up!
| HC-SR04 Pin | Connects To | Arduino Pin | Wire Colour |
|---|---|---|---|
| VCC | Power | 5V | Red |
| GND | Ground | GND | Black |
| TRIG | Trigger output | Pin 9 | Purple |
| ECHO | Echo input | Pin 10 | Blue |
Step-by-Step Instructions
Go to tinkercad.com, sign in, click Circuits → Create new Circuit. Or use the pre-built link in Section 7!
Search "Arduino UNO" in the components panel and drag it onto the canvas.
Search "Ultrasonic Distance Sensor" and drag it onto the breadboard. You will see 4 pins: VCC, TRIG, ECHO, GND.
Connect: VCC → 5V, GND → GND, TRIG → Pin 9, ECHO → Pin 10. Click any pin and drag to the Arduino to draw a wire.
Click Code in the top menu, switch to Text mode, clear any existing code, and paste the Arduino code from Section 6.
The Arduino Code
Every line is commented so you understand exactly what it does.
// ===== 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 }
pulseIn(pin, HIGH)Measures how long a pin stays HIGH — returns duration in microseconds.
Serial.print()Sends text and values to the Serial Monitor for real-time debugging.
Try It Now!
Open the pre-built circuit, start the simulation, and watch live distance readings appear in the Serial Monitor!
▶ Open in TinkercadLearning Outcomes
After completing this project you will understand:
Real-World Applications
Troubleshooting Tips
Serial.begin(9600) in your code.Test Your Knowledge!
Answer these questions to check what you have learned.
Comments
Post a Comment