LESSON 17: Measure Speed of Sound with Arduino and Ultrasonic Sensor

Поделиться
HTML-код
  • Опубликовано: 26 окт 2024

Комментарии • 91

  • @hughpatterson1480
    @hughpatterson1480 2 года назад +2

    Another great lesson. I liked this one as an introduction to your other ultrasonic sensor lesson. I will pass this on to my students when we start working with them. Thanks again for including the dimensional analysis Paul!

  • @erygion
    @erygion 5 лет назад +6

    Another great lesson Mr. Mcwhorter, thank you. I had lots of fun with this because I've never done this experiment. I'm going to be sad when I run outta lessons lol.

  • @JohnGeddie
    @JohnGeddie 5 лет назад

    You can also use the same sensor to calculate the distance within 3 cm. This would allow you to do everything automatically with no need to manually measure. The Formula is pingTime/58 to get cm. Then convert to inches (pingTime*0.3937) for the targetDistance variable. I know the video is 4+ years old but thanks for taking the time to do these. They are great!

  • @StonedToDeliverance
    @StonedToDeliverance 3 года назад

    never thought I'd get into writing or understanding codes at 30 old. You good sir have helped spark some major interest in things i never considered a couple months ago!

  • @zatisofi9867
    @zatisofi9867 5 лет назад +1

    i rarely comment on video in youtube but Sir, you are an exception. Thank you so much for the detail and all. may god bless Sir.

  • @batmansgta
    @batmansgta 6 лет назад

    With my Swiss cheese brain due to the amount of pain meds that I've been on for 20 years, things don't come easy to me. You still manage to make it seem easy. Thank you, Paul!
    I hope this stays formatted correctly. Here's my homework.
    // Libraries:-
    // Defines:-
    // Pin allocations:-
    // Constants:-
    // Global variables:-
    int trigPin = 13; //Sensor TRIG pin to Ard 13
    int echoPin = 11; //Sensor ECHO pin to Ard 11
    float pingTime; //Time for ping to hit target and return
    float speedOfSound; //Calculate SOS
    float targetDistanceMin = 4; //Minimum istance to target - 600ms = 4" = 150ms/inch
    float targetDistanceMax = 8; //Maximum istance to target - 1200ms = 8" = 150ms/inch
    float targetDistance;
    // Class objects:-
    void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    pinMode(trigPin, OUTPUT); //Set TrigPin as output
    pinMode(echoPin, INPUT); //Set EchoPin as input
    }
    void loop() {
    // put your main code here, to run repeatedly:
    digitalWrite(trigPin, LOW); //Set TrigPin LOW
    delayMicroseconds(2000); //Pause to let sig settle
    digitalWrite(trigPin, HIGH); //Set trigPin HIGH
    delayMicroseconds(10); //Pause trigPin HIGH
    digitalWrite(trigPin, LOW); //Set TrigPin LOW
    pingTime = pulseIn(echoPin, HIGH); //Measure ping travel time in microseconds
    targetDistance=pingTime/150; //Target distance in inches is ping time / 150ms
    Serial.print(targetDistance); //Print target distance in inches
    Serial.print("\" is ");
    //So far, so good!
    if(targetDistancetargetDistanceMin){
    Serial.println("within target range." );
    }else{
    Serial.println("out of target range!");
    }
    delay(2000);
    }

  • @narbigogul5723
    @narbigogul5723 6 лет назад

    I modified your code so the speed of sound were in meters per second, I got to changed TrigPin from Int to Float so I could use 0.20 meters as my target distance. Thank you very much Mr. Paul McWhorter !

  • @Allthingstech3108
    @Allthingstech3108 8 лет назад +5

    You are a Great teacher. You lessons are organised nicely.
    Thanx

  • @chrisingram7277
    @chrisingram7277 5 лет назад

    Thanks for another enjoyable lesson, Hurricane Dorian had left me without internet in Nova Scotia for a few days so I had to revert to Elegoo's lessons on cd. It was kind of boring as I just inserted a file from the library and ultrasonic sensor worked with no explanation, just like magic. Your video certainly gives us many more possibilities for the sensor, even to measure golf club speed...

  • @MJCPeters
    @MJCPeters 6 лет назад

    Another great lesson! When looking at the setup I see quite a few variables, eg. Angle of the post-it's, lead sheet is slightly separated, and where on the sensor you choose to begin measuring (front, back or center). All in all I'd say ~1% +/- is dead on.

  • @batticha462
    @batticha462 5 лет назад

    Although I'm not a beginner I very much enjoy your lessons.
    By the way, humidity also affect the speed of sound.

  • @McGavel1
    @McGavel1 8 лет назад

    Awesome, I got one of these sensors too so I can do this one. I thought these were speakers when I got it in the mail cuz I just ordered a bunch of parts about a year ago that my friend who is into Arduino said I might wanna play with and just barely started finally learning Arduino about a week ago. Thanks again for the cool lessons - peace.

  • @W7LDT
    @W7LDT 7 лет назад +1

    I am a 63 year old airline pilot with a BS in Aero Eng. Technology. I am looking forward to applying this to my ham radio hobby. My question for Mr. McWhorter is; will the resulting speed of sound that is measured be affected by the density of the object the ping is striking? I will experiment with a pad of post-it-notes and then a piece of ceramic tile and find out.

  • @foxingeverything6100
    @foxingeverything6100 4 года назад +1

    Please make a updated version of this
    Thankyou

  • @tehyonglip9203
    @tehyonglip9203 7 лет назад

    I have measured the speed of sound and got the speed of sound as 333.67 m/s (mks) on average with a 2.7% error, I am now very happy!

  • @buzzvideos
    @buzzvideos 8 лет назад +1

    Hey paul!
    thanks for this tutorial . I got 1201 Km per hour , not even close ! but very helpful video .

    • @oneamongstthecrowd
      @oneamongstthecrowd 4 года назад

      No, you're actually right! 1201 km/h is equal to 333.6m/s!!

  • @marriuss9
    @marriuss9 8 лет назад

    you got to love the metric system

  • @govardhangd9387
    @govardhangd9387 7 лет назад +6

    sir can u properly explain the function of pulsein

  • @ilyashick3178
    @ilyashick3178 5 лет назад

    Paul, according to data sheet of HC-SR05 "DETECTION DISTANCE: 2 cm to 450 cm". It is not inches. Because of it inches needs to transfer cm to get speed close enough to real. As 6 inch is 15.24 cm. It means it needs statement. Usually sound of speed is M (meter) per second (Second). Miles per hours are not practical and confusion as we talked about speed of sound

  • @rafahatmuzaffar
    @rafahatmuzaffar 7 лет назад

    Good video Paul it was good and also I don't know how to use an Ultrasonic senser but know I to thanks good video.

  • @mohammedzoheb5299
    @mohammedzoheb5299 4 года назад

    Little confused on the pulseIn function. As per the definition on their website "if value is HIGH, pulseIn() waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing". We already gave the duration of pulse to be 10 microseconds so isnt it measuring this again instead of the time it takes between trigger and echo?

  • @jimrummy1300
    @jimrummy1300 9 лет назад

    You're the man Paul!

  • @Joeteck
    @Joeteck 6 лет назад

    I have a great question for you, how would you calculate the speed of light with a LIDAR sensor? What would you make the targetDistance variable to be? Leaving all of your variables as is, while using 58924.8 as my targetDistance I get the 669,600,000 miles per hour output. It took a bit of guessing but I got it. I did change the variable name to speedOfLight and such. Incidentally I am doing this on an ESP8266 using the MakerFocus Lidar device. If I Serial.print the pingTime, I get 10. Your thoughts?

  • @amandeepk9120
    @amandeepk9120 8 лет назад

    I wish you long long life. Thanks for sharing your knowledge.

  • @errrzarrr
    @errrzarrr 6 лет назад

    Do you measure the distance from the very base of the sensor board (the innermost part of it) or the outer tip of it (the physical outter part). There seems to be a few centimeters but maybe it matters.
    Thanks, you make this look simple and enjoyable.

  • @McGavel1
    @McGavel1 8 лет назад

    I wonder if you had 2 of these if they could be used to pulse Morse code communications to each other.

  • @tesfalegntadesse3737
    @tesfalegntadesse3737 6 лет назад

    thanks a lot sir! you are gentle man!

  • @davitgharibyan5871
    @davitgharibyan5871 8 лет назад

    Hi, thanks for the tutorial;
    Question: Is there a function of producing a pulse instead of creating it ourselves ?

  • @glynnhm0lsg308
    @glynnhm0lsg308 9 лет назад

    Great stuff as always

  • @droidocean2014
    @droidocean2014 9 лет назад

    Many thanks for your tutorials they are just awesome. I see on the video that you set the delay in microseconds. This made me think of something in general for other projects, as I wondered if you could change the unit of delay to seconds, minutes or hours? I know I could calculate everything in milliseconds, but if you want delays for hours you end up with massive numbers. For this it would be much better to be able to change the unit of delay to say minutes or hours. Is this possible?

    • @paulmcwhorter
      @paulmcwhorter  9 лет назад

      +David Hall You could probably do that if you wrote a custom function to define those functions.

  • @bhargavpatel1986
    @bhargavpatel1986 6 лет назад

    Sir give some video on stepper motor, LCD and menu button

  • @stclairstclair
    @stclairstclair 6 лет назад

    Hi again Mr.McWhorter, I'm having a hard time with when it's proper to use a space before or after keywords,
    I see "int trigPin=13;" and "int trigPin= 13; So this spacing will not effect code operation, I just need a little guidance.

    • @kyberghost3607
      @kyberghost3607 6 лет назад

      A lot of the time it doesn't really matter, it's just up to which way you prefer to do it.

  • @arshadarif2830
    @arshadarif2830 6 лет назад

    Sir,When you used pulseIn command for the color sensor you used LOW as an argument and here you are using HIGH. any reasons for it?

  • @adwaytnadkarni183
    @adwaytnadkarni183 8 лет назад

    Hey Paul!
    that was a good lesson, I enjoyed it.
    say, can you help me out with a doubt i have?
    when you taught the colour sensor, you used unsigned int for the pulseIn command and now you are using float. do the two data types provide simmilar ranges? 0-102400 i think??
    thanks!

    • @paulmcwhorter
      @paulmcwhorter  8 лет назад

      Floats can be very large numbers, but include the inbetween numbers. In math terms, floats are the "Real" numbers

  • @martyjames6204
    @martyjames6204 7 лет назад

    very good - thank you

  • @rafahatmuzaffar
    @rafahatmuzaffar 7 лет назад +3

    Hey guys I am just in fifth grade and I have done a lot of it and it is so simple.

    • @paulmcwhorter
      @paulmcwhorter  7 лет назад +5

      Well it sounds like you are a very smart and dedicated young man. Maybe a future engineer!

  • @farooqsheikh8603
    @farooqsheikh8603 4 дня назад

    good mr paul

  • @bobsheehan5164
    @bobsheehan5164 6 лет назад

    Could you use that component with a modified sketch to measure speed of an object? (i.e. a speed detector?)

  • @كرتوناطفال-غ4و
    @كرتوناطفال-غ4و 5 лет назад

    Welcome Doctor
    How to create a metal detector, underground spaces and pharaonic tombs using arduino

  • @dvistediallo7588
    @dvistediallo7588 5 лет назад

    You are just awesome...

  • @shauntippit4937
    @shauntippit4937 4 года назад

    So how do I hook up LED lights and two DC motors a fan motor and then hook it all together with the Bluetooth so I can run it on PlayStation controller

  • @tinoysantra1556
    @tinoysantra1556 6 лет назад

    Is this compulsary that we set target distance in units of inches? Can I set distance in centimeters?

  • @bubunmazumder
    @bubunmazumder 8 лет назад

    Do the trigpin & echopin always set to pin no. 13 & 11 respectively?or i can set to whichever pin no...

  • @amishpatel7794
    @amishpatel7794 7 лет назад

    I need help i want t make it portable so have a battery and and led to it my idea was you start your stop watch when the led turns on then stop it when it turns off now i don't really know how its going to work please answer tomorrow this is due

  • @luisviera3902
    @luisviera3902 4 года назад

    How can you measure the speed of an object? I am new at this

  • @sourodipbiswas2001
    @sourodipbiswas2001 7 лет назад

    Sir, I'm getting speed of sound as infinite in My serial monitor, I also tried calculating distance & time, both are coming zero, I thought time is very less so I converted it into nanoseconds still I'm getting zero.

  • @DragonSlayerPAT
    @DragonSlayerPAT 6 лет назад

    He sayed if you are doing this in High school you ahead and you are 1 in a 1,000 chance. I am in Elemerty school and I follow along completely

  • @phamtanduong4426
    @phamtanduong4426 7 лет назад

    thank you so much

  • @arkojeetmukherjee6490
    @arkojeetmukherjee6490 4 года назад

    Sir can I use A0 and A1 pins as trigPin and echoPin??

  • @vijaychandarkollipara8795
    @vijaychandarkollipara8795 7 лет назад

    can you please give more discription about pulseIn command. in previous lesson low pulse has messured colour strength which will be 0-102400 which will be converted to 0-255 i.e., backward 255-0 but here high pulse to get time
    in pulseIn we write HIGH or LOW is this for "forward and backword" or "high pulse pwm and low pulse pwm"

    • @paulmcwhorter
      @paulmcwhorter  7 лет назад +1

      Pulsein HIGH . . . the signal is normally low, and you measure how long it goes high.
      Pulesin LOW . . . the signal is normally high, and you measure how long it goes low.
      Depends on how the sensor behaves which command you use.

    • @vijaychandarkollipara8795
      @vijaychandarkollipara8795 7 лет назад

      thank you for the lessons, is there any chance of making lessons on microcontroller communication protocols like CAN, I2C, UART, SPI ..... i think they are intresting

  • @imretoth8150
    @imretoth8150 7 лет назад

    Hello I have the accent same code and my serial monitor reads the speed is 'inf' could you please explain what this means because i tried but i can't figure it out on my own.

    • @paulmcwhorter
      @paulmcwhorter  7 лет назад

      inf probably means you are dividing by zero. Before you do the calculation, print each variable that is in the calculation and make sure it has a reasonable value. Then if you have a variable that is 0 in the denominator, then find the mistake that led that variable to be zero. Use lots of print statements and figure it out.

  • @vigilante_stark
    @vigilante_stark 6 лет назад +5

    Everything was just perfect. But if Americans could just use the SI system for fu*s sake the world would be a much better place.

  • @elhammoutimed4389
    @elhammoutimed4389 5 лет назад

    Im using the metric system, so im measuring in cm per microsseconds, to convert it to km/h i multiplied the speedOfSound by 36000, but i get “inf” as a final result. Any might know what is wrong with what i did? Thnks

    • @SteffenABPetersen
      @SteffenABPetersen 5 лет назад

      I get the Inf as well,, Did you ever figure out a solution?

    • @F4LL__
      @F4LL__ 5 лет назад

      @@SteffenABPetersen l
      Lol I get the Inf as well,, Did you ever figure out a solution?

  • @ankurmazumder4395
    @ankurmazumder4395 7 лет назад

    what do u exactly mean by pulse width? Is it intensity or frequency?
    can u plz explain sir...

    • @paulmcwhorter
      @paulmcwhorter  7 лет назад

      pulse width is measured in time. How wide, in time, is the pulse.

    • @ankurmazumder5590
      @ankurmazumder5590 7 лет назад

      so does that means it's the frequency?

    • @paulmcwhorter
      @paulmcwhorter  7 лет назад +1

      No, pulse width is TIME! How long does one pulse last. Frequency is pulses per second, which is the inverse of period. Pulsewidth is simply how long one pulse lasts.

  • @stanleyludovick8803
    @stanleyludovick8803 6 лет назад

    "The Speed of Sound is: inf miles per hour" what does it mean? i'm not getting the value of the speed of sound rather this one, correct me please

    • @paulmcwhorter
      @paulmcwhorter  6 лет назад

      inf means infinity which means you are probably dividing by zero. Print out each variable you use in your calculation equation in your program, and probably one is zero, then find out why it is zero.

    • @stanleyludovick8803
      @stanleyludovick8803 6 лет назад

      Paul McWhorter thank you for your response, let me find that zero value in my variables. I'll get back to u for feedback soon. Asante sana (thank you very much)

    • @paulmcwhorter
      @paulmcwhorter  6 лет назад

      Yes, I know the meaning of Asante Sana . . . I have been to Africa 7 times and hope to move there soon.

  • @kostasfykouras2244
    @kostasfykouras2244 7 лет назад

    Hello mr Paul .My arduino after the serialprint command prints "nan" ,am i doing something wrong ?

    • @paulmcwhorter
      @paulmcwhorter  7 лет назад

      Somewhere you are probably dividing by zero.

  • @borutonarutonextgeneration691
    @borutonarutonextgeneration691 7 лет назад

    Is giving the target distance value necessary

    • @kyberghost3607
      @kyberghost3607 6 лет назад

      It allows for an easy way to quickly change that value.

  • @charlesklein7232
    @charlesklein7232 6 лет назад

    im back, the person who does not like your face. well you educated me! i spent an enormous amount of time trying to figure out the library needed to make the sensor work. BUT YOU EXPLAINED HOW IT DID. so now i dont need the library and i understand what it does, and how it does it. DONT GET IN THE HELICOPTER! their dangerous. most people die in "twins," but helicopters are second cause of death at the airports.

  • @disappear1234
    @disappear1234 8 лет назад

    im trying to make an led fed but i only turn it on in off thats the code i put in
    int trigPin=13; //sensor trig pin is connected to arduino13
    int echoPin=11; //sensor echo pin is connected to arduino11
    float pingTime; //time for ping to hit target and return
    float speedOfSound; //we will calculate the speed of sound
    float targetDistance=8;
    int LED=6;
    int writeValue
    void setup() {
    Serial.begin(9600); // turn on sserial port
    pinMode(trigPin,OUTPUT); //set trig pin as an output
    pinMode(echoPin,INPUT); // set echo pin as an input
    pinMode(LED,OUTPUT);
    }
    void loop() {
    digitalWrite(trigPin, LOW); // SET triger pin low
    delayMicroseconds(2000); // pause to let signal settle
    digitalWrite(trigPin,HIGH); // take trig pin high
    delayMicroseconds(10); // pause with triger pin High
    digitalWrite(trigPin,LOW); //finish trigger pulse by bringing it low
    digitalWrite(LED,speedOfSound);
    pingTime= pulseIn(echoPin,HIGH); //measure ping travel time in microsecond
    speedOfSound= 2*targetDistance/pingTime; // gives us speed in inches per microsecond
    speedOfSound= speedOfSound/63360*1000000*3600; // gives us speed of sound in miles per hours
    Serial.print("speed of sound is: ");
    Serial.print(speedOfSound);
    Serial.println(" miles per hour.");
    delay(3000);
    }

    • @disappear1234
      @disappear1234 8 лет назад

      Paul McWhorter
      you dont have to answer this one i figure it out instead of this digitalWrite(LED,speedOfSound); i put this
      if(pingTime/2*targetDistance){
      analogWrite(LED,speedOfSound);
      }

  • @sashamuller9743
    @sashamuller9743 4 года назад

    and this is why we use the metric system...

  • @nathanaferg
    @nathanaferg 6 лет назад

    What am i doing wrong, Im not getting a ping time. Sorry its a lot of code.
    int Trig=13;
    int Echo=11;
    float PingTime;
    float SpeedofSound;
    float Distance=6;
    void setup() {
    pinMode(Trig,OUTPUT);
    pinMode(Echo,INPUT);
    }
    void loop() {
    digitalWrite(Trig, LOW);
    delayMicroseconds(3000);
    digitalWrite(Trig,HIGH);
    delayMicroseconds(10);
    digitalWrite(Trig,LOW);
    PingTime=pulseIn(Echo,HIGH);
    Serial.print("Distance "); Serial.println(Distance);
    Serial.print("PingTime "); Serial.println(PingTime);
    Serial.println(" ");
    Serial.println("SpeedofSound = 2 * Distance / PingTime");
    Serial.println(" ");
    Serial.print("SpeedofSound = 2 * "); Serial.print(Distance); Serial.print(" / ");Serial.println(PingTime);
    SpeedofSound=(2*Distance)/PingTime;
    Serial.println(" ");
    Serial.print("The speed of sound is ");Serial.println(SpeedofSound);
    Serial.println(" ");
    Serial.println("SpeedOfSound = SpeedofSound / 63360 * 1000000 * 3600");
    Serial.println(" ");
    Serial.print("SpeedofSound = "); Serial.print(SpeedofSound); Serial.print(" / 63360 * 1000000 * 3600");
    SpeedofSound=SpeedofSound/63360*1000000*3600;
    Serial.println(" ");
    Serial.print("The speed of sound is "); Serial.print(SpeedofSound); Serial.println(" MPH");
    Serial.println(" ");
    Serial.println(" ----------------------------------------------------------------------------- ");
    delay(3000);
    }

  • @burakayan3360
    @burakayan3360 4 года назад

    in my measurment, i got the answer of infinite instead of some number.

  • @91722854
    @91722854 7 лет назад

    isn't delay already in microseconds?

  • @spazfall
    @spazfall 6 лет назад

    i keep getting "inf MPH" anybody know what that means?

    • @danbishop4035
      @danbishop4035 6 лет назад

      My sensor wasn't reading because I was careless and sloppy in void setup check there if you're still having trouble.

  • @daveplumley7854
    @daveplumley7854 9 лет назад

    Things were great until now. I keep getting 0 as a return when it divides (targetDistance*2)/pingTime. I started a new sketch and used the text code verbatim. Still getting 0. It's doing integer math on floats. I even tried changing from UNO to Micro to Nano. Nothing works. Broke it down serial.print and debug line by line. ping =11000. (Distance*2)= 12. Divide them and return is zero zero zero zero zero zero zero. Add some decimals? no difference. Define as float in global? no difference. Typecast as float during the operation? nope. 0. 0. 0. 0. 0. 0. 0.

    • @daveplumley7854
      @daveplumley7854 9 лет назад

      Created another variable of 1/ping and multiplied that by the other. No division = problem solved. Still don't know why that happened. But I learned that sound moves at 1cm in 29ms and one of my sensor was garbage(ping =11,000 @6")...

    • @adwaytnadkarni183
      @adwaytnadkarni183 8 лет назад

      maybe you would have figured as of now, nevertheless if it helps, try putting decimal values to all your ints as floating variables still return int, in your case 0

  • @stuarts7188
    @stuarts7188 6 лет назад

    My only issue, and it's entirely personal, is that all your measurements are not scientific ie not metric.