In this section, we will cover the process used to measure distance with the HC-SR04. Don't get concerned if this does not make immediately sense. I've provided the details here as background material, as this is the logical process that is implemented by our example program to make the sensor work. You will also find the process documented in the sensor's datasheet.
We measure distance with the HC-SR04 through the correct use and monitoring of the TRIG and ECHO pins. The process looks like this:
- Pull the TRIG pin HIGH for 10 microseconds. Pulling TRIG HIGH also makes the ECHO pin HIGH.
- Start a timer.
- Wait for either of the following to happen:
- ECHO to go LOW
- 38 milliseconds to elapse (from the datasheet, this is the time for >4 meters)
- Stop the timer.
If 38 milliseconds have passed, we conclude that there is no object in front of the sensor (at least within the effective range of 2 centimeters to 4 meters). Otherwise...