Arduino - HC-SR04 Ultrasonic Sensor

The HC-SR04 Ultrasonic Sensor is a device that uses ultrasonic sound to measure distances. When it works - it is very cool.

This page exists because it DOESN'T work .. without a little help :)

Background | Basic operation | NewPing Library | Failure | Solution | Documentation - Schematics Timing | References


Background

There are several common sonar devices used with Arduinos to measure distance. The example code, available from the Arduino IDE via is for the PING))) ultrasonic rangefinder - besides power and ground, it uses a single Arduino pin and changes it from input to output as required. While the Grove device has 4 pins, their library is similar to what is used for the PING))) 3-pin device.

Code for the HC-SR04 is similar, but uses 2 separate Arduino pins. For example

Unfortunately, there appear to be 2 different versions of the HC-SR04 They both have the same part number and the only way (that I know of) to tell them apart is to test them. (Well .. the price may be an indicator - perhaps the really cheap ones are being dumped for a reason .. or not. Who knows?) The point of this page is to explain this issue and to provide a "fix".


Basic operation

The HC-SR04 Ultrasonic Sensor (ranging module) connector has 4 pins

When a trigger is received, the HC-SR04 produces 8 pulses at 40 KHz and sets the echo output high. When an echo is received, the echo output goes low. Assuming that you know the speed of sound (which is affected by air pressure, temperature, and humidity), it is possible to use the length of time that the echo output is high to compute the distance to a reflecting surface.

Using the example software (linked to above), the distance to an object is displayed in the serial monitor. To display the monitor, from the Arduino IDE menu select

or click the magnifying glass icon in the upper right of the IDE.

The software displays the distance between the sensor and some object. Unfortunately, the values are not very accurate because both programs use integer math and do not provide a way to adjust the values for the local speed of sound. This is, of course, acceptable for examples, but is something you should be aware of so you can "fix" it for your application (as required, or not).


NewPing Library

The NewPing Library for Arduino uses separate trigger and echo pins (required for the HC-SR04). Instead of using pulseIn() to read the echo, the library uses timer interrupts and masked digital I/O (presumably to make the code faster). This procedure automatically places the provided examples under

To test a sensor

Note that the default comport baud rate is 115200 (the other programs use 9600) - if you see gibberish in the serial monitor, this is probably why.

Unfortunately, this program suffers from the same problem as the other example programs.


Failure

Nominally, the HC-SR04 works as expected. The problem occurs when no echo is received.

Using the example software linked to above, objects a foot or two away are easily detected. However, when you point the sensor at an object more than 9 or 10 feet away - the echo is simply so weak that it is not detected. In that case, the software displays zero (no signal detected). The same thing happens if you cover the transmitter with your hand (makes testing easier), or do something else to keep a reflection from being detected.

The problem occurs when you now place an object back within the range of the sensor - you still get zeroes. Just zeroes. Until you manually reset the sensor.

I originally thought that this was simply a defective sensor .. until an internet search revealed that it is a fairly common problem. The only suggested solution was to use a transistor (driven by the Arduino) to cycle power to the device - a kludge at best! But it does work.

Using an oscilloscope, I verified that the ping pulses were not being generated when the next trigger occurred. Apparently, with some devices, the next trigger resets the unit, a ping is sent, and the device continues to work normally (as advertised in the provided documentation). However, with the devices I bought, the next trigger does not reset the device. (Very frustrating!)

One way to "fix" the problem (and to verify that a ping pulse is not being generated) is to just snap your fingers. The receiver will detect the snap and finish its timeout (send the echo line LOW). After that, the sensor will work as expected until another pulse is not detected.


Solution

There are several "solutions" to this problem. Cycling power and snapping your fingers have already been mentioned. The simplest is to modify the software to To be explicit This code is modified from the examples


Documentation

This pdf provides a general description of the HC-SR04. However, the translation is pretty bad - for instance, What I think this really means is I have repeated that particular example here because I have seen it, copied exactly, on numerous ebay offers of the device. In addition, one way to "fix" the problem (as mentioned above) is to cycle the power (disconnect and then reconnect the power) to the device.


Schematics

I was able to find 2 reverse engineered schematics These are the main differences The heart of the sensor is the EM78P153 8-bit microprocessor.


Timing

Using a dual trace oscilloscope, I measured the following timing. To create a brighter scope trace,


References


Author: Robert Clemenzi
URL: http:// mc-computing.com / Hardware_Platforms / Arduino / HC-SR04_Ultrasonic_Sensor.html