The HC-SR04 is an excellent low-cost ultrasonic sensor that works really well with Arduino micro-controllers. The quality of these sensors does vary depending on where you buy them from, so we recommend sticking with reputable suppliers. We have some for sale over in our store.
These sensors are very easy to use. First connect the VCC and GND pins to the Arduino’s +5V and GND pins. Next connect the Trigger and Echo pins to two digital pins on the Arduino, perhaps pins 2 and 3 for example.
Setting the Trigger Pin to HIGH for 10 microseconds will activate a measurement reading and then the pulseIn() function can be called on the Echo Pin to determine the distance. The length of the incoming pulse is proportional to the distance measured.
Here is some simple example code to demonstrate the usage:
pinMode(triggerPin, OUTPUT); pinMode(echoPin, INPUT); digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW); int distance = pulseIn(echoPin, HIGH) / 2;
This code returns the raw measurement of the time for the sound to reach the object. Sound travels at approximately 340 meters per second at sea level. Therefore, the length of time it takes sound to travel 1 meter is 1/340 which is 0.0029 seconds. To calculate the length of time for sound to travel 1 centimeter, we divide this by 100 to get 0.000029 seconds, or 29 microseconds.
So to convert the reading from the sensor to centimeters, we divide by 29 (or to convert to inches, divide by 74, since 1 inch = 2.54 cm).
int distance_cm = distance / 29; int distance_inches = distance / 74;
For accurate readings, it is recommended to leave some time in between measurements, as the sound waves could be echoing around for a while. A good rule of thumb is to sleep for 50 milliseconds between readings but you may want to adjust this based on your requirements.
Another good tip is to specify a timeout when calling pulseIn() to avoid waiting a long time for a signal that never arrives. If you are only interested in measuring distances up to 100 cm for instance then you could specify a timeout of 5,800 microseconds (100 * 29 * 2).
int distance = pulseIn(echoPin, HIGH, 5800) / 2;
As good as these sensors are, they cannot defy the laws of physics and results will vary greatly depending on the object being measured and the angle between the sensor and the object.
Some smooth objects (like glass) at a 45 degree angle from the sensor may not return an accurate signal, and even worse could bounce the signal of some other object, giving a false reading. Objects made from less smooth materials will usually return some signal.
For the best results, it is recommended to have multiple sensors at different angles around the robot. We sell packs of these sensors at a low cost in our store, making it very affordable to have multiple sensors.
Also, it is good to have a backup plan if obstacle avoidance fails. It is always a good idea to be able to detect if the robot has hit a wall, perhaps using physical sensors, such as microswitches.