Arduino Tutorial 53: Understanding and Connecting the HC-SR04 Sensor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order from top tech boy comm and we are here today with lesson number 53 and our new improved our epic our legendary series of Arduino tutorials I am going to need you to get your super starter kit from a Lego if you don't have one look in the description down below there's a link you can get all the components that I'm using in this entire series of videos and it does in fact help if we are working on the same set of components I also need you to pour yourself a nice big mug of iced coffee and I will need you to get ready for a new and exciting lesson where we are going to learn about the HC SRO for ultrasonic sensor and the cool thing about this sensor is you can do a lot of things with it and one of the things you can do with it is to measure a distance you can use it as a distance measure and what I'm going to need you to get out of your kit today is get your Arduino get the HC SRO for sensor and then you're gonna need some wires now I will tell you that it's we're going to some of these later lessons and more advanced lessons I am starting to use an arduino nano now you can follow along and use your Arduino Uno r3 they work exactly the same but is I'm starting to work with some of these precision measurements I just find that you know when you start having a lot of wires running from your Arduino over to your board there's just a lot of opportunity for some intermittent connections and so what I'm starting to do is I am starting to use an Arduino Nano I've got the links down below in the description if you don't have a nano yet man I'm going to be using a nano and a lot of in a lot of the future videos and I think that you would find that you can just get a lot a lot cleaner builds if you use a nano the other thing that I'm using is the straight jumper wire again a link down below if you don't have them but these straight jumper wires just allow you to make much much cleaner bills okay so let me I've lost my cursor with my 14 monitors okay so this is what I'm going to be using for the build today but let me just kind of explain to you how this HC s ro4 sensor works and it really works pretty simple it has four pins it has it has four pins it has VCC which is just five volts that you bring in there's a trigger pin there's an echo pin and there's a ground pin and the way this works is of course 5 volts to VCC and then on the trigger pin if you send a pulse to the trigger pin you write the trigger pin low and then very small delay right the trigger pin high very small delay right the trigger pin low it will send out an ultrasonic signal as soon as it sends out that ultrasonic signal it starts listening for the echo and while it is waiting for the echo it sends echo pin high an echo pin high echo pin stays high until it hears the echo and then it comes down and so if you measure how long that echo pin signal stays high that is a signal that is going to be proportional to the distance to your target and so to kind of show you here what we would be dealing with me this here so I would put trigger pin I would put trigger pin low I would put trigger pin high I would put trigger pin low it will launch an ultrasonic signal it will come out it will hit a target whatever's in front of it and come back and then it will send echo in low so as soon as it's echo leaves as soon as it's ping leaves echo pin goes high echo pin stays high until the ping comes back and then a pingos love alright we're going to start this build and the build is pretty simple because this is a relatively simple sensor to hook up this is a picture of a schematic you can see that what we're doing is the VCC is going to go to five volts on the arduino the trigger is going to go to pin 12 and then the echo i am going to look closely trigger goes to 12 and then echo is going to go to pin 11 so we can do this pretty pretty cleanly and pretty easily what I am trying to do is I'm I'm probably gonna be doing some other things this is a pretty important sensor in a pretty cool sensor so we're probably going to continue to build on this first simple lesson so I'm going to use the Nano and I'm going to try to make a really neat bill I probably need to get further out of your way so let's go ahead and hook this up and the first thing that I want to do is I want to establish my grounds on my ground rails okay so I'm going to come over here to the Arduino ground pin and then I'm going to connect that to the negative rail and hence I will be creating a negative rail I love these nice straight short little jumpers I love to build with these things I'm gonna go ahead and make a top rail up here into Row 2 mark- because the arduino nano also has a ground on the top now if you are using the arduino r3 you know are three for this it has more than one ground as well and so you can activate both of those ground rules alright now and notice that on mine if you're using the e Lego kit you'll have the same thing this bottom negative okay and then here it's row 2 that's negative and I always make sure that you hook it up the same way that it's label and that just helps you not and later forgetting that you did it backwards and then and ending up maybe burning something out alright now I also want to get a I also want to get the five volts down here on the second to the bottom row and so I can see that I can't do that there five volts is up at the top okay so five volts is going to come right here and then like that okay can you see that let me let you focus on that so you can see exactly what I did okay do you see that five volt goes to the top row and that ground goes to the second row and then the ground on the bottom goes to that negative so everything is hooked up according to this plus minus plus minus but I haven't activated this bottom plus which I might need and guys I don't even really go through and think do I really need to do that or do I not need to do it I just go ahead and establish them because I sometimes not exactly sure where a project is going and so I want to have those rails activated so I think I can come in here with a straight wire like this you know sometimes these wires don't want to go in and don't force them in okay don't force them in just kind of jiggle them till they go in okay so with this not not so perfect jumper like that okay you see how I've tied the top plus to the bottom plus so now I have my power in my ground rails established and guys one of the things I'm going to do on this project is it is simple enough for you just just go somewhere and see you schematic and then download someone's library and then just hit run and make it work but and what I want is you know my goal is not for you guys to have a distance sensor my goal is for you guys to learn how to design things learn how to build things really learn how this sensor works and so we're going to kind of develop the whole thing from scratch and we will not be using pre-canned programs and pre-canned libraries because you don't need them right you can figure out and learn how to do things these things yourself so I do believe that we said that trigger pin so I'm going to come here but to the left trigger pin should be marked trigger is going to go to pin 12 okay and I need to come over one more because that wire is almost perfect and I am placing this sensor so that these nice wires will stay straight so now trigger pin is going to go to pin 12 on the arduino nano and that was just almost perfect length okay then what we also said was the echo pin on the sensor and I'm gonna move down one you know what before I do that I'm gonna go ahead and hook up power so I am going to hook the power VCC to the positive also notice that as I'm building I am not plugged in and I am going to go ahead and I'm going to move this further forward okay I moved it to the furthermost one right up against this trench to give me a little bit more room to build okay so now the trigger goes to pin 12 and now I will hook up VCC to VCC so the VCC on the sensor is now hooked to our positive rail and then I should have another small wire here you know I am bad about losing my little wires and so I'm going to have to get another one so I can connect up the ground so I will come here from the ground on the sensor and not press it in but jiggle it and yes that is the ground and it does go to the ground VCC to VCC 12 to pin 12 to trigger and now I need to connect I mean yeah pin 12 to trigger and now echo to pin 11 all right and do you see why I fussed with this a little bit I fussed with it because I didn't really want to run these wires over this one and so I try to just as I'm doing things as best I can keep a really clean layout and keep things where it's nice and neat see see that looks good not a lot of wires running around not a lot that could go wrong there so let me just stop and think okay I've got the Arduino grounded the Arduino grounded establishes the ground real I have 5 volts coming up to the top row then I jump for that 5 volts down to this positive rail I have the sensor grounded I have VCC to the +5 voltage and then I have those two and I'm not just saying that to be repetitive I am checking what I did to make sure what I did work now it looks pretty good and so what I am going to do now is I am going to plug this whole thing in and so I hope when I plug it in that I don't start seeing sparks flying I don't want to let the smoke out because once you let the smoke out it is very hard to get the smoke back in and these chips don't work without their smoke if you let their smoke out it is all ok so now that looks like it's hooked up so we need to come Oh and do some coding okay and so let me see if I can find a nice code view for you I usually have some pretty good code views okay I think that will work maybe yeah that's gonna work all right so let's think of what we need to do and just to remind you that we are pinging I'm gonna put our little target up and so our goal is to throw out a ping have it hit this come back and then print what that ping travel-time is but I am going to start here by saying all right what do we have well we started with a trigger pin and trigger pin went to 12 I hope you see I like to do this kind of methodically what else did we have we had an echo pin and that was pin 11 what else are we gonna have we're gonna look we're gonna send a ping and we're gonna look for a ping coming back and so we are going to have I'll call it ping travel travel time that is how long does it take for the ping to go from the sensor hit the target and return ping travel time I don't give that a value because we will be reading that as a value okay so now let's think of what we are going to do here in the void setup well I'm going to have a pin mode and I'm going to do a trig pin since that is we're sending a signal to it that would be an output and then I'm going to do a pen mode and then I am going to have a echo pin and that is an input I'm going to try to read something from that okay so that's setup so how would we get this thing to send out a pin a ping well I want to go to did digit all right digital right and what I want right trig and and what do I want to do I want to set it low because I want to start I want to create a low high low to get a real clear pulse there to send out that ping okay I'm gonna wait you just need to wait a very small amount of time so I'm gonna delay microseconds and just say 10 microseconds should make it work and then I'm going to do a digital digital right and then I'm going to do a trig pin-high all right so I'm putting the trig pin low and then high and then how long do I need it high not very long at all again to delay the lay my microseconds and then again about 10 microseconds should do it and now I'm gonna bring that low and when I bring that low that is when it's going to launch my pain so now I'm going to say digit all right trig in low all right now what is going to happen it's going to send echo is going to go high and echo will stay high until that ping makes it back and then echo will go low so if I just ran this right now what would happen did I start my cereal I need to sew my serial monitor serial dot again and then 2696 all right like that okay if I just download this now what's going to happen it's gonna ping and it's going to ping and it's gonna ping and then that trigger pin that echo pin is gonna once the ping is set sent that echo pin is gonna stay high until the ping comes back and so if we let's just see if we can download this do we expect let's see I need to make sure that I have my cereal my port it is not seeing at my port okay I probably might have shorted something out here or positive goes to positive 5 volts goes to 5 volts ground goes to ground this ground goes to this ground this goes to positive that goes to print so I am not seeing anything wrong here but I'm not seeing signs of life on this Arduino because the other end of that cord was not plugged into anything so that was a little bit of a faux pas and I do have I am sure that I have a live one around here somewhere okay so let's try this and see if we can bring a live happy little USB noise that's good so now let's see if we can download it let's I'm going to go ahead and just check make sure I'm on a thing also under tools under tools and Arduino board I'm going to set it at Arduino Nano okay because I am running a nano so let's see if we can download this we download it boom it downloaded okay so what these things should be doing is just sitting and sending out pings and listening for pings but we don't really see anything happen because we're not trying to read that echo pin right now but let's just see if it's doing what we want and so what I'm going to do is I am going to go over I happen to have here an oscilloscope and I'm going to hook the oscilloscope up to the circuit and then we are going to see if we can see that then come that echo coming back so if I am going to do this okay if I am going to do this where should i hook the oscilloscope what should I be looking at with the oscilloscope so this is the positive lead going to the oscilloscope this is the negative lead what should I be looking at I should be looking at echo pin right so I'm going to come in and I'm going to connect to echo pin and then the other one is going to go to ground all right and so I've got echo pin and then I have ground and look it seems like something is trying to happen here you know what I probably need to do I probably need to put a little bit of a delay in here because you know what's happening it's just like I'm hammering it before it even has a chance to look for that so I'm going to say delay let's say let's say 25 milliseconds you can see something happening on the oscilloscope there but what I didn't think is I'm sending out new pings probably before the old ping has a time to come back right let's see all right do you see what is happening I am sending out I got a rogue okay so I am sending out a ping and when that ping goes out do you see how that echo pin goes high and then it's listening and when it when the trigger when it hears that ping come back then it goes low alright so if I move this away from the sensor you tell me what the ping time should do it's not going to go up and down it's gonna what it's gonna stay high longer because the ping has to go further so it will take longer for it to get back and now watch me and let's see probably need to move this down so watch as I move the watch as I move the target away from the sensor what is happening to that ping time it is getting longer now at this point it's not saying things good so there it goes okay let's see I can go further and I can come back so do you see how that ping travel time is proportional to the distance the further the target gets the longer that it takes that ping to get back and so let's just review this we sit we set the trigger pin low we set the trigger pin high we set the trigger pin low the ping goes out and then echo goes up and echo stays up until it sees the ping come back and then it goes down and so if we looked at this like what I can see here is my oscilloscope is on 0.2 microseconds per division and let's say that right about here we are at about 1 division and so this ping that you're looking at up here on the scope that ping is about that ping is about I'm sorry my scope is on 0.2 milliseconds per division that is 200 microseconds per division the ping is about 1 division and so it is taking about 200 microseconds for the ping to leave the sense or go out and come back echo ping goes low and then 25 milliseconds later the thing repeats and goes again so if you had an oscilloscope in this sensor you could build a distance sensor by thinking okay I know how far the ping went I can calculate then with the distances or I know how long the ping was gone I can calculate distance but unfortunately you probably do not have an oscilloscope but lucky for you I'm going to show you a way that you can measure that size the length of time the length of that pulse the length that that echo pen stays high with a new command that you are going to learn today and what the command is is we will come over here the command is going to be pulse in so I'm going to look at a pin and I'm gonna see how long is the pulse so I'm going to say pulse in well what pin are we looking at we're looking at the echo pin and then what cop type of pulse are we looking for it will look at the oscilloscope we're looking for a high pulse all right now that's gonna make that reading I have to put it somewhere where would you put the value in the ping travel time of course so ping trap travel time is equal to pulse in and so what that should do is that should exactly measure how long in time that is without having to use missile scope does that make sense let's try to download this okay I'll come over here let's try to download this everyone hold their breath Oh denied echo it does oh oh were you yelling at me it was echo pin but the real problem was you did not hold your breath all of you so once again everyone hold their breath so this will work them okay now this doesn't say that it's working and but this does say that we got it downloaded but really what did I forget to do I forgot to print so I better cereal cereal doc all in and then what am I going to print ping travel time okay like that okay you don't have to hold your breath this time because I'm confident that's only for like trickier situations okay now we need to see if we can go to a view where we can see what is being printed I'm pretty sure I have one here let's see is that going to do it oh my I don't see anything maybe I didn't open my serial monitor okay I need to get this to come back I have so many views here don't I okay I've got to fix this so you can see this I am sorry sometimes it gets confused on this particular one okay give me just a second please to fix this it does not look like it wants to be fixed you see it's working it is indeed working but I've just got to get this one over here to become life and I think I know how to do that it is this one okay and then because I really apologize for this but I don't want to start over okay I promise you it is going to work this time oh you know what I see what was wrong it was not I see what was wrong it was hiding okay there it goes now that is working okay so you see ping travel time and look at this what did I say over here on the oscilloscope that my oscilloscope was set at 200 microseconds per division if we look on the oscilloscope that pulse width is about 200 microseconds and then we look down here at the serial monitor and it's about 200 okay what do you expect that ping travel time to do when I move the target away you expect it to go up because it's going to take more time for the ping to go and come back because it's got further to go what if I go further it takes even more time okay guys let me give you a little heads-up about some of the things to make sure that you do like as we start trying to figure things out and working with this sensor what I have found is is that the sensor zero point is about in the middle of the barrel and do you see how I've kind of drawn a line there that's like a good zero point that I think that this sensor measures about from there okay and let's see what else when you are doing this make sure that you have a good target and also make sure that you don't have something that could get in the way like in this case are you reading the pin are you reading the target you want to also make sure that if you're pointing too far down and you're going out a little ways you could be going down hitting the table and bouncing off and so you want to be kind of perpendicular to the surface here with the sensor and then an another thing is like if I'm measuring the distance from the sensor to the target you want to make sure that the it is parallel to the sensor so like if you have it like this the question is are you measuring this distance are you measuring this distance similarly if it's like this you want it Square and straight with your target but when you do that you see at least qualitatively it seems to be making sense what it's doing that as I go out further I'm out now about twice as far and the pulse is about twice as long I come in it comes back down so you can see at least qualitatively this seems to make sense now what you can see is is that from this we are going to be able to build a distance sensor okay we are going to be able to build a distance sensor but I don't want you again to use a library I don't want you to go in and just copy someone's code I want you to understand how to solve problems because yeah you can download canned solutions to this but nobody's gonna pay you to download canned solutions and the solutions people will pay you to develop are ones that don't already exist so you need to learn how to develop these things yourself so this is your homework assignment for next week okay for next week you've got this working let's say I hope you have this working where you are measuring the ping travel time of course I'm getting a nonsense answer now because I just move the sensor out of the way without a target okay but this is the homework assignment you have the ping travel time working I should also tell you that this ping travel time is in microseconds okay so your homework for next week is to make a graph yes I said make a graph all right and so on this graph what I want you to do is want you to make measurements with this sensor and so this is time and we can call this like our ping travel tie right that's what you're measuring on the sensor and then distance okay how do you get distance you get distance with your ruler and so what I want you to do is I want you to creat cut and complete this this table for 200 and remember what are we measuring in microseconds okay we're measuring in microseconds so measure 200 300 400 500 600 700 800 and 900 microseconds and then with your ruler over here measure distance and so what is this this is going to be in inches okay your time is in microseconds and your distance is in inches and so to kind of show you on 200 what I would need to do and I can go back over to this other view here no not that one I had it just a second ago it's gonna still be working as soon as I see it but I just had to find it see perhaps this one no that's not it I'm sorry that I am sluggish today on having these things but I want to look at ping travel time as I move things and I am sure we were doing that while ago of all of these years I have I can't show you both things that I want to show you at the same time so what I want to do here is okay I want to adjust my distance until I get my desired ping travel time this is the one I was looking for okay so what did I want I wanted a ping travel time of 200 so if I'm coming here and I'm watching that it looks like right about there not quite and you're not going to get it perfect but you see that is about a ping travel time of about 200 now you can sit and tweak it and the better you tweak it now that is pretty good so what I would want to do now is I would want to come in with the ruler and from those marks on there I would want to measure how far that is and then at once I've measured it I want to put that distance that I measure on here then what do I do I move the target until I get a ping travel time of about what about 300 and that's pretty close and then what do I want to do I want to measure that and then what I want to do is I want to keep moving it for 200 400 500 600 700 800 900 and then I want to fill in these values okay now this is the homework assignment for you to make that table once you have made that table your assignment is to graph that data and graph it very neatly and carefully okay and what was this on the x-axis this was ping travel time what is the y-axis this is distance the distance is in inches and the ping travel time is in microseconds okay then like you're going to have data and what do we expect we expect it to be linear it might not be a perfect line but then I want you to draw through your data a line and then I want you to calculate the slope of the line okay so your assignment for next week is to leave a comment in this video down below what slope did you get and then checked out in the comments and are people getting about the same slope so we're gonna do this all over the world let's see if we can hit all four hemispheres northern southern eastern western hemispheres let's see if we can hit all four hemispheres and everybody report what slope they are measuring all right that is problem number one on your side and make sure you do the accesses like this the ping travel time here and the distance here so that we're all reporting the same the same type of slope now the second thing is what does it mean okay and you see people hate math because it's just like it's totally abstract you go through your high school career finding the slopes of lines and it never means anything there's something very very meaningful about the slope of this line and if you look at that number and play with it you can see that it means something but then you're gonna see it doesn't quite work and then you have to figure it all out so somebody tell me what is going on here what does the slope of this line actually represent because it's something pretty important and you've just done something here when you did this that is significant and important and you probably don't even realize it so what our goal is is to hit all four continents as many different countries as we can get the slope of this line and then report down in the comments and then I will go ahead next week and I will work this with you and then I will give you the answer but let's see how many of you down below can leave the answer and get the solution okay and I know you guys hate me because so many of you just want to go in and copy something and make a little cool demo and you're gonna spend your life copying and pasting what other people have done no not on my watch you're gonna go figure this out and you're going to figure out what slope means it's kind of like a puzzle and it's sort of like really important okay so this is polemic order from top tech boy calm if you guys liked the video think about giving us a thumbs up subscribe make sure you ring that Bell so you'll get announcements on our new video releases and then think about sharing this with other people on your social media I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 52,687
Rating: undefined out of 5
Keywords: HCSR04, Arduino, Ultrasonic, Distance
Id: M-UKXCUI0rE
Channel Id: undefined
Length: 40min 31sec (2431 seconds)
Published: Tue Mar 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.