Prototype a VL53L0X range decision in Wokwi
Exercise a VL53L0X firmware decision path with an explicit Wokwi range-input surrogate and compare it with one HC-SR04 line without claiming physical-sensor evidence.

Radio Remi: compare the two readings, then state exactly what the simulator did not test.
Predict the reading, then compare it with the measurement.
Wokwi ESP32
Third party ToolOpen 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 Wokwi's diagram.json editor, identify the labelled optical-range surrogate and the HC-SR04 trigger and echo wiring.
- You will see
- Power: ToF surrogate target VCC connects to ESP32 3.3 V; GND returns to ground (diagram.json line 11). ToF surrogate signal connects to ESP32 GPIO34 (diagram.json line 12). Power: sonar VCC connects to ESP32 VIN; GND returns to ground (diagram.json line 13). Sonar trigger connects to ESP32 GPIO18 (diagram.json line 14). Sonar echo connects to ESP32 GPIO19 (diagram.json line 14).
- Why it matters
- GPIO34 is the controlled optical-range surrogate, while GPIO18 and GPIO19 are the separate ultrasonic trigger and echo paths. Later readings can be compared without presenting the surrogate as an actual VL53L0X.

Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 2
- Do
- In Wokwi's sketch.ino editor, find the surrogate boundary, millimetre conversion, and ultrasonic comparison line.
- You will see
- Enter the surrogate ToF reader:
int readTofMm() {(sketch.ino line 16). Read the surrogate ADC pin:int raw = analogRead(TOF_SURROGATE_PIN);(sketch.ino line 17). Map 12-bit counts to 50–2000 mm:return map(raw, 0, 4095, 50, 2000);(sketch.ino line 18). The surrogate range maps ADC 0 to 50 mm and ADC 4095 to 2000 mm (sketch.ino lines 16–18). - Why it matters
- The function labels an ADC-derived surrogate rather than a real optical sensor. Mapping 0–4095 into 50–2000 mm defines the simulated comparison range used later.

Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 3
- Do
- In the Simulation toolbar, start the run and read the Serial Monitor heading.
- You will see
- Input: supplied optical-range surrogate sketch. Observed: "RANGING READY: VL53L0X range-input surrogate; not a stopping-safety test". Observed: a ToF surrogate row follows the heading. Expected: the run states its simulation boundary.
- Why it matters
- The named range input is a surrogate. The heading explicitly rules out a stopping-safety test. The monitor identifies the running measurement model. This statement belongs with every reading from the lab.

Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 4
- Do
- In the Serial Monitor panel, leave the range input at its supplied setting.
- You will see
- Input: supplied surrogate setting and 100 cm ultrasonic target. Observed: "ToF surrogate=202 mm status=SIM | HC-SR04=101.4 cm (different beam and physics)". Observed: an earlier transient HC-SR04=-1.0 cm row also appeared. Expected: roughly 200 mm and 100 cm are reported separately.
- Why it matters
- The optical surrogate is reported in millimetres. The ultrasonic comparison is in centimetres. The valid later row is 202 mm and 101.4 cm. The serial label prevents treating the two devices as the same measurement.

Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 5
- Do
- In the range-input control panel, set the surrogate to the 500 mm fixture.
- You will see
- Input: move the labelled surrogate control to its 500 mm fixture. Observed: "ToF surrogate=500 mm status=SIM | HC-SR04=101.4 cm (different beam and physics)". Expected: surrogate changes while the ultrasonic target stays fixed. The preceding valid row was 202 mm and 101.4 cm.
- Why it matters
- The changed input moved the surrogate to 500 mm. The HC-SR04 comparison stayed at 101.4 cm. The status is still SIM. The two fields are separate simulated range paths.

Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 6
- Do
- In the Simulation toolbar, stop and restart; read the Serial Monitor safety boundary.
- You will see
- Input: restart the Wokwi simulation once. Observed: "RANGING READY: VL53L0X range-input surrogate; not a stopping-safety test". Observed after restart: "ToF surrogate=500 mm status=SIM | HC-SR04=101.4 cm (different beam and physics)". Expected: the declared safety limit remains visible after restart.
- Why it matters
- The heading appears again in the second simulation. The simulator retained the selected surrogate control. The status remains SIM. No physical stopping-safety claim follows from this test.

Step 6 · 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.
Why should a ToF reading carry a status or confidence value?
Return to the chapter’s knowledge checkWhich choice best fits one controlled, nearby optical zone?
Return to the chapter’s knowledge check