The HC-SR04 is a small low-cost ultrasonic sensor that can measure distance from 2cm up to 4 meters with a stable performance and high ranging accuracy under a 15¡ã detection angle and in an easy-to-use package. This sensor is very easy to interface with the Arduinoboard or any other microcontroller.
As any ultrasound sensor, the HC-SR04 operates like a simple radar, by sending a series of pulse waves and listen to an echo. To measure a distance, start by sending a short trigger pulse to the TRIG pin, at the same time monitor the ECHO pin until the output is HIGH and measure the pulse width.
The distance is equal to (pulse duration in seconds) * (speed of sound) / 2 where the pulse width is in unit of second and sound speed is in unit of meter/second. Normally sound speed is 340m/s under room temperature.
To use this sensor with the Arduino simply download the srf04-library if you're using the Arduino IDE 1.0 or the Ultrasound Library if your IDE version is older. We recommend a pause of 60-100ms between each distance reading to allow the echo pulse enough time to settle.
Note: The HC-SR04 is not temperature compensated, because the speed of sound varies with the room temperature you might get different result for the same distance when measuring at different room temperatures. If you need more precision we recommend theMaxbotix Ultrasound Sensors.