Projects / Speed of sound and temperature

Speed of sound and temperature

Intermediate2.3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Wire the sensor

    VCC and GND, Trig to D7, Echo to D6.

  2. NANODS18B2028.6°C

    Wire the probe

    Red to 5V, black to GND, yellow to D2, 4.7 kilo-ohms between D2 and 5V.

  3. NANO

    Wire the display

    5V or 3V3, GND, SDA to A4, SCL to A5.

  4. NANO18.2 mASSD1306

    Install the libraries

    OneWire, DallasTemperature, SSD1306 and GFX.

  5. NANO

    Measure the distance

    Tape from the sensor face to the wall, then set DISTANCE_M.

  6. NANO

    Upload and wait

    About four seconds per reading.

  7. NANO

    Compare

    Measured speed against the formula.

In short

How do you build a speed of sound and temperature?

An HC-SR04 measures the echo time to a wall at a known distance while a DS18B20 records the air temperature, and a Nano works out the speed of sound and compares it with the formula. You need Arduino Nano, HC-SR04 Ultrasonic Sensor, DS18B20 waterproof probe, Resistor 4.7 kΩ, SSD1306 OLED Display, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 7 steps and about 140 minutes.

Level
Intermediate
Time
About 140 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
HC-SR04 VCCNano 5VPower at 5 V
HC-SR04 GNDNano GNDCommon ground
HC-SR04 TrigNano D7Start a ping
HC-SR04 EchoNano D6Echo time
DS18B20 red (VCC)Nano 5VPower
DS18B20 black (GND)Nano GNDCommon ground
DS18B20 yellow (data)Nano D2One-wire data
4.7 kilo-ohm resistorBetween D2 and 5VPull-up for the data line
OLED VCCNano 5VPower (check your OLED accepts 5 V, otherwise use 3V3)
OLED GNDNano GNDCommon ground
OLED SDA and SCLNano A4 and A5I2C, address 0x3C

Code

Sound travels through air at about 331 metres per second at zero degrees, and a little faster as the air warms, by about 0.6 metres per second for each degree. This experiment measures it. The HC-SR04 sends out a burst of ultrasound and the program times how long the echo takes to come back from a flat wall a known distance away. The sound has gone there and back, so the speed is twice the distance divided by the time. The program averages thirty pings to reduce the scatter and compares the answer with the standard formula, using the air temperature from a DS18B20 probe. Measure the distance carefully with a tape, from the face of the sensor to the wall, and use a smooth hard wall at right angles to the sensor. The echo timer in the sensor adds a small delay of its own, so expect the result to be within a few percent of the formula and not exact. To see the effect of temperature, repeat the experiment in a warm room and in a cool one, with the probe in the air and not touching anything, and write the results in a table. Do not point the sensor at people's ears at close range for long.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const int TRIG = 7, ECHO = 6, ONE_WIRE = 2;
const float DISTANCE_M = 1.00;                     // measure from the sensor face to the wall with a tape

Adafruit_SSD1306 oled(128, 64, &Wire, -1);
OneWire oneWire(ONE_WIRE);
DallasTemperature probe(&oneWire);

unsigned long pingMicros() {
  digitalWrite(TRIG, LOW); delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  return pulseIn(ECHO, HIGH, 30000);               // round trip time in microseconds
}

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  Serial.begin(9600);
  probe.begin();
  Wire.begin();
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
}

void loop() {
  unsigned long sum = 0;
  int good = 0;
  for (int i = 0; i < 30; i++) {                   // average thirty pings
    unsigned long t = pingMicros();
    if (t > 0) { sum += t; good++; }
    delay(60);
  }
  probe.requestTemperatures();
  float tempC = probe.getTempCByIndex(0);

  if (good == 0 || tempC < -100) {
    oled.clearDisplay();
    oled.setCursor(0, 0);
    oled.print("No echo or probe");
    oled.display();
    delay(1000);
    return;
  }

  float seconds = (sum / (float)good) / 1000000.0;
  float measured = (2.0 * DISTANCE_M) / seconds;   // metres per second
  float formula = 331.3 + 0.606 * tempC;           // the standard approximation
  float diffPercent = (measured - formula) * 100.0 / formula;

  Serial.print(tempC); Serial.print(" C  measured "); Serial.print(measured);
  Serial.print("  formula "); Serial.println(formula);

  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  oled.setTextSize(1);
  oled.setCursor(0, 0);  oled.print("Air "); oled.print(tempC, 1); oled.print(" C");
  oled.setCursor(0, 14); oled.print("Measured "); oled.print(measured, 0); oled.print(" m/s");
  oled.setCursor(0, 28); oled.print("Formula  "); oled.print(formula, 0); oled.print(" m/s");
  oled.setCursor(0, 42); oled.print("Difference "); oled.print(diffPercent, 1); oled.print(" %");
  oled.display();
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

24.50 C  measured 428.88  formula 346.15
24.50 C  measured 428.75  formula 346.15

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
StartTemperature, measured speed, formula speed
Room temperatureBoth near 343 m/s
Change the distance in the sketch to a wrong valueThe difference grows
Warm the room a few degreesBoth rise a little
Aim at a soft curtainPoor or no echo

If it does not work

No echo or probe

Check the Trig and Echo wires and the probe wiring and pull-up.

Result is far off

Measure the distance again and set DISTANCE_M.

Numbers scatter

Use a flat wall, keep the sensor still and avoid clutter.

Probe reads minus 127

Check the three probe wires and the resistor.

Display blank

Check the OLED address 0x3C.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the speed of sound and temperature?

Arduino Nano, HC-SR04 Ultrasonic Sensor, DS18B20 waterproof probe, Resistor 4.7 kΩ, SSD1306 OLED Display, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points.

How long does the speed of sound and temperature take to build?

About 140 minutes for a intermediate maker, in 7 steps.

How do I know the speed of sound and temperature works?

Start: Temperature, measured speed, formula speed. Room temperature: Both near 343 m/s. Change the distance in the sketch to a wrong value: The difference grows.

No echo or probe: what should I check?

Check the Trig and Echo wires and the probe wiring and pull-up.

Result is far off: what should I check?

Measure the distance again and set DISTANCE_M.

Numbers scatter: what should I check?

Use a flat wall, keep the sensor still and avoid clutter.

Probe reads minus 127: what should I check?

Check the three probe wires and the resistor.