Echolocation with HC-SR04 ultrasonic distance sensor

Ultrasonic echolocation is common practice in many areas including nature itself. We all know how bats or dolphins navigate – they emit ultrasonic waves and depending on time delay of reflected echoes, they can determine the distance from obstacles or catch. From engineering perspective it is relatively easy to measure distances with ultrasound – all you need to know is sound speed in the air and time delay between sent and received pulses.

ultrasonic echolocation_principle

(Source: http://www.analog.com/)

 L = C_{AIR}\cdot \frac{t}{2}

L is the measured distance; CAIR – ultrasound speed; t – time between transmitted and received pulses.

The accuracy of measured distance mainly depends on air temperature. Approximately sound speed in air can be calculated by following formula:

 C_{AIR} = 20.0457\cdot \sqrt{273.15+T} (m/s)

For instance at 25ºC the speed of sound in dry air CAIR = 346.13 m/s. So if you want greater accuracy, you should also measure temperature to adjust sound speed.

There are several cheap ultrasonic echolocation modules available, but probably most known and used is HC-SR04. You can find lots of projects built around them including distance meters, robots, radars, thickness, and security barrier, parking and liquid level. These modules have pretty good characteristics that make them so popular:

Working Voltage DC 5V
Working Current 15mA
Working Frequency 40 kHz
Max Range 4m
Min Range 2cm
Measuring Angle 15 degree
Measuring accuracy ±3mm
Trigger Input Signal 10µS TTL pulse
Dimension 45 x 20 x 15mm

Interfacing module is straight forward – it comes with only four pins. Two of them are power (VCC and GND), one pin is Trigger pin and one Echo pin. The hard work of transmitting and receiving signals is done by module itself. In order to perform basic measurement send at least 10us pulse to Trigger pin and wait for rising edge signal on Echo pin. Once raising edge is detected start microcontroller counter and wait for falling edge on Echo pin. Pulse on Echo is the time between transmit and receive signals. Having time delay you can calculate the distance from obstacle by formula above.

Running HC-SR04 with Arduino and LCD keypad shield

Let’s build a prototype using Arduino, LCD keypad shield and HC-SR04. The distance will be displayed on LCD. We will use a button to implement couple different functionalities. Also it will be interesting to see how accurate the measured distance is comparing with tape meter. LCD keypad shield is connected to Arduino as reviewed previously here. So it occupies pins 8, 9, 4, 5, 6, 7, 10 (backlight) and analog 0 for buttons. So we are left with 1 to 5 analog pins and 0, 1, 2, 3, 11, 12, 13 digital. We will leave 0 and 1 digital pins for serial output and use digital pin 2 for HC-SR04 trigger and digital 3 for Echo. Also we need to connect VCC and GND.

Additionally we want to monitor environment temperature to calculate sound speed in order to achieve better accuracy. So for this we are going to use DS18D20 where sense pin is connected to A1 pin of Arduino pulled up to VCC with 4.7k resistor.

echosketch_schem

Programming Arduino is fairly easy, especially when special libraries are used. First of all lets deal with temperature measurement. In order to read temperature value, we need two libraries: OneWire and DallasTemperature. Using them life become much easier as you only need to initialize temperature sensor, assign pin and read its value.

Same situation is with LCD keypad. The LCD is controlled with standard Arduino library and no additional libraries are required. If you need to work with buttons that are read with single analog pin, then you should get LCDKeypad.zip library which makes key reading more robust. We are not going to analyze them more deeply, the working is obvious in source code.

We have used temperature to calculate sound speed in the air. A simple empirical formula is used to get speed values in cm/us:

sound_speed = 20.0457*sqrt(273.15+temp1)/10000.0; //cm/us

Let’s pay a bit attention on how we can read distance out of HC-SR04 sensor. We have assigned Arduino pin 2 for Trigger and pin 3 for Echo signals. So first of all we need to get pulse time information which indicates the duration of ultrasonic pulse flight and return. There is a simple routine to trigger sensor and get time value in microseconds:

 

digitalWrite(Trigger, LOW);

delayMicroseconds(5);

digitalWrite(Trigger, HIGH);

delayMicroseconds(10);

digitalWrite(Trigger, LOW);

First of all we give short 5us LOW pulse before sending clean high pulse. High trigger pulse needs to be at least 10us. When trigger pulse drops LOW, sensor sends eight 40kHz square pulses. At same time Echo pin becomes HIGH until bounced back signals are received. Arduino needs to capture the duration of HIGH state of Echo pin. Luckily Arduino comes with handy function

pulselength =  pulseIn(Echo, HIGH); //microseconds

Which returns time duration in microseconds of pin state high.

Once we have time pulse duration and sound speed we can calculate the distance using simple formula:

 

//centimeters

distancecm = sound_speed *  (pulselength/2);

//inches

distancein = distancecm * 0.39370;

SR04_temperature_and_distance

With keypad buttons UP and DOWN we can change centimeters units to inches. With LEFT and RIGHT buttons we can switch secondary screen where pulse time and sound speed values are visible.

SR04_pulse_and_sound_speed

And with SELECT button we can call REL function which allows measuring relative distances from position where button was pressed.

SR04_relative

To evaluate measuring accuracy, I have constructed simple rig with measuring tape.

ruller_compare_setup

I didn’t do any statistical analysis, just a simple comparison of two values. As you can see with tape meter I get 39.7cm while with SR04 sensor 38.9. The difference is 0.8cm.

SR04_accuracy_test

Practically we cannot expect greater accuracy without additional measures. We have calculated sound speed by taking only temperature into account. But there are other factors that influence sound speed – air density which in it hand depends on altitude. Also there is an influence of sensor position and measured surface properties temperature measurement accuracy. In my opinion 0.3cm accuracy can only be reached in controlled environment where sound of speed is known for real or is directly measured.

Download source code (echolocator.zip) to give it a try.

Leave a Reply

Your email address will not be published. Required fields are marked *