Brigadoon - An Occasional Corner on the Internet
Ultrasonic Range Sensor
Designed by Mark Little
 
The HC-SR04 Ultrasonic Range detector uses 40kHz ultrasonic sound to measure the distance between the sensor and an obstruction (or target) in front of it. Depending on the cross section of the target, the sensor is capable of measuring range between 2cm and 4 metres. This makes this sensor suitable for projects such as robotic collision avoidance, parking obstruction detection, object distance measurement and water level detection.
The sensor has a 4-pin connector with the following pin-out:
Pin Function
VCC Supply Voltage (+5V) to the Senor.
GND Ground or Earth connection for the supply voltage and the sensor output
Trig The Trig connection is an input that accepts a 10us TTL pulse to trigger the start of the measurement cycle.
Echo The Echo connection is an output pin that provides a TTL pulse whose length is proportional to the range of the object.
Because the Echo pulse starts with the Trig pulse and ends when the ultrasonic signal returns, its width is proportional to the times it takes the sound to travel twice the distance to the object (there and back. As the speed of sound is 340m/s, the distance is half that speed times the duration of the pulse. That is, if the pulse length is 20milliseconds, then the distance is 170 * 0.02, or 3.4 metres.

The minimum detection distance is 2cm, so that shortest pulse length will be about 117.6us, while at a distance of 4m, the pulse length will be about 23.5ms. These two measurements sets range of times that the microprocessor connected to the sensor must be able to accurately measure.

What the spread of times indicates is that the Trig repetition rate must be no less than 23.5ms,if the range is not known. Once the range has been measured, the re-triggering rate can be increased, without any range ambiguities being introduced (assuming that there are not multiple objects at different ranges).

Mounting

The number of mounting holes vary depending on the model of range sensor. Typical sensor boards have either two (2) mounting holes (see image above), or four (4) mounting holes, one at each corner. Because of the crystal mounted on the same size as the transducers, spacers will likely be required to stand the PCB away from the case and to keep the mounting firm.

Care should be taken when using these devices as they are not waterproof. This means that they are not suitable for outdoor use where they may be splashed with water, or subjected for fog or mist.