Table of Contents
KEY TAKEAWAYS
- Temperature and humidity affect the speed of sound, which directly impacts distance calculations
- Compensate readings by calculating the actual speed of sound based on ambient temperature
- Taking multiple readings and averaging them reduces noise and improves measurement reliability
- The speed of sound increases with temperature: ~331 m/s at 0°C to ~343 m/s at 20°C
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:-- Temperature
- 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
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 varyingVelocity = 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
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 |

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
delay(100);
} 
Vivek Bhageria — Lead Firmware R&D Engineer, 12+ years. Ex-Bosch (automotive powertrain), MusicTribe (real-time audio), medical devices. M.Tech BITS Pilani. I write at NerdyElectronics — practical, register-level embedded systems for engineers who want to understand what’s actually happening under the hood.




