How to improve readings of Ultrasonic Sensor – Temperature and Humidity Compensation

In our previous post of ultrasonic sensor we understood the working ,applications ,advantages as well as disadvantage. As we know already, in ultrasonic sensor ultrasonic waves propagates from the transmitter site and once encounters an object it returns back at the receiver site of the sensor. Here, the sound waves are propagating in the air media with a specific temperature and humidity.

However, if the temperature or humidity changes, the speed also changes. This causes the readings of the distance to be different in different temperature and humidity settings. In this post, we will see how to improve readings of Ultrasonic Sensor. We will understand the effect of temperature and humidity on the speed of sound and also see how to compensate for those effects to get accurate and reliable readings.

Effects of Temperature and Humidity on speed of sound waves:-

Speed of Sound waves can be effected in various ways but here we will mainly talk about two major factors. They are:-

  1. Temperature
  2. Humidity

In varying temperature :-

    • Temperature increases ⇒ Kinetic energy of the molecules increases ⇒ sound velocity of the media increases
    • Temperature decreases ⇒ Kinetic energy of the molecules decreases ⇒ sound velocity of the media decreases
    • kinetic energy of the sound increase in the form of vibration that is,when more kinetic energy more no of two and fro motion occurs between the molecules of the media

In varying humidity :-

  • More water vapour in air thus more no of molecules in air and therefore better and faster propagation of sound waves

∴When humidity increases  velocity of the sound wave increases                                  

and, similarly when humidity decreases ⇒  Velocity of the sound wave decreases  

Calculation of sound speed in varying temperature :-

Previously in our post we have introduced a formula that is mentioned below to measure the distance of the object from the ultrasonic sensor

figure 2   …..equation (1)

here we can see that the speed of the sound is fixed that is 340m/sec ,but now we want to introduce a formula where speed of the sound can be calculate with respect to varied temperature that is

V= [331 + 0.6 * T ]…… equation (2)                                                                                                                               

V= speed of the sound wave in meters/second

T=Temperature in degrees

Using this formula we can  calculate the value of the speed of sound to measure the distance in ultrasonic sensor

Flowchart of calculation of distance when temperature is varying

Calculation of speed of sound in varying temperature and humidity

As per earlier we have seen that in ultrasonic sensor speed of the sound wave changes when the temperature and humidity changes and we have introduced an equation that calculates the speed of the sound when  temperature changes but now,lets learn another formula which can calculate the speed of the sound when both temperature and humidity is  varying

Velocity = 331.4 + 0.6 * temperature +  0.01243 * relative humidity……..equation(3)

temperature should be in celsius degree. So, to convert fahrenheit to celsius use the following formula


C = temperature in celsius and

F = temperature in fahrenheit

& relative humidity will be measured by the sensor

Flowchart of calculation of distance when temperature and humidity changes 


Improve readings of Ultrasonic Sensor – Temperature and Humidity Compensation

  • To overcome the problem of the speed of the soundwaves due to temperature and humidity we can add a DHT11 or DHT22 temperature and humidity detection sensor
  • This will allow us to calculate the speed of the sound in real time which can increase the accuracy and thereby increases the effectiveness of the ultrasonic sensor  HC-SR04 module
  • here we will use DHT11 sensor and to interface it with a breakout board we will connect it with 3pins i.e.
     DHT11      Arduino
    VCC(+)     5volt
    signal(s)    pin (4)
    GND(-)    ground






Diagram below explains the interfacing of DHT11 with Arduino  board


HC-SR04 with Arduino and DHT11 wiring diagram

Source code to interface DHT11 with Arduino with temperature compensation


#include <Adafruit_Sensor.h> // Including Adafruit sensor library:
#include <DHT.h> //including DHT-sensor-library
#define trigPin 2       //defining the trigger pin (pin no-02)
#define echoPin 3       //defining the echo pin (pin no-03)
#define DHTPin 4       //defining the sensor pin (pin no-04)
#define DHTType DHT11  // As we are using DHT11 sensor thus Defining DHT sensor type
long duration;         //defining variable duration which will hold the length of the soundwave
int distance;          //defining distance which will store the data of distance of the object
float speedofsound;    //defining speed of sound it will store the calculated speed of sound
DHT dht = DHT(DHTPin,DHTType);// Create a DHT sensor object i.e. dht
void setup() {
  pinMode(trigPin, OUTPUT);//declaring trigger pin as the output pin  
  pinMode(echoPin, INPUT); //declaring echo pin as the input pin 
  dht.begin(); // Begins the Serial communication so that the Arduino can send out commands through USB connection
  Serial.begin(9600); // Starts the serial communication with the baud rate i.e.9600 so that data can be shared faster
void loop() {
  digitalWrite(trigPin, LOW);//setting trigger pin in LOW state
  delayMicroseconds(5);      //for 5 microseconds
  digitalWrite(trigPin, HIGH);  //setting trigger pin in HIGH state
  delayMicroseconds(10);       //for 10 microsecond and emits 8pulses from the transmitter and bounces back onces hit by an object
  digitalWrite(trigPin, LOW);  //after recieving the signal,setting the trigger pin to LOW state
  duration = pulseIn(echoPin, HIGH); //duration is a variable that starts timing once echo pin is high by using  arduino  function pulseIn()
  float temperature = dht.readTemperature();// temperature is the variable use to store the value of current temperature which sensors detects
   speedofsound = 331.3+(0.606*temperature);// Calculate speed of sound by using the given formula in m/s:
   distance = duration*(speedofsound/10000)/2; // Calculate the distance by using the given formula in cm:
   Serial.print("Temperature = ");
   Serial.print(temperature);     // Print the temperature which has been measured by DHT11 sensor on the Serial Monitor:
   Serial.print(" Celsius");      // print temperature in celcius
   Serial.print(", Distance = "); 
   Serial.print(distance);        // Print the distance calculated by the formula on the Serial Monitor:
   Serial.println("cm");           //pint the distance in unit of centimeters


Leave a Reply

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