ESP32-C3: 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

ESP32-C3: HC-SR04 Ultrasonic Distance

This example measures distance with an HC-SR04 ultrasonic sensor — TRIG on GPIO5, ECHO on GPIO6 — and prints a centimeter reading to the Serial Monitor twice a second. There are no libraries at all: the whole protocol is a ten-microsecond trigger pulse and a timer, written out by hand so you can see exactly what the sensor does.

Board ESP32-C3 (RISC-V single-core, 160 MHz)
Difficulty beginner
Category sensors
Libraries None — just the Arduino core
Runs in Sensbench browser simulator — free, no account
ESP32-C3 wired to an HC-SR04 ultrasonic sensor with TRIG on GPIO5 and ECHO on GPIO6 in the Sensbench simulator

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

The sketch

// ESP32-C3 — HC-SR04 Ultrasonic Distance Sensor
// Wiring: TRIG → GPIO5  |  ECHO → GPIO6  |  VCC → 3V3  |  GND → GND

#define TRIG_PIN 5
#define ECHO_PIN 6

void setup() {
  Serial.begin(115200);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  Serial.println("ESP32-C3 HC-SR04 ready");
}

long measureCm() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  long d = pulseIn(ECHO_PIN, HIGH, 30000UL);
  return (d == 0) ? -1 : (long)(d * 0.0343 / 2.0);
}

void loop() {
  long cm = measureCm();
  if (cm < 0) Serial.println("Out of range");
  else        Serial.printf("Distance: %ld cm\n", cm);
  delay(500);
}

How it works

An HC-SR04 transaction has two halves: you ask, it answers. measureCm() pulls TRIG low, then high for exactly ten microseconds — delayMicroseconds(10) — which tells the sensor to fire its burst of 40 kHz pulses. ECHO then goes high for as long as the sound takes to reach the target and come back.

pulseIn(ECHO_PIN, HIGH, 30000UL) measures that high time in microseconds, with a 30 ms timeout — about five meters of round trip, comfortably past the sensor’s rated 400 cm. If it times out, pulseIn returns 0, measureCm() returns -1, and the sketch prints “Out of range” instead of a bogus zero-centimeter reading.

The conversion, d * 0.0343 / 2.0, is the speed of sound in cm/µs at 20 °C, divided by two because the pulse travels out and back. No library, no floating-point mystery — the entire protocol is visible in about twelve lines, which is exactly why this example is worth reading before you reach for a library.

Before you build it for real

The wiring here powers the HC-SR04 from 3V3, and that is the simulator being kind — the sensor is specified for 5 V, and many clones misbehave below 4.5 V with missed echoes and phantom short readings. The tempting fix, powering it from 5 V, creates the real trap: ECHO then swings 0–5 V into GPIO6, and the ESP32-C3’s pins are not 5 V tolerant. Use a divider on ECHO (1 kΩ + 2 kΩ lands at 3.3 V) or buy a 3.3 V-native variant like the HC-SR04P. Also know that pulseIn is blocking — the loop freezes while it waits, harmless here and a real bug in anything with a second task.

Get the parts

Related examples

FAQ

Why does it print "Out of range"?

pulseIn() timed out — 30 ms passed with no ECHO pulse, which means either nothing is within roughly five meters, the target is closer than the sensor’s 2 cm blind spot, or a wire is loose. In the simulator, first check ECHO actually landed on GPIO6.

How accurate is the 0.0343 factor?

That is the speed of sound at 20 °C expressed in cm/µs. It drifts roughly 0.17 % per degree, so expect centimeter-scale error outdoors on a hot day. For a parking sensor it is fine; for anything tighter, add a temperature sensor and scale the constant.