pulseIn to Time the Echo
Measure the echo pulse and convert to cm.
Fire the Pulse
To take a reading you first send a clean trigger pulse. The HC-SR04 expects a short HIGH on Trig to start a new measurement.
Start From Clean
Set Trig LOW for a couple of microseconds first. This clears any leftover state so your trigger pulse is crisp and reliable.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);All lessons in this course
- How Ultrasonic Ranging Works
- Trigger & Echo Wiring
- pulseIn to Time the Echo
- Build a Proximity Alarm