Arduino Tutorial 54: Measuring Speed of Sound With HC-SR04 Sensor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and we are here today with lesson number 54 and our legendary our epic series of Arduino tutorials what we're gonna do today is we are going to look at the homework that you were supposed to do in Lesson number 53 did anybody do the homework did anybody get a good answer I hope so we're gonna go through that today and what we are looking at is we are looking at the HC s ro 4 ultrasonic sensor and we're looking at using this to measure distance but unlike most channels where they just show you a picture and give you code and you just hit download and things are working on this channel I want you to understand I want you to understand how sensors work and I want you to understand how to do things for yourself because it's a lot of fun to copy and paste doesn't pay very well ok needs you to pour yourself a nice big mug of iced coffee is the day is coming to an end my mug of coffee is beginning to run low as well I need you to get your a Lego super starter kit if you don't have one look in the link down the description down below there's a link pick up the kit for $35 it has a boatload of components and it is what I am using for this series of lessons what we need to do is we need to start where we left off in Lesson number 53 and we had this circuit put together and then we had some simple code put together where we could send out a ping from the ultrasonic sensor from the HC s ro 4 and then we could measure the time that it took for the ping to reach the target for the ping to be launched go out reach the target and come back and that is not the view I want that is the view I want and I should probably get further out of your way and so if you are not up on this you can go back to lesson number 53 and catch up with us but what you can see is just where we left off we wrote a program and is the target let me move this back as the distance between the target in the sensor changes over here over here it is so hard to point over here you see that we are printing out the ping travel time in microseconds and then right here behind me is the oscilloscope and there we just have a picture of that high pulse which is the ping travel time illustrated as a pulse coming off of this sensor so as we move this away notice that both the width of that high pulse on the oscilloscope as well as the number being reported on the serial monitor in microseconds boast both are indicating that it's taking the ping longer to travel so what you can see is the ping travel time is proportional to the distance of the to the target which kind of makes intuitive sense right if you have to go further it is gonna take you longer to get there and back and we gave you a homework assignment and lesson number 53 and that homework assignment was to create a table where you adjusted this for ping travel times of 200 300 400 500 600 700 800 and 900 microseconds and then use a ruler to measure at each one of those the distance and inches from the kind of center point of this sensor where I've drawn the line out to the target did you all do your homework okay I am going to go through it with you and we're gonna talk about it because it's really important and I hope you did the homework and you might not have really understood where we were going with this but what I asked you to do was to adjust the target distance adjust the target distance until you got a ping time of 200 ma four seconds and then measured the distance to the target and so the numbers I got for 200 to 900 were one and a quarter two and a quarter 2.75 three point two five three point seven five four point two five five and five point seven five and then the assignment was to graph this looking at distance in inches as a function of time in microseconds and so that is what you were supposed to do and then you were supposed to calculate the slope of this thing well what we see is as we anticipate this is nice and linear it's not perfect because the sensor is not perfect and your ability to use one of these your ability to use one of these a ruler your ability to use a ruler is not perfect but what you can see is it's a very well behaved linear function and your assignment was to calculate the slope of that thing well how do I do this well one way I could do this I could put this in Excel and then I could plot it in Excel I could have Excel add a trendline and calculate the equation of the line but I just like being a math teacher kind of like doing things myself because I want you to see that lines are important well how do we calculate the slope of a line well Y M is y2 minus y1 over x2 minus x1 all right now what two points can you use you could use any two points but you're going to get slightly different answers depending on what's points you choose and if you pick two points really close together there's a better chance that you're gonna get an error so this one looks pretty close that one looks pretty close I'm gonna get a big span and that's going to help me I think get a better answer and so I'm going to use this point and I'm going to use this point which are over here this point and this point so what is y2 let me get this where you can see all of it at the same time y2 is five point seven five - y1 which is 1.25 over x2 which is 900 - 700 and then we remember that this is microseconds down here and this is inches and so what do we get and I do not do even the simplest math live without a calculator I'm really good at math but I do not do head math - one point two five one of the good things of a calculator is situations just like this four point five over okay I will do head math 900 - 700 is 200 it's really good to put parentheses in here like that so that's 200 and so then I have 4 point 5 divided by 200 and I get the number twenty two point five e to the minus three now let me make sure that that's right 900 - man that is crazy right I did you guys catch that that was crazy 900 - two hundred I said seven hundred because I'd already done it in my head nine hundred minus two hundred this is seven hundred so four point five over seven hundred that is wrong so I'm going to get four point five divided by seven hundred is going to be six point let's call it six point four three six point four three e to the minus three so in a number that would be 0.06 four three so I'm going to write M is equal to 0.06 for three all right somebody tell me what that number is what it means and tell me why it's important it has meaning okay it has meaning what does it mean pause the video put a comment down below and somebody tell me what this number means I would say probably most of you can't answer it because most of you know how to do math you know how to do the problem but you don't know what it means it's like here's the real world in your brain and this part of the brain is your math brain and those two parts of the brain don't talk to each other okay they don't talk to each other and I want to try to help you get those two parts of your brain talking together that is way too big okay so how do we start trying to figure out what this means well first of all distance this was the Y value okay what was the Y value it was inches and then what was the x value it was time in microseconds and so what we have to see is this actually has units some number of inches minus some number of inches that is going to be I'm going to write this again here M is equal to 0.06 for three when I ask you what this means the first step is to think about the units okay and what would the units be all right I have some number of inches minus some number of inches that leaves SWAT in the numerator that leaves dimensions of inches of inches and then down here I have some number of microseconds minus some number of microseconds that is going to be units of microseconds okay does that help now M is equal to some number of inches per microsecond inches per microsecond what does that mean does that mean something it sounds like a what it sounds like a velocity it sounds like a speed it sounds like some sort of rate of change remember how we said that slope back in the day in eighth grade or whatever slope is rate of change well yeah it's rate of change in this case if I'm plotting distance versus time it is a speed so what I can say is is that speed is equal to 0.06 for three okay inches per microsecond well we usually don't think in terms of this and so what are we usually thinking and I don't really speed of what we haven't gotten there yet but I want you to think through this okay I want you to think through this it's speed of something with some help but what I know is is that if I calculate the slope of this I have calculated a speed and a speed of something what is it the speed of well you know inches per microseconds or crazy units and so let's change that into something that makes sense which lets say miles per hour is something that a lot of us think in and if you don't don't start shaming me about the metric system okay I don't want to hear that I don't want to get shamed about not using them on the the metric system here all right what we are going to do is I'm going to show you how to change this into miles per hour and then this is a really really important technique that I'm going to show you it's called dimensional analysis and it's so important we'll do a future video on it but today I'm just going to show you how to do dimensional analysis with one example man the key to being successful in engineering and in life in general is to know how to do dimensional analysis so I start by writing what I have what do I have I have 0.0064 three and I have inches per micro second inches per microsecond the secret to dimensional analysis is you just go across multiplying by things that are equivalent to on okay until you get the dimensions that you want so you're going to be multiplying by one well what is it that I don't want I don't want inches okay I want to go to miles but I don't know how to go to miles in one step but what I can say is one inch is the same as I'm sorry twelve inches is the same as one foot so this is one to twelve inches is the same as one foot so now you see when I did that inches over inches is going to make one and so now the numerator is an in dimensions of feet what what else do I know I don't want feet there so I'm going to have feet down here well what do I know 50 to 80 foot 50 to 80 foot is one mile okay so now foot over foot makes one inches over inches make one what am I left with in the denominator miles okay now what do I have in the denominator I still have microseconds so I want to get rid of microseconds well I can go to seconds okay so microseconds are down here and I want to get rid of them so I put them in the numerator 1 million microseconds so 1 million microseconds is equal to one second okay so now microseconds over microseconds make 1 and so now I am in units of miles per second but I want miles per hour so what do I know well I know that one power is equal to 60 seconds times 60 minutes which is 3600 seconds okay so seconds over seconds make 1 microseconds over microseconds make 1 and foot over foot makes 1 so what am I left with I am left with miles per hour and now to get the answer I have to just go in and multiply all the numerators together and divide by all the denominators and then that will give me an answer in miles per hour if I do it right so let me let you watch the calculator here I will get a little further out of the way ok I guess the main thing is you just kind of watch the numbers going in as you look at the equation all right so let's start and we are going to say that's kind of got a glare on it doesn't it all right we'll do the best we can like this okay so we are going to say 0.06 4 3 because that's what we start with there times 1 million 1 1 2 3 1 2 3 times 3,600 3,600 and then I'm going to divide by I've got to divide by the 12 there someone to divide by 12 and 1/2 divided by 5280 okay and what do I get I get an answer which is 365 365 new that's that's 3650 I think I did this wrong so let's try it again I'm going to try it again kind of hard to hold it where you can see it then it's hard for me to see it okay so we are gonna do this again maybe you guys saw my mistake point 0:06 for three times one one two three one two three times 3600 and then divide by 50 to 80 and divide by 12 is equal to 365 boom okay so this is equal to 365 and now let's check those units again there's no such thing as cancelling but I'm gonna say okay let's make sure that we did this right this inches can't and that inches make one this foot in that foot make one this microsecond and that microsecond make one and then I this second and that second make one and I am left with miles per hour so 365 miles per hour Wow something something here is kind of like saying that it's moving at 365 miles per hour but I need you to do is pause the video and tell me what is moving at 365 miles an hour and then tell me what we did wrong okay we made a mistake all right tell me like at least tell me 365 miles an hour is probably related to what well what do I have moving I have a ping in the pingos and it comes back and therefore measuring 365 miles an hour is the what and then what mistake did I make pause the video come back pause the video go down explain what is happening all right and then come back okay let's think about what's happening if this is a velocity right if this has given me a velocity in this thing what is the only thing that's moving the ping the ping is a what it is a sound it is a sound that is going from here to there and back and so I am in effect measuring the speed of sound by taking the slope of this curve but wait when I look up the speed of sound on the internet I am told that the speed of sound is about I think 760 miles per hour so if the internet says that the speed of sound is 760 miles an hour in my sensor in my math is telling me that the speed of sound is 365 miles an hour what are the possibilities well one possibility is a really lousy sensor but when we look at the math the math is very linear it's very well-behaved and so there's not a huge amount of noise so I don't think that its sensor error and we did our math right so I don't think I made a math mistake but what did I do I made a thinking mistake we made a thinking mistake all right so let's think when we did this distance what did we do okay we said that like right here we said that at a ping time of about 400 we were let's see we said that that was like right here we measured that as three inches let's say we measured it as three and a quarter inches and so we wrote down distance as three point two five inches because I look at my ruler and this is one two three point two five so I wrote down three point two five is distance why is that off okay here's the secret the ping went three and a half inches there and three and a half inches back and so that three point two five that is the distance to the target but the actual travel distance was six point five okay and so in effect all of these numbers were off by a factor of two they should have been a factor of two larger because we should have been reporting roundtrip the distance traveled not the distance measured with a ruler did anybody figure that out okay now what you can see is is that with the y-values being twice as much the slope would have been twice as much and therefore this would have been twice as much and so if we take 365 times two to correct for our miscalculation our miss thinking we get seven hundred and thirty let me write it better seven hundred and thirty miles per hour okay and the known speed of sound is seven hundred sixty miles an hour well why are we off well I can believe that I can believe that's like what that's like a five percent error well I can believe that I can't read this ruler better than five percent is that really five percent ten percent would be off by seventy three and I'm not off by seventy three I'm off by like half of that I'm like 5% err I can believe that I can't read this ruler to 5% so probably a very accurate measurement with the sensor and then I what we are doing is we're just getting a little bit of error with rulers so you see what we just did we measured the speed of sound how did we do that well think of it this way another way to think of it is what they tell you in physics distance is equal to rate times let me start with an image what do they tell you in physics the distance travel distance is equal to rate times time okay and so if we think of that ping the distance the ping travels is equal to the rate that it travels at times the time okay what is the rate the rate is the speed of sound because it's a sound so the rate the speed of sound is equal to the distance traveled divided by time okay and so what we could do here is we could put the distance from the ruler and we could divide by the time that's the ping travel time and then we could get the rate but you've got to remember that whatever distance you measure with the ruler you've got to multiply by two because it goes and then it comes back so the actual distance traveled is twice what you are reading on the ruler now you could just do that directly and so I guess from any and one of these any one of these numbers you could calculate a speed of sound but if you do it using a line in using the slope you sort of have a tendency to smooth out the error and you'll get a more accurate answer okay is this just boring you is this making you angry does this make you wish that I would just go away and start posting libraries and codes that you could copy and paste and then you know feel good that you have something you something have something that is doing something like you got something that's doing so what's that's fun but you gotta outgrow that right you got to start thinking through things and what I've taken you through is kind of like how you have to think as an engineer how you build something you take data and then you you think about it okay now this is your assignment for next week so this time you what you did was without knowing it or maybe without knowing it you calculated the speed of sound with pretty good accuracy what's your assignment for next week is is use the speed of sound let me see let me just check that to make sure that I'm getting a good speed of sound I remember it is 770 maybe I wouldn't remember it exactly right okay and the internet says 767 okay so go ahead and just use speed of sound is equal to 767 miles per hour and now I want you to build a distance meter that will use this and will calculate the distance all right so before I use the ruler to calculate distance is the ruler to measure distance and then calculate the speed of sound this time what I want you to do is use the known speed of sound measure ping travel time and then calculate the distance does that make sense so what you're gonna do is you're gonna make a distance meter now with this sensor okay guys I hope you will stick with me on this I hope there are people out there that really want to learn how things work and learn how to think and just not just copy and paste all right palma quarter from top tech boy calm I will talk to you guys later and make sure that you do your homework okay the next lesson coming up I do believe I do believe that the next lesson I get I get confused sometimes about which lesson I am the next lesson will be lesson number 55 and I will go through building the distance meter in less than in Lesson number 55 but do it before the video comes out do your homework and then report down below if you if you have something that was working you'll know it works because you can check you can check your answers with a ruler okay top pulmicort from top tech boy go.com think about giving us a thumbs up think about subscribing to the channel think about sharing this with other people I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 24,671
Rating: undefined out of 5
Keywords: HC-SR04, Ultrasonic Sensor, Speed of Sound
Id: BTMMNsL0_b0
Channel Id: undefined
Length: 29min 22sec (1762 seconds)
Published: Tue Mar 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.