LESSON 17: Measuring the Speed of Sound with Arduino and Ultrasonic Sensor

Now that we know the basics of Arduino from the first 15 lessons,  we can begin to focus on more and more cool projects.

Arduino Ultrasonic Circuit
Simple Circuit for measuring the speed of sound

I think you will be surprised to see how many sophisticated things you can do with the simple arduino skills you have already learned. In this project, we are going to measure the speed of sound using the arduino and an ultrasonic sensor. We will use the Virtuabotix ultrasonic sensor which you can get HERE for about nine bucks. This sensor measures the time it takes an ultrasonic ping to go out,  bounce off a target, and come back. The time it takes for the ping to leave and come back to the sensor depends on the speed of sound and the distance to the target. This sensor works in the same way a bat uses high pitched tones to navigate in the dark. It is also the same principle used in submarine sonar. The Arduino circuit for this project is very simple, and shown in the following schematic:

Ultrasonic Sensor Circuit
Simple Circuit for Measuring Speed of Sound

This sensor is fairly easy to use. To hook it up, we take the sensor VCC pin and hook it to the arduino 5V pin. We take the sensor GND and connect to Arduino GND. The Trig pin on the sensor we take to pin 13 on the arduino and the Echo pin on the sensor we connect to the arduino pin 11.

The sensor works as follows.  You take the trigger pin LOW with a digital write. You then pause, take the trigger pin HIGH, pause, and then take the trigger pen LOW again.  This LOW-HIGH-LOW sequence creates a high pitched ultrasonic tone, or ping, which is sent out from the sensor. This ping will go out and bounce off the first thing in front of it, and bounce back to the sensor. The sensor will output a HIGH pulse on its echo pin, and the length of the pulse in microseconds indicates the time it took the ping to travel to the target and return. We can measure the length of this pulse using the pulseIn command we learned in lesson 15.

Once you make the pulseIn measurement, and know the time it took for the ping to travel to the target and return, you can use that to calculate the speed of sound. Since

distance = rate * time.

Distance is the distance traveled by the ping, time is how long it took the ping to travel to the target and return. With this we can rearrange the equation to solve for rate, which would be the speed of sound:

rate = time/distance

Since pulse in returns the ping travel time in microseconds, and if you measure the distance to the target in inches, the units on the rate will be in inches per microsecond. We would need to use dimensional analysis to convert this to miles per hour as follows:

(rate in inches/mircrosecond)*(1000000 microsecond/second)*

(3600 seconds/hour)*(1 mile/63360 inches)

This will then give rate in miles per hour, as you can see as the units cancel out. The video explains this in more detail.

Key thing to remember in doing this calculation is that the ping has to travel to the target and then return. If the target is 6 inches from the sensor, how far did the ping travel? It traveled 12 inches. Six to get to the target and six to return from the target.

Try and put it all together into a program that will measure the speed of sound. I have included the code below. Try and write the program yourself, but if you get stuck you can look at the code below. It is important not to copy and paste this code, but only use it as a guide. You need to type your programs in yourself.

Check this out and see how close it is to published values for the speed of sound. Pretty Cool!

34 thoughts on “LESSON 17: Measuring the Speed of Sound with Arduino and Ultrasonic Sensor”

    1. Measure the ping time for a ping to travel to and from a target. Now know that distance=rate*time. Ping time gives you time, distance you can measure with a ruler, and now solve for rate, which will be the speed of sound.

      1. What i dont understend is why longer ping time means longer distance, on pulsein(v,high) definiton i read it measure the time the pin is on the next time it is turned on after is called so if you call the funtion when its off it has to mesure in the next loop, why it change with distance if is the same pulse length from the triger.

  1. Thank you very very much for your lesson Mr. Walter
    what i am thinking that if we can measure the speed of sound
    so with this same little sensor we can measure the temperature in air as the time in microseconds is know and we also know the distance in air on different temperatures, if we set ultrasonic sensor in very precise known distance ..
    am i right ?
    thank you very much again for your Lessons
    Tanveer Ahmed

  2. Hello Paul,
    Thank you so much for all your video-lessons about programming the Arduino. I’m a retired electronic engineer of 64 (worked for 40 years for the Royal Dutch Navy in radio and all the communicationstuff on the ships )and started in januari 2014 with Arduino, to make a controlbox for my Yaesu FT950, but programming in C was a problem for me. I did some Visual Basic and PIC-basic projects, but it came never to a good end 🙁 . So with Arduino it seems to go to that same direction, till I find your very good lessons. First I vieuw a complete lesson, then I make notes and write down the code while I listening to your explenation, and at last I type down the code in the ArduinoIDE and loading it up to my Arduino, and experiment with different for loops etc etc. I learned in a few weeks more from your lessons then the last year, en looking forwards to start every new lessons in combination with some electronics. Myself starting 2 new projects with Arduino, a waterdropcontroller and a movement controller to control my DSLR for my photography-hobby. I hope you have some lessons to make my own functions for Arduino, because that makes programming a lot compacter. The way you explain the subjects are so good to understand, and giving my understanding about programming a boost. Thanks again Paul, for all you effort and time to making those video-lessons for us.

    my regards

    Jan PA2JJB Texel-Island

    1. Stan,

      To incorporate temperature into the speed of sound you’ll need to do a few experiments. The speed of sound changes with the temperature of the air it’s passing through. You need to know what reading you are getting for the speed of sound at different temperatures.

      I would build something that will hold the sensor at a fixed (and precisely known) distance from the object the sound is bouncing off of. You could assume the relation between the speed of sound and temperature is linear. If you do you only need two points. The math to get an equation for the relationship then becomes exactly the same as the math for the color sensor on a previous lesson.

      You could verify it is linear by getting a lot of different temperatures and speeds. If the make what looks like an almost straight line, then it’s linear. If it isn’t linear then you can put all of the temperatures and speeds you get into a spreadsheet and do a regression. I hope this helps.


      If you want to measure the speed of an object, you could try measuring the rate of change in the measured distances between two pulses.

      You’ll have to already know the speed of sound (not hard to do if you followed the lesson). Make one pulse, measure the distance and save it to a variable. Then take another pulse, measure the distance and save it to another variable. Sense you coded how far apart the pulses are and you can take distance 1 minus distance 2 to get the change in distance, you will know the speed. Then use demensional analysis to get from in/ms to mph.

      There are limitations to this though. I don’t know how the doppler effect would interfere with your readings. You can experiment and figure that out. There is also a distance limitation previously pointed out. I also don’t know how accurate it is on none flat surfaces and small objects. But, in theory, you should be able to get the speed. You could also try putting the sensor on the moving object. That might eliminate some of the limitations, but you still have a maximum range on the sensor. I hope this helps get you moving in the right direction.


      1. The Doppler effect is not relevant. The Doppler effect applies to situations where the sound originates at a different speed than the recipiant. With the ultrasonic sensor the origin and receptor of soundwaves are at the same location.

  3. Dear Mr Walter,

    Thanks for all of your good works and lessons.
    I wonder if I could use ultrasonic sensor HC-SR04 to measure speed of sound in liquid. For example, by putting a very thin plastic over the sensor and immerse it in the liquid. I am a PhD student and conducting experiment.
    I will wait for your kind reply.

    1. I am really not sure how that would work. If something is moving fast you will get a doppler effect. Also, the sensor really works best for relatively short distances.

  4. This site is really great coz it is very useful. Keep up the good work sir! I am also an educator btw. 🙂 More power to you and God bless!

  5. Hi Paul,
    First, Thanks man, your videos have been of great help in boosting our confidence.
    Second, I was following this lecture and got curious about how the sensor works.
    So while experimenting, I put a delayMicroseconds(x) between the digitalWrite(trigPin, LOW) and pulseIn(echoPin, HIGH).
    I changed the value from 400 to 500 for two different positions of the object.
    ASTONISHINGLY, for around x>435 pingTime returned ‘0’ sometimes and x>450 returned ‘0’ always but for around x<=430 it always made a similar measurement.
    Now, I am very very curious. And I NEED TO KNOW how this thing actually works.
    Kindly explain the journey of the ultrasonic pulse from the module, back to the module and conversion of it to a signal to arduino.
    Is there a kind of buffer involved in the module that flushes out charge after sometime?

    Thanks Again,

    1. Hello Viabhav, you should NOT put a delay between the two commands. What you are basically doing, is setting a time during which bounced pulses are disregarded. So if the object is too near to the sensor, you will be pausing while the echo passes your sensor. It is quite obvious that you are reading a “0” feedback result.

  6. Hello,
    if I would like to measure the speed of sound through a SAMPLE of material, for example a pastic parallelepiped, is this method applicable? That sensor take the echo of the first discontinuity/medium, so basically I can measure the speed of sound of air or of water because ARDUINO will detect just the first echo, is it?

    1. The sensor will detect the first reflection, and it then generates the pulse on the echo pin. It will not register other bounces until it is triggered again.

  7. Hello Paul, I wrote the code just as you instructed in the video but the massage am receiving on the monitor is; (The speed of sound is: inf miles per hours)

    Please advise.

  8. Enjoying the lesson.
    Just one query though. As the sensor is about 1/2 inch (12mm) deep, should distance measurements be taken from the front or rear of the unit?

  9. hello Sir i really love your work , and i enjoyed every lesson.
    but how can for this i was wondering how can we use an LCD 16*2 instead of a servo.m ???

  10. I love your work Paul, but the concept here troubles me. You say that the width of the pulse in microseconds is the key. I used many delay times, and it’s clear that the time value stays the same regardless of what delay time is used. (so long as the distance stays the same, of course). This tells me that the time calculation occurs on the sensor, and the Arduino is simply reporting a value. The “pulseIn” makes reference only to when it goes HIGH. It does not know which pin sent the pulse, or when, or for how long. These facts are inconsistent with the notion that the width of the pulse in microseconds is important, unless the calculation occurs on the sensor. Any help in clearing this up is most appreciated.

  11. I found the answer to my confusion from a forum: “The pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds.” The only metric being measured is the time it takes for the onset of the ping to reach the trigger. The length of the ping and the time between pings is irrelevant. For me, the use of micro-seconds in the delay only served to confuse the issue. The pulseIn is measured in micro-seconds , but the delay time can be any length you want.

  12. I love the tutorials. Wouldn’t the speed of sound be the same no matter the distance? Shouldn’t we measure the distance instead?

    1. i think so
      i’ve tried this lessons, and when i change the distance the speed of sound is also changed.
      i think we can’t measure the speed of sound directly.
      the distance must be known first

    2. oh wait,
      the distance is 6 .
      so, just measure in 6.
      or you can change it, but don’t replace your sensor to another distance.

Leave a Reply

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