Measure ultrasonic distance with an ESP32
Convert an echo pulse duration into a distance.

Physics Phoebe
Predict the reading, then compare it with the measurement.
Wokwi ESP32
Third party ToolConvert an echo pulse duration into a distance.
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.
sketch.ino
- Use the launch button above to open the ESP32 editor in Wokwi.
- Select the editor’s diagram.json tab and replace all its text with the supplied diagram.json.
- Select the sketch.ino tab, replace all its text with the supplied sketch.ino, then click Start Simulation.
Steps
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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · 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.
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 checkA 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