Raspberry Pi
raspberry pi
 
Ultrasonic distance measuring - HC-SR04
These devices can be found on ebay, costing from £7.99 for 5.

There are some issues using these devices. They miss narrow objects (like chair legs) and if the object is at an angle then the distance returned can be very wrong. Not to mention that linux is not a real time OS and can completely miss the timed echo.


image


How it works

Simply connect Vcc to 5v (Raspberry Pi pin 2) and GND to 0v (Raspberry Pi pin 6). Trig is connected to a GPIO output pin and Echo is connected to a GPIO input pin. In this example, I use Raspberry Pi pin 7 connected to Trig, and Raspberry Pi pin 11 connected to Echo.

Set the Trig high for 0.0001s. Wait until the Echo returns high and time how long it is until Echo returns low.

This time is the length of time taken for the ultrasonic sound wave to travel to what is detected and back again. All you need to know is multiply this time by 1000000 to convert to microseconds, then divide by 58 to get the distance in centimeters.

Wait at least 0.01s before setting TRIG high again.


Example Python Code

To use this code you must install the RPi.GPIO library, and run the program as root.

To install RPi.GPIO type :
sudo apt-get install python-rpi.gpio

Please Click Here to download the zipped python file.