Monday, December 3, 2018

AVCS1: Sonic Range Finder (HC-SR04) discussion

Th HC-SR04 sonic range finding modules are a popular choice for projects which require sensing the distance to some object. I recently ordered a bunch of them to keep on hand, but I wanted to add one to the Infrared Light Goggle project I built previously. This addition will allow me to add the approximate distance to the HUD.

The Sensor

The HC-SR04 sensor produces 8 sonic pules at 40kHz (called a ping) and then measure the time it takes to receive back an echo of that ping. To figure out distance from time, you also need speed (or rate if you prefer) (d = r*t). In this case the speed of sound:
"The speed of sound is the distance traveled per unit time by a sound wave as it propagates through an elastic medium. At 20 °C (68 °F), the speed of sound in air is about 343 meters per second"
The HC-SR04 is actually This model claims to be accurate up to 400cm (about 13 feet) So let's suppose we send a ping that at 343m/s it will take about 0.0116 seconds to travel there and approximately the same to travel back for a round trip time of 0.0233 seconds. A ping is triggered by a 0.01 second HIGH pulse on the Trig pin. The Echo pin can then be read, the width of the incoming pulse can be used to calculate the total distance traveled and therefore the distance to the obstacle.
The Data sheet (https://www.mouser.com/ds/2/813/HCSR04-1022824.pdf) explains that the formulas used:
"uS / 58 = centimeters or uS / 148 =inch; or: the range = high level time * velocity (340M/S) / 2;"
Where uS is the microseconds the Echo Pin was held HIGH for. The manufacturer also suggest using over 60ms between measurement cycles in order to prevent triggering to a previously echoed signal. 

The Hookup

This part is really easy. The pins are 5v tolerant but will work with the Pi's 3.3v as well. Both the trigger and the echo pins can be hooked to any GPI/O pin, as long as you can set them in your code as a Digital OUTPUT and a Digital INPUT respectively. This means even a small device, like the Arduino Nano, can support multiple HC-SR04 sensors. I am no PCB designer but I threw together a board for a multi-sonar example project anyway. It is based on the Arduino Nano v3 controller and 4 HC-SR04 sensors.
Click the picture to see the board files.

The Code 

I have written two versions of code demonstrating this sensor. The first is for the Raspberry Pi, using the RPi.GPIO library. The pulse timing is being handled by Python's time library. Most of the functions defined in this module call platform C library functions with the same name. That translates may add some inaccuracy to the results depending on the resolution of the clock source used. You can find out more about the time function and it's limits in the docs.
HC-SR04 Sensor on Raspberry Pi.

I reworked the Arduino example to account for multiple sensors attached to the device. This could be useful if you want to build a multi-directional sonar, without the need to physically rotate the sensor. 
HC-SR04 Sensor on Arduino Nano.
Using the sketch above and modifying the pin assignments to match the PCB above, you can measure distance to objects across two complete axis (X and Y). You can use this to determine the boards distance to an obstacle, or determine the distance between two obstacles along the same axis. The board is around 4 inches long by about 2 inches wide. Using our previous maximum sensing distance of 400cm (about 13 ft), this board is able to sense a distance between object up to 800cm (about 26.25 ft). Add in the 10cm between sensors 1 & 2 or 5cm between sensors 3 & 4 for even more accurate measurements.

Conclusion

There are several bonuses to using sonic range finders over other range sensing equipment. They are not susceptible to bright lighting issues that inexpensive IR sensors often suffer. They perform well across a range of environmental conditions, are inexpensive, and durable enough to be used in more rugged applications than laser range finders. Now I will go over the drawbacks. Keep in mind some of these may not be unique to the HC-SR04 necessarily, but represent the challenges this class of sensing faces as a whole.
First, the effective angle is listed as <15 degrees, meaning the object you are detecting needs to be less that +/- 15 degrees from the center-line of the HC-SR04. I pulled this image from the Datasheet which show the decrees in effective sensing ranges.
Second the size, shape, and construction of the object can be an issue. Ideally you'd always be trying to sense a smooth flat surface, but in the real world that is not the case. Since the sensor relies on the reflection (or echo) of sound, any materials that absorb or muffle sound waves will be harder (or in some cases impossible) to detect. Similarly anything that refracts or redirects sound waves will cause inaccurate readings.
Reflected wave misses the sensor. The obstacle is invisible.
This issue can manifest in another interesting way in spaces with a lot of echo (signal feedback). I noticed this when using multiple sensors. The Ping from the first sensor would propagate off of multiple surfaces and reach the second sensor before it's own ping could. When the board would try to read the second sensor, the echo would be enough to cause an incorrect distance calculation (shorter than actual). Finally, there are the inherent speed and resolution drawbacks when compared to light-based systems like IR or laser.

Overall these little sensors are good to keep on hand. They are accurate enough for most collision avoidance, or proximity sensing needs. They would be great in any non-critical capacity. As I mentioned at the start, I added one of these sensors to the IRG setup I wrote about previously. The distance is placed on the HUD using a data pipe file.

No comments:

Post a Comment