Zonnestroompanelen in Nederland

duurzaamheid achter de meter

(33) Arduino Nano – HC-SR04 distance meter with temperature correction

by Floris Wouterlood – June 4, 2022

Summary
A HC-SR04 ulltrasound distance meter is here connected to an Arduino Nano. A 20×4 liquid crystal display provides visual output. To increase accuracy air temperature data acquired with a DS18B20 sensor is used to calculate the exact distance between sensor and object.

Introduction
Distance meters are sensors typically applied in robots crawling around on the ground. Many educational ’roaming’ robot devices are equipped with an Arduino microprocessor as their ‘brain’. This equipment must avoid obstacles and for that purpose carry sensors that supply input about objects in their path. A sensor that measures distance on a scale and economy interesting for Arduino aficionados is the HC-SR04. This device combines a transmitter and a receiver, hence its two ‘eyes’.

figure 1. Working HC-SR04 ultrasound distance sensor, Dallas DS18B20 temperature sensor, Arduino Nano and (parallel) 20×4 LCD display. The temperature sensor is mounted as a companion to the HC-SR04 (indicated with red circle).

The transmitter emits bursts of ultrasonic sound: trains of 8 pulses at 40 kHz. The receiver picks up the sound that is bounced back by an obstacle. Compare this mechanism with the classical sonar ‘pinging’ in movies featuring WWII submarines being hunted by destroyers. Detection range of the HC-SR04 is 2-400 centimeters, with a reported accuracy of 3 mm.

Pinout of a HC-SR04
A HC-SR04 has four pins labeled VCC, Trig, Echo and GND. VCC is connected to the 5V pin of an Arduino, and GND to GND. The Trig and Echo pins can be connected to any digital output pin of an Arduino. Here we are using an Arduino Nano. Regardless of the pin requirements for the display, which in this example is of the parallel type that occupies 10 pins, there is a range of input-output pins to choose from. I selected in this project pins D9 and D10 for Trig and Echo, respectively (figure 2).

 

Figure 2. Wiring diagram: Arduino Nano, HC-SR04, DS18B20 temperature sensor and parallel LCD.

A HC-SR04 can be activated by setting the Trig pin HIGH for at least 10 msec. The device then emits a compact pulse train of 8 pulses at 40 kHz. As Trig is activated, Echo is set HIGH by the HC-SR04. If no (reflected) ultrasound is detected in a time slot at Echo of 28 msec then the internal decision is made that there are no obstructions, and Echo gets LOW. The only thing that the Arduino has to do is to trigger the HC-SR04 via the Trig wire and to check activity at the Echo pin. If Echo ‘hears’ the 8-pulse sound train there has been reflection and the duration in milliseconds between trigger and reception is recorded. Distance can simply be calculated by multiplying speed with half the time time elapsed between triggering Trig and detecting the bounced-back pulse train at Echo. The ‘half’ is necessary by the circumstance that the received sound burst has traveled forth and back the distance between the sensor and the object.

Speed of Sound
What we consider ‘sound’ is a standing energy wave within a certain frequency band that travels through air, water or another substance. ‘Listening’ to sound means that our brain recognizes patterns of electric activity of hair cells located in the organ of Corti in our inner ear. Because we are dealing with a standing wave, sound has three major components: frequency, amplitude and medium. The HC-SR04 uses a fixed freqency (40 kHz) and a fixed amplitude. It is the medium that matters most. In air at 20 oC and at sea level sound travels with a speed of 343 meters per second, or 1.192 km/hr. In non-air media different speeds are measured. For instance, in water sound travels at 1, 481 m/sec. A good rule of thumb is that the denser the medium, the faster the speed. One would expect therefore that in air the speed of sound decreases with altitude. Surprisingly this is not the case; the speed of sound is fairly constant whatever altitude one chooses (except outer space where there are no air molecules around to support a standing wave). The cause for this remarkable phenomenon is that air pressure has an inverse effect on the speed of sound while the density of the air favors it. Both effects are in constant equilibrium when altitude above sea level changes. Thus: sound travels equally fast for visitors of the Dead Sea (300 m below sea level) as for mountain climbers yelling out when they reach the summit of Mount Everest (8,888 m above sea level). However, there exists a variable that has much more effect than altitude: temperature. At the summit of Mt. Everest air is a lot colder than at the shores of the Dead Sea. At these extreme spots the on-site temperature of the air really matters. In equation (and at 20 oC air temperature):
Vsound = 343 + 0,606*T where T is the ambient temperature,
which implies that sound projects faster over the hot brine of the Dead Sea than through the freezing air at Mt. Everest.

Distance between HC-SR04 and object at different temperatures
Assume that an object is 34.3 cm away from the sensor. If we do not take temperature into consideration, the HC-SR04’s ultrasound pulse needs 10 msec to reach the object and another 10 msec to travel back to the sensor. If we raise the temperature of the air with 10 degrees Celsius then the speed of sound becomes 346 m/sec and the pulse needs 9.8 msec to travel the distance, resulting in a 2% deviation.

Assume an air temperature of 20 oC.

A 0.56 centimeter difference! Also, this difference may vary during different times of the day as the temperature varies. For that matter I added a temperature sensor to the HC-SR04 (figure 3). A perfect sensor for this purpose is the well known Dallas DS18B20*.

Figure 3. HC-SR04, DS18B20 temperature sensor mounted on a breadboard.

 

 

figure 4. Output for the HC-SR04 application, with temperature correction

The data pin of the DS18B20 is connected here to pin D8 of the Nano. A 4.7 kΩ pull resistor is placed between the sensor’s data wire and 5V to provide pull up resistance. The LED in Figure 2 is optional.

Sketch
A sketch that works together with the equipment wired in Figure 2 is supplied here. Output is as in Figure 3.

Download
Nano_ultrasonic_HC-SR04_DS18B20_20x4_LCD.ino (compressed as ZIP).

References
* The DS18B20 temperature sensor – implementation with an Arduino. Floris Wouterlood – TheSolarUniverse, August 17, 2017