In the previous section, we learned how to detect movement with a PIR sensor. As we discovered, our PIR sensor was a digital device that signaled movement detection by making its output a digital HIGH.
It's time to learn how to measure distance with our Raspberry Pi. There are a variety of sensors that are capable of performing this task, and they commonly either work with sound or light. Our example will be based around the popular HC-SR04 ultrasonic distance sensor (it works on sound), as pictured in the following figure:
A place where you commonly find ultrasonic distance sensors is modern car bumper bars (they're often little round circles, which is a different form factor than the HC-SR04 pictured in the preceding figure). These sensors calculate the distance between your car and a nearby object and, for example, make a beeper inside your car...