Home / Tutorials / Arduino Tutorial / Ultrasonic Sensor and Temperature Compensation
Ultrasonic sensor with temperature compensation

Ultrasonic Sensor and Temperature Compensation

If you've been using Arduino, you might have used or have heard about the ultrasonic sensor. Most of us use it for level/distance measurement while some use it to detect an obstacle in front. What most of us don’t know is that there is an underlying flaw with the ultrasonic sensor, particularly on the first application I mentioned (level/distance measurement).

The ultrasonic sensor is able to detect an object in front of it by emitting an ultrasound and then waiting for that ultrasound to bounce back. Naturally, the further the object, the longer the “echo” to arrive.

How ultrasonic sensors work

Most tutorials (including my own), uses this formula to determine the distance of the obstacle:

Here, 0.034 is the speed of sound in centimeters per microsecond. So when the “echo” returns in 100 microseconds (t in the formula), the distance of the obstacle that bounced the ultrasound is 1.7 centimeters.

Speed of Sound and Temperature

The error lies in the value of the speed of sound because this value is actually temperature dependent. In fact, the practical formula of the speed of sound is:

Where T is the temperature of the surrounding in degree Celsius. Moreover, the usual speed of 0.034 cm/us or 340 m/s, using this formula, only works when the temperature is 14.35 °C!

Now let’s see the measurement difference between the two formulas. The old one, as mentioned, detects the obstacle to be at 17 centimeters. For the new formula, I will be using today’s temperature which is 33 °C (yeah it’s hot here). This gives me a speed of sound value of

Consequently, the distance of the object would be:

A 0.56 centimeter difference! Also, this difference may vary during different times of the day as the temperature varies.

Adding Temperature Compensation

So what I did is install a temperature sensor with my ultrasonic sensor to compensate for any temperature difference. Here’s my setup:

Ultrasonic sensor with temperature compensation

I used a DHT11 to measure the ambient temperature and to compensate for the temperature effects to the ultrasonic sensor.

The sketch for this project is down below. For those looking to replicate this project, you need to download the Adafruit Unified Sensor and DHT libraries.

Naturally, I had to test this sketch. Here’s a picture of my actual setup:

Ultrasonic sensor with temperature compensation actual setup

The distance from the sensor to the obstacle is around 5.4 centimeters (as shown by the ruler). Here are the results if there's no temperature compensation:

Output without compensation

The sensor reads a distance of around 5.9 centimeters, and 0.5 centimeter difference. Now here’s the results with temperature compensation:

Output with compensation

The sensor now reads a distance of around 5.4 centimeters!

Check Also

arduino current sensor

Arduino Current Sensor Tutorial

Current sensors can have different applications: power supplies, over-current protection, and motor control to name …

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.