Skip to content
Home » Embedded Systems » Sensors » How to improve readings of Ultrasonic Sensor – Temperature and Humidity Compensation

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

Arduino Uno breadboard wiring diagram connecting HC-SR04 ultrasonic sensor and DHT11 temperature humidity sensor for temperature-compensated distance measurement

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
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 sensorfigure 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 isV= [331 + 0.6 * T ]…… equation (2)                                                                                                                                V= speed of the sound wave in meters/secondT=Temperature in degreesUsing 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

Flowchart for temperature-compensated ultrasonic distance measurement showing three steps: read temperature, calculate speed of sound using V equals 331 plus 0.6 times T, then calculate distance using speed times time divided by 2

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 formulaFahrenheit to Celsius conversion formula C equals F minus 32 multiplied by 5 divided by 9, used for temperature compensation in ultrasonic sensor distance calculationswhereC = temperature in celsius andF = temperature in fahrenheit& relative humidity will be measured by the sensor

Flowchart of calculation of distance when temperature and humidity changes 

Flowchart for temperature and humidity compensated ultrasonic distance measurement showing three steps: read temperature and humidity from sensor, calculate velocity using formula 331.4 plus 0.6 times temperature plus 0.01243 times relative humidity, then calculate distance 

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 
Arduino Uno breadboard wiring diagram connecting HC-SR04 ultrasonic sensor and DHT11 temperature humidity sensor for temperature-compensated distance measurement
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
   delay(100);
}
 

Leave a Reply

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