Skip to content

Measure ultrasonic distance with an ESP32

Convert an echo pulse duration into a distance.

Physics Phoebe, your practice guide

Physics Phoebe
Predict the reading, then compare it with the measurement.

Wokwi ESP32

Third party Tool

Convert an echo pulse duration into a distance.

Tier 2 · Web · paste-in setup · Account needed to save a project

Version tested: Compiled with arduino-cli 1.5.2-rc.1 for esp32 core 3.3.11; booted headlessly with wokwi-cli 0.26.1 on 2026-09-07; serial line 'Echo=5883 us, distance=101.4 cm' seen at 3.823 s (wall time from CLI start; scenario timeout 20 s simulated time); captured in the Wokwi web editor on 2026-09-09. Date: 2026-09-06.

Open the ESP32 editor, paste diagram.json, then paste sketch.ino.

Open Wokwi to paste in the files (new tab)

Get the files

Use both prepared files. This is a paste-in setup; saving a project requires a Wokwi account.

diagram.json

906 bytes · Circuit connections

Download

sketch.ino

395 bytes · ESP32 program

Download

  1. Use the launch button above to open the ESP32 editor in Wokwi.
  2. Select the editor’s diagram.json tab and replace all its text with the supplied diagram.json.
  3. Select the sketch.ino tab, replace all its text with the supplied sketch.ino, then click Start Simulation.

Steps

Screens captured against Wokwi ESP32 Compiled with arduino-cli 1.5.2-rc.1 for esp32 core 3.3.11; booted headlessly with wokwi-cli 0.26.1 on 2026-09-07; serial line 'Echo=5883 us, distance=101.4 cm' seen at 3.823 s (wall time from CLI start; scenario timeout 20 s simulated time); captured in the Wokwi web editor on 2026-09-09 on 2026-09-06; the tool may have moved on — the text steps are the contract.

  1. 1 Step 1

    Do
    In the Simulation panel, inspect the HC-SR04 wiring after pasting both supplied files.
    You will see
    The sensor has power and ground connections, with TRIG on ESP32 pin 18 and ECHO on pin 19.
    Why it matters
    The chapter starts by matching a sensor family to a physical quantity. This proximity setup uses pulse timing to represent distance.
    Step 1: The HC-SR04 power, ground, TRIG, and ECHO connect to the ESP32. The ring marks the sensor.
    Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  2. 2 Step 2

    Do
    In the sketch.ino editor tab, find pulseIn(19,HIGH,30000) and the distance conversion us/58.0.
    You will see
    The code reads echo duration in microseconds and divides it by 58 to print centimetres.
    Why it matters
    The interface determines what evidence you need. Pulse timing and its conversion must both be checked before interpreting the distance.
    Step 2: pulseIn reads the echo duration and us/58.0 converts it to centimetres. The ring marks those source lines.
    Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  3. 3 Step 3

    Do
    In the Simulation toolbar, click the play button and wait for compilation.
    You will see
    The timer starts, the stop button appears, and the Serial Monitor opens with echo and distance results.
    Why it matters
    A running model tests the supplied setup. The chapter still requires checks of mounting, target surface, and field conditions for a real ranging device.
    Step 3: The simulation is running with echo and distance output. The ring marks the Stop control.
    Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  4. 4 Step 4

    Do
    In the Serial Monitor panel, compare the reported distance with the sensor’s initial 100 cm setting.
    You will see
    At 100 cm, the recorded run shows Echo=5883 us, distance=101.4 cm.
    Why it matters
    The reported value differs from the input setting. Keeping that difference visible follows the chapter’s rule to record uncertainty rather than hide it.
    Step 4: At the 100 cm control setting, Echo=5880 us, distance=101.4 cm. This run differs slightly from the historical echo timing. The ring marks the measured echo and calculated distance.
    Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  5. 5 Step 5

    Do
    In the Simulation panel, select the HC-SR04 and set its distance control to 50 cm.
    You will see
    At 50 cm, the recorded run shows Echo=2942 us, distance=50.7 cm.
    Why it matters
    A nearer target tests another point in the chosen range. One successful distance does not establish the part’s accuracy throughout its use case.
    Step 5: At the 50 cm control setting, Echo=2936 us, distance=50.6 cm. This run differs slightly from the historical echo timing. The ring marks the measured echo and calculated distance.
    Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  6. 6 Step 6

    Do
    In the HC-SR04 control panel, set distance to 200 cm.
    You will see
    At 200 cm, the recorded run shows Echo=11759 us, distance=202.7 cm.
    Why it matters
    The longer range changes the pulse duration and the reported error. Comparing points supports the chapter’s call for calibration and range evidence.
    Step 6: At the 200 cm control setting, Echo=11765 us, distance=202.8 cm. This run differs slightly from the historical echo timing. The ring marks the measured echo and calculated distance.
    Step 6 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  7. 7 Step 7

    Do
    In the sketch.ino editor tab, find the if(!us) branch for a missing echo.
    You will see
    That branch prints No echo: out of range or wiring fault instead of a distance.
    Why it matters
    The chapter asks for a known failure state and a safe fallback. A missing echo must stay distinguishable from a valid distance reading.
    Step 7: The missing-echo branch prints No echo: out of range or wiring fault. The ring marks the fallback branch.
    Step 7 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)

Chapter checks

These questions refer to the chapter’s examples. Use the return links to review their answers.

  1. A distance sensor works on a flat white target in the lab but fails when installed over angled dark material. What is the best under-the-hood response?

    Return to the chapter’s knowledge check
  2. A wearable activity prototype uses an accelerometer successfully on a desk, then moves to wrist-worn use. What evidence should the practitioner add?

    Return to the chapter’s knowledge check

Caution

Tool versions change and screens may differ. Reopen the supplied setup, check the tool documentation, and use the site feedback control if the problem remains. The screenshots show browser measurements, not hardware calibration; values may vary slightly between runs. The division by 58 is an approximate conversion; the captured distances differ from the control settings and are not calibrated measurements.

Return to Common Sensors and MEMS · Browse Labs