Measure obstacle distance with trigger and echo timing.
Open in compiler →Source code and build tools are available in the protected platform workspace.
How it works
Measure distance with an ultrasonic trigger/echo module.
- Wire TRIG and ECHO to the listed pins.
- Send a short trigger pulse.
- Measure echo pulse time with pulseIn().
- Convert travel time into distance in centimeters.
Connections
| Pin | Connect to | Note |
|---|---|---|
| D5 | HC-SR04 TRIG | Sends a 10 microsecond pulse. |
| D7 | HC-SR04 ECHO | 3.3V boards need level shifting for 5V echo modules. |