Inertialess measurement of air temperature by ultrasound

Hi, Habr.
I love custom solutions. Now I will show how to measure air temperature using ultrasound.
The circuit is ugly simple - take an Arduino (I took Nano) and connect an ultrasonic range finder to it (pin 2 - Echo, pin 4 - Trig). I used US-020 as it is longer range and gives more stable readings than HC-SR04.

Fill the sketch:
#define Trig 4
#define Echo 2
#define Steps
static const float defDist = 173.2; // cm
static const float defTemp = 17.0; // Celsius
void setup ()
{
pinMode (Trig, OUTPUT);
pinMode (Echo, INPUT);
Serial.begin (57600);
}
unsigned long impulseTime = 0;
void loop ()
{
float dist = 0;
for (int i = 0; i <50; i ++)
{
float distance_sm = 0;
digitalWrite (Trig, HIGH);
delayMicroseconds (10);
digitalWrite (Trig, LOW);
impulseTime = pulseIn (Echo, HIGH);
distance_sm = float (impulseTime) /58.0;
dist + = distance_sm;
delay (50);
}
dist / = 50.0;
Serial.println ("Distance:" + String (dist));
float Speed_of_sound = defDist / dist * sqrt (1.4 * 287.0 * (273.15 + defTemp)); // c = sqrt (X * R * T)
Serial.println ("Temp:" + String ((Speed_of_sound * Speed_of_sound) / ( 1.4 * 287.0) - 273.15)); // T = (c * c) / (X * R) in Kelvin
}
#define Echo 2
#define Steps
static const float defDist = 173.2; // cm
static const float defTemp = 17.0; // Celsius
void setup ()
{
pinMode (Trig, OUTPUT);
pinMode (Echo, INPUT);
Serial.begin (57600);
}
unsigned long impulseTime = 0;
void loop ()
{
float dist = 0;
for (int i = 0; i <50; i ++)
{
float distance_sm = 0;
digitalWrite (Trig, HIGH);
delayMicroseconds (10);
digitalWrite (Trig, LOW);
impulseTime = pulseIn (Echo, HIGH);
distance_sm = float (impulseTime) /58.0;
dist + = distance_sm;
delay (50);
}
dist / = 50.0;
Serial.println ("Distance:" + String (dist));
float Speed_of_sound = defDist / dist * sqrt (1.4 * 287.0 * (273.15 + defTemp)); // c = sqrt (X * R * T)
Serial.println ("Temp:" + String ((Speed_of_sound * Speed_of_sound) / ( 1.4 * 287.0) - 273.15)); // T = (c * c) / (X * R) in Kelvin
}
We place the ultrasonic sonar in the direction of some obstacle at a distance of the order of two to three meters (less - worse accuracy, more - the sonar may not catch the echo) and securely fix. I have it from table to ceiling. We start, look at the distance in the serial port monitor. Edit the sketch - replace defDist and defTemp with your distance and current temperature. The starting temperature will have to be measured, or point to the eye (like me). Again, flashing.
That's it, in the port monitor we look at the temperature of the air volume between the sensor and the obstacle:

And now the explanation of "street magic". According to physics textbooks:
The speed of sound in gases depends only on temperature and does not depend on gas pressure.
And this dependence is expressed by the formula:
c = sqrt (X * R * T), where:
c is the speed of sound, m / s
X is the adiabatic index
R is the gas constant, J / kg · K
By measuring the response time of a sonar and comparing it with the default, you can easily calculate the speed of sound. And knowing the speed it is just as easy to calculate the temperature. For greater accuracy, we average the sensor readings over 50 measurements.
There is no inertial thermal element in the system that needs time to heat or cool to ambient temperature. Therefore, the inertia of the measurement is completely absent - if you ventilate the room, then the readings change very quickly. Accuracy is certainly not so hot - of the order of 0.5 degrees, but not template.