Uno: HC-SR04 Ultrasonic Distance is a runnable microcontroller simulation example on SensBench — open the wired circuit, inspect the code, and compile real firmware in your browser, no install needed. Last updated Aug 14, 2026.

FREE SIMULATOR EXAMPLE · BEGINNER

Uno: HC-SR04 Ultrasonic Distance

This example measures distance with an HC-SR04 ultrasonic sensor — TRIG on pin 9, ECHO on pin 10 — and prints centimeters to the Serial Monitor twice a second. Past roughly four meters it prints ‘Out of range’ instead of a bogus zero, because the code actually checks the timeout.

Board Arduino Uno (ATmega328P, 16 MHz, 5 V logic)
Difficulty beginner
Category sensors
Libraries None — just the Arduino core
Runs in Sensbench browser simulator — free, no account
Arduino Uno wired to an HC-SR04 ultrasonic sensor, TRIG on pin 9 and ECHO on pin 10, in the Sensbench simulator

The exact circuit you get when you open this example — already wired, ready to run.

The sketch

// Arduino Uno — HC-SR04 Ultrasonic Distance Sensor
// Wiring: TRIG → pin 9  |  ECHO → pin 10  |  VCC → 5V  |  GND → GND

#define TRIG_PIN 9
#define ECHO_PIN 10

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  Serial.println("HC-SR04 Ultrasonic Sensor ready");
}

long measureCm() {
  // Send 10 µs trigger pulse
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  // Read echo duration (30 ms timeout ≈ 5 m max)
  long duration = pulseIn(ECHO_PIN, HIGH, 30000UL);
  return (duration == 0) ? -1 : (long)(duration * 0.0343 / 2.0);
}

void loop() {
  long cm = measureCm();
  if (cm < 0) {
    Serial.println("Out of range (> 4 m)");
  } else {
    Serial.print("Distance: "); Serial.print(cm); Serial.println(" cm");
  }
  delay(500);
}

How it works

measureCm() opens each reading the way the HC-SR04 datasheet demands: TRIG low for 2 µs to clear the line, then a clean 10 µs HIGH pulse. The sensor answers by firing eight 40 kHz bursts and holding ECHO high for exactly as long as the sound takes to return. pulseIn(ECHO_PIN, HIGH, 30000UL) times that pulse, with a 30 ms ceiling — 30 ms of round-trip sound travel is about five meters.

The conversion is one line: duration * 0.0343 / 2.0. The constant 0.0343 cm/µs is the speed of sound in 20 °C air, and dividing by two removes the return leg. When pulseIn() times out it returns 0, which naive code prints as 0 cm — this sketch returns −1 instead, and the loop translates that into an honest out-of-range message.

Before you build it for real

The HC-SR04 is a 5V sensor, which is exactly why it pairs so cleanly with the Uno: ECHO swings 0-5V and the ATmega328P reads that as a proper logic HIGH with no divider. Do not carry this wiring over to a 3.3V board without two resistors on ECHO. Two more things the simulator will not teach you: pulseIn() is blocking, so a missing echo stalls your loop for the full 30 ms timeout, and soft or angled surfaces — curtains, foam, a wall at 45 degrees — absorb or deflect the ping and come back as −1 even at close range.

Get the parts

Related examples

FAQ

Where does 0.0343 come from?

It is the speed of sound in air at 20 °C: 343 m/s, which is 0.0343 cm per microsecond. Multiply by the echo time and you get the round-trip distance, so the sketch divides by two. Sound speeds up roughly 0.6 m/s per degree, so a hot attic reads slightly short and a cold garage slightly long.

What is the real range of this setup?

The HC-SR04 is specified for about 2 cm to 400 cm. Below 2 cm the outgoing ping and returning echo overlap; past 4 m the echo usually arrives too weak or after this sketch’s 30 ms timeout. Close targets and hard, flat surfaces give the cleanest numbers.