Earlier we did a project with an infrared distance measurement project with the GP2D12 module from Sharp. This is a good solution for indoor projects and can detect objects up to 80 cm. To detect objects further away we can use an ultrasonic ranging Module. Also the HC-SR04 is cheaper than the GP2D12. In this project we are going to use the HC-SR04 ultrasonic ranging module with an Arduino.
The HC-SR04 uses sound instead of light as the GP2D12 module does. The HC-SR04 sends a ping as a submarine does and measure the time between sending and receiving anything back when an object is in front of the sensor. Because using sound for its measurements we can reach up to 4 meters. The module is about 45x20x15 mm in size and has a 4 pin connection. Two pins are needed to power the module with 5 Volts. The working current is about 15 mA. One pin is the trigger ping and the last one is used to read the result of the measurements, the echo pin. The measuring angle from the HC-SR04 is 15 degree. At 4 meter distance this should be a beam of about 1 meter. At 1 meter this is 26 cm so we have to keep this in mind when using this information.
One ping of the HC0SR04 actually exists of 8 pulses at 40 kHz to do the measurement. To start a ping you need to provide a 10us pulse on the trigger input. When the distance is measured by the 8 pulses the HC0SR04 puts a pulse on the echo pin. You can calculate the distance with the length of the echo pulse and the speed of sound. The speed of sound is 340 m/s or 2.9 micro seconds per mm. We have to divide the length of the pulse by 2.9 to get the result in mm. The ping is traveling towards an object and back to the sensor again. Because of this we need to divide the result by two. Between two pings we need to keep a 60ms measurement cycle.
The connection of the HC-SR04 to an Arduino is really straight forward. Just connect the 5 volt and the ground to the outer pins. In this example the trigger pin is connected to 8 and the echo pin to the 9 of the Arduino.
In real life this is the setup I used in my case. Beware of the little change between the photo and the schematic. I rotated the HC-Sr04 180 degree to let it face it to the edge of the board.
The original article is at http://www.swanrobotics.com/HC-SR04_Project