Skip to content
Buy 10+ on select items — save 10% auto-applied
Free US shipping on orders $35+
Order by 3pm ET — ships same-day from the US
Skip to main content

Arduino HC-SR04: Stream Distance to Serial Monitor | ShillehTek

May 14, 2026 11 views

Arduino HC-SR04: Stream Distance to Serial Monitor | ShillehTek
Project

Build an Arduino HC-SR04 ultrasonic distance meter that streams live centimetre readings to Serial Monitor for fast prototyping with ShillehTek parts.

10 min Beginner4 parts

Project Overview

Arduino + HC-SR04 ultrasonic distance sensor: Build a simple distance meter that streams live centimetre readings to the Arduino Serial Monitor. The HC-SR04 fires a 40 kHz ultrasonic ping and times the echo, letting you measure roughly 2 cm to 4 m with hobby grade accuracy for parking sensors, level meters, and obstacle avoiding robots.

  • Time: ~10 minutes
  • Skill level: Beginner
  • What you will build: An Arduino streaming a live distance reading to the Serial Monitor.
HC-SR04 ultrasonic distance sensor module used with Arduino for distance measurement
The HC-SR04 - 4 pins, 5 V, hobby grade ranges from 2 cm to 400 cm.

Parts List

From ShillehTek

External

  • USB cable + computer running the Arduino IDE

Note: The HC-SR04 is 5 V only. If you are wiring it to a 3.3 V board (ESP32, Pi Pico, Pi 5) drop the ECHO line through a level shifter or a divider.

Step-by-Step Guide

Step 1 - Wire the Sensor

Goal: Connect the four pins to your Arduino.

What to do: Place the Arduino Nano and HC-SR04 on the breadboard (or position them so you can jumper between them), then make the connections below.

Arduino Nano wired to HC-SR04 sensor on a breadboard with VCC to 5V, GND to GND, TRIG to D9, and ECHO to D10
VCC -> 5 V, GND -> GND, TRIG -> D9, ECHO -> D10.
  • VCC -> 5 V
  • GND -> GND
  • TRIG -> D9
  • ECHO -> D10

Expected result: Four jumper wires connected, no extra components needed for an Arduino Nano at 5 V.

Step 2 - Upload the Sketch

Goal: Trigger a ping, measure the echo time, and convert it to centimetres.

What to do: Open the Arduino IDE, select your board and port, then upload the sketch below.

Code:

const int TRIG = 9;
const int ECHO = 10;

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);

  long us = pulseIn(ECHO, HIGH);
  float cm = us * 0.0343 / 2.0;   // speed of sound 343 m/s
  Serial.print("Distance: ");
  Serial.print(cm, 1);
  Serial.println(" cm");
  delay(200);
}

The math: sound travels 343 m/s, so each microsecond of round trip echo time means 0.0343 cm of round trip path. Divide by 2 for one way distance.

Expected result: The sketch uploads successfully and the Nano starts printing distance measurements over Serial.

Step 3 - Verify in the Serial Monitor

Goal: Confirm clean readings in real time.

What to do: Open Tools -> Serial Monitor and set the baud rate to 9600. Move your hand toward and away from the sensor to see the numbers change.

Arduino Serial Monitor showing live distance output from an HC-SR04 ultrasonic sensor in centimetres
Distance updates about 5 times a second. Move your hand to watch it track.

Expected result: Readings between 2 cm and about 400 cm, typically within about 1 cm at room temperature.

Step 4 - Where to Take It Next

Goal: Use the distance value to drive outputs or motion.

What to do: Once you have stable readings, you can build on the same sketch with any of these ideas:

  • Drive an LED with analogWrite proportional to distance for a parking aid bar
  • Wire a buzzer that beeps faster as something approaches
  • Mount on a servo to build a 180 degree ultrasonic radar
  • Pair with the L298N to make a basic obstacle avoiding robot

Expected result: A clear plan for your next build using the same HC-SR04 distance measurement.

Conclusion

You built an Arduino HC-SR04 distance meter that measures echo time and prints live centimetre readings to the Serial Monitor. With just two GPIO pins (TRIG and ECHO) you can turn distance into useful behavior for robotics, parking aids, and level sensing.

Want the exact parts used in this build? Grab them from ShillehTek.com. If you want help customizing this project or building something for your product, check out our IoT consulting services.