Connecting Raspberry Pi to a USB sonar sensor
One of the easiest ways to sense the presence of objects is to use a sonar sensor. Before adding this capability to your system, here's a little tutorial on sonar sensors. Sonar sensors use ultrasonic sound to calculate the distance from an object. The sound wave travels out from the sensor, as illustrated in the following figure:
The device emits a sound wave 10 times a second. If an object obstructs these waves, the waves will reflect off of the object and then return to the sensor, as shown in the following figure:
The sensor then measures the returning waves. It uses the time difference between when the sound wave was emitted and when it returned in order to measure its distance from the object.
Note
Sonar sensors are also quite accurate, normally with a small percentage error, and are not affected by the lighting or color in the environment.
There are several choices if you want to use a sonar sensor to sense the distance. The first option is...