Transcript for:
Teaching Your Raspberry Pi About Echolocation

hello guys this is Paul McCarter with talktechboy.com and we're here today with episode number 20. in our incredible new tutorial Series where you're teaching your Raspberry Pi who's boss what I'm going to need you to do is pour yourself a nice tall glass of ice cold coffee that is straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at Sun founder Sun founder is actually sponsoring this series of video lessons and in this class we will be using the most excellent the most excellent Sun founder Raphael kit most of you guys probably already have your gear but if you don't look down in the description there is a link over to Amazon where you can pop over and pick up the identical Hardware that I will be using but enough of this Shameless self-promotion let's talk about what I am going to teach you today and what I'm going to teach you about is echolocation okay we are going to be learning about echolocation what is echolocation you ask well let me explain echo location is using sound to identify objects and sort of know where objects are okay so instead of using your eyes you're using sound to detect where something is an echolocation is actually used uh in quite a number of different applications by humans but it's also common in the animal world and can you tell me two animals that are very famous for using primarily echolocation in their interaction with the environment okay can you name the two the first one is the bat okay the bat lives in caves and caves are dark and so if the bat is going to interact and do things in a cave it needs a non-optical way to know where things are and that is echolocation so what the bat does is he puts out a little chirp and that chirp goes out it hits an object or a Target and then it bounces back and put the bat knows is he gives the chirp he wakes for his Echo and then when he hears his Echo he knows something's there and also a little bit about how far away it is and so if he chirps and gets the echo back quickly the object is near if he chirps and gets the echo back in a longer period of time he knows that it's distant and it's just really amazing how sophisticated the Echolocation capability is of bats the other echolocation animal is whales whales are famous for making those squealing or those those moaning noises and those noises can trans uh they can go through the water almost halfway around the world because sound really travels a long way in in water and so whales can use echolocation to very very very great distances probably for us humans the most uh the the kind of uh most familiar sort of echolocation here you're using radio waves instead of sound waves but it would be radar that you send out a radar signal it goes out it bounces off the Target and comes back and based on the amount of time in Flight of that signal you can detect whether something is there and how far away it is also sonar okay submarines use sonar and they're sending out pings if you've ever watched one of those old uh submarine movies you hear that ping ping well they're sending out a ping and they're waiting for it to bounce off of something and come back and good sonar operators can distinguish between you know not just how far something is away but but how big it is and what it might be and so uh sonar is really quite uh quite an art so sonar is pretty important echolocation is pretty important and that is what we are going to learn about today so enough of this introductory banter and discussion let's jump in and the good news is in your most excellent Sun founder Raphael kit there is a device which will allow you to do echolocation and that device is the hc-sr404 rather than giving you the model number it would probably be a little bit easier if I showed it to you so I'm going to go to this view it is over here it's that little uh it's that little board that has two nodes coming out like two little cylinders coming out that is your echolocation device and that's the device that we are going to be using today so I need you to go ahead and open up your Raphael kit dig through it carefully keeping it neat and find your hcsro4 then what we're going to need to do is we're going to need to get that thing hooked up so you're going to need a few wires now I show you the bond out here but you can see that when I built it I actually went ahead and I put it on a board when I built it I went ahead and I put it on a board give me just a second here I need to do a little Windows management there it is okay we're we're good I was talking too long in my Raspberry Pi went to sleep it's pretty sad when your Raspberry Pi goes to sleep right but uh What uh if you look up here what you can see is the sensor is pointing towards you the sensor is pointing towards you and when the sensor is pointing towards you let me get this when the center is pointing towards you pointing towards you the leftmost pin is the positive voltage since we're using a Raspberry Pi that would be 3.3 volts the rightmost pin is ground so left is 3.3 volts the right is ground that is pretty simple but pin number two the second from the left is your trigger pin and then pin number three is Echo so you power you ground and then you have a trigger pin and you have an echo pin what we're going to do is we're going to hook the trigger pin up to gpio pin 23 as you see here and we're going to hook we're going to hook the echo pin the echo pin to gpio 24. so trigger to 23 Echo to 24 and we're using the BCM numbering system and just as a friendly reminder these uh this breakout board is labeled with the BCM numbering system so that's why I like to use it in that way I can read the label right off of the little pins and not have to try to count pins and do calculations and things like that so hopefully that makes sense now you see when I actually built it on the board here I bought I brought 23 over to the uh I brought 23 over to the trigger pin and I bought brought 24 over to the echo pin and then I powered up my positive rail my top rail I powered up with the 3.3 volts and then I grounded uh I hope I didn't no I'm sorry up here this is ground I grounded here and then I made the rail positive with the 3.3 so 3.3 volts to your positive rail and then ground to your negative Rail and in the first few lessons I just think it doesn't matter so I hook them up different from the labels because just mentally in my mind power was power and ground was ground but that really some of you guys that really offended so I'm sorry I'm going back and I'm hooking things up according to the labels and so what I have is I have the ground pinned to the ground Rail and I have the 3.3 volts to the positive Rail and then I've jumped across so I've jumped a wire from ground to ground rail so that connects the ground rails and then the positive I've jumped over to the positive so the positive rail is connected to the positive rail the ground rail is connected to the ground Rail and that way I can snag either a ground I can snag either a ground or I can snag a 3.3 volts off of any either the top two rows or the bottom two rows if that makes sense so hopefully you have this thing hooked up but now how do we actually operate it it's really very easy to operate but I'm going to be I'm going to be honest with you if if we just jump in and start coding you're just going to be copying what I'm doing and so I want to go through a little bit of a little bit of work here on my Sketchbook and with a little bit of work on my Sketchbook I want to explain to you what we're doing right we always want to do things on paper before we do them we always want to do things on paper before we do them in code code and then the other thing that we always want to do we want to use good engineering practices we want to understand what we're doing so if I show you how this is actually working then you might want to do it differently or if you saw a strange result you would you know it would help you in debugging and troubleshooting hopefully that makes sense but let's jump in and remember we had what two pins what two pins did we have the first pin that we have is the trigger pin the trigger pin the trigger pin is the pin that we send signals to to command the HC sr04 HC sro4 ultrasonic unit to do a certain thing and then we listen on the echo pin we talk to the trigger pin and we listen to the echo pin so here's the trigger pin and then down here show this this is the echo pin so I have trigger and Echo and then what I have here is just time along this axis so this is just looking as Time Marches across and then here this is the signal we apply and so what we do is we just start we start somewhere and then what we do is we send the trigger pin to zero okay we send the trigger pin to zero so we're starting out at a zero and then what we're going to do is we're going to leave that that we're going to leave that trigger signal to zero for two microseconds and two microseconds is 2 e to the minus six all right two microseconds two e to the minus six seconds okay then we're going to come over and then we are going to send the trigger pin to a one to high okay now how long do we stay at high we stay at high for 10. microseconds okay we're going to stay high for 10 microseconds and then we are going to come down back to a zero all right so what did we do we set a pulse to the HC sro4 telling it do your thing okay but we're very deliberate about it the zero last two microseconds we transitioned that PIN to a one we hold the pin at 1 for 10 microseconds and then we come back to zero now the HC sro4 is going to start doing its thing so here I'll change colors because we are now we are now going to be looking at the signal coming off of the echo pin so blue was talk red is listen and you can see these are I'll just kind of draw a a dotted line here so it's at this point that that trigger pin sees that dropping edge of our control pulse and that's when it's going to go so once it's c is that there is going to be some period of time and we don't know what it is but some period of time for it to begin its thing okay for it to begin you know listening and getting all uh getting all configured but what it is going to do is it is going to now it is going to send out a Sonic pulse it's going to send out a Sonic pulse all right and when it sends the Sonic pulse out how does it know the Sonic pulse has left the barn the Sonic pulse has left the building it tells us that by sending Echo high now it is waiting it is waiting to detect the echo it is waiting to detect the echo of the pulse it sent out and when it hears it it will go down okay so we send a zero we send a we send a zero we send a one and then we send a zero now the HC sr04 knows that it should send out a ping at the point that it sends out a ping it indicates that by sending the echo pin high now it sits and listens and listens and listens and when it hears its ping come back it lets us know that the Ping came back by sending the echo pin low so if we want to measure how long that pulse was away how long that pulse was traveling what do we need to know we need to know this time the time that the echo pin was up and that that would be the Ping travel time that's how long it takes the Ping to get to the Target and to return does that make sense so as far as what we're going to do here in the code we need to generate this signal and then we need to listen here and then we need to measure that time okay now if you see that it's pretty easy but we've got to be very deliberate in doing it and you can't start your clock you can't start your stopwatch at your trailing Edge because we don't know how long it takes for it to actually initiate the Ping and so you don't time you don't time from your drop your trailing Edge you time from the echo pins Leading Edge does that make sense I hope it doesn't man this is pretty cool so I hope you guys sort of sketch this down with me because as we are going through our coding what we're going to need to do is we are going to need to be looking back at that it's going to be a lot easier to code if we look at our picture okay remember one of the things that I'm trying to do is teach you good engineering practices and part of that good engineering practice is to think about it and get things working on paper before you try to jump in and start coding and man that's one of the things that so many of these lessons are so bad is they just start showing you lines of code without really explaining to you what you're doing and then what you're ending up doing is just copying what the guy's typing not here not on my watch I want you to learn and I want you to be able to go out and do things on your own okay I need to switch over to my Raspberry Pi view I think now that you see you can see that we have this thing hooked up Okay so we've got this thing hooked up and now we are ready to code so I'm just going to come over to the full screen code View and what I will need you to do is come down and I gotta use the right Mouse it's very confusing having two mice one for the Raspberry Pi and one for the uh studio computer and I'm always grabbing the wrong Mouse but I digress so come down to your Raspberry Pi start icon we're going to go up to programming and we're going to come down to Sony and then thony's going to open up and I'm going to go ahead and I'm going to save this I'll save I'll save this as okay I'll save this as echo location like that dot p y n the dot py is kind of important and boom we're ready to start coding we are ready to start coding so what are we going to do we're going to be using the gpio pin so we better well we better import RP uppercase little I dot gpio all uppercase then we're going to need to import time because we're going to be doing a lot of very precise timing functions now what are we going to do we are going to do the gpio dot set mode and we are going to be telling it that we are working with what gpio.bs BCM numbering scheme why the BCM numbering scheme that way I can just read the PIN numbers directly off the sun founder breakout board and I don't have to be counting pins or anything like that so if I'm working just directly with the Raspberry Pi connecting directly to it I like to use the board or the physical numbering system if I'm going through the connect or through the breakout board I like to use the BCM hopefully that makes sense to you guys hopefully that makes sense all right so we've got that set up now we've got to tell it what is our trig pin our trig pin was 23 I'm looking over to make sure yes gpio pen 23 and then we have an echo pin and that was 24. now we need to go ahead and set those things up so I've got gpio dot set up what do I want to set up I want to set up trig pin and trig pin is a what it's a gpio DOT out like that okay and then I need to do a gpio.setup and what I'm going to set up I'm going to set up Echo pin and that is going to be a gpio dot in why we are talking to the trigger pin we're sending signals to the trigger pin and then we're reading from the echo pin so hopefully that makes sense all right now we need to go ahead and we need to do our try and accept so that we can cleanly lead lead the program so we'll have a try and we'll come back and fill in that try later and let's go ahead and finish it up while we're thinking about it except and we want to accept out on keyboard interrupt like that and then just open and close and what do we want need a colon there like that and then what are we going to do when we see the control C or the keyboard interrupt we're going to do this little bit of code which is gpio dot cleanup like that and then to let us know that it actually executed the pin uh the uh GPO cleanup I'm going to just do a print and it's going to say G is going to say g p g p i o good to go good to go like that all right now we need to come in and write our program well you know we're going to want to do this over and over and what I'm going to be doing is I should probably say what this program does is I'm just going to be sending a ping and then see how long it takes to get the echo back to get the uh the reflection back from the Target and so if the object is very close that should be a smaller number and if the echo time is very large then it will be a it will be further away so a small number means that it was you know boom boom it was fast a larger number means it had to travel a larger distance but you can see we're going to want to do this over and over so we'll do a while true one is true true is always true so we're going to create an infinite Loop here that's going to keep looping until it sees the control C then it will drop down into the keyboard interrupt and I hope that makes a lot of sense well the first thing that we are going to need to do is we are going to need to gpio dot output right the first thing that we did let me uh let me switch back uh let me switch back over here and let's look at our little picture what's the first thing that we do we send that trigger pin we send the trigger pin to zero right there okay so that's the first thing we are going to do is set the trigger pin to zero and so that is going to be that is going to be a gpio DOT output what are we going to Output to the trig pin and then what are we going to Output we are going to Output a zero so we're going to set trig pin to zero and now what do we need to do we need to wait and we need to wait for how long two microseconds so we are going to do a Time Dot sleep we're going to do a Time dot sleep of 2 microseconds well that would be 2 e to the minus 6 because in scientific notation e to the minus 6 is micro so two microseconds like that so we wait now after we wait what do we do gpio dot output now we're going to set the trig pin what to a 1 or to high now we're going to wait again go look at your little drawing we were going to leave that trigger pin high for how long for 10 e to the minus 6 like that okay and then after we do that then what are we going to do we are going to gpio dot output and that output is going to be trig pin and that's going to go back to zero so we've set it our control pulse and let me go ahead and just show you what we have coded up so far what we have coded up so far is is is this okay so we we set it low we go high and then we set it back down so we've done all of that right there now what we have to do we don't start our timer now because that ping does not immediately leave the sensor we have to start our P we have to start our timer our stopwatch what when the Ping leaves the sensor how do we know when the Ping leaves the sensor when we see the echo pin go high all right now this is something that you've got to really think about to make sure that you understand it you've got to think about it to make sure that you understand it but what I'm going to do is I need to what I need to sit and wait I need to sit and wait for the echo pin to go high and so what do I do I say while [Music] gpio dot input what am I going to read from child that is not a command shall is not a command while gpio input the input where of echo pin so this is going to look at Echo pin and if Echo pin as long as Echo pin is in the uh or while gpio Echo pin is equal equals zero as long as it is at zero what do I do absolutely nothing I just sit and hang here until I see the echo pin go high so what do I do I just pass so it's going to get here and if Echo pin is low it's just going to loop loop loop loop loop loop loop loop loop and do nothing and then when it sees the echo pen go high Echo pin is no longer zero and it's going to break out of this so this is just a little place where your program hangs waiting for the echo pin to go high if that makes sense now once that Echo pin goes High what do I do I start my stopwatch and I do that by grabbing the present system time and so what I'm going to do is I am going to grab my system my system Time by say Echo start time is equal to time dot time which just gives me the system time so now I've got a marker I've got the time that the echo pin went High all right now what do I want to do I just want to sit and see how long it stays high so now I'm going to do while gpio for while uh yeah while gpio Dot input of Echo pin while that equals equals now what one okay well that is equal to one what am I going to do okay what am I going to do absolutely nothing I'm going to wait and so I'm going to hang here for as long as the echo pin is high then when the echo pin goes back low now I need to what I need to click my stopwatch again and now I'm going to say Echo stop time is equal to time Dot time dot time again so I've recorded when the pulse goes up the echo pin goes up and then I've recorded when the echo pin goes down so Echo start time and Echo stop time now I have those two I have those two numbers now how long was my ping in Flight how long was my ping in Flight what was my ping travel time you guys tell me what was my ping travel time well it would be the echo stop time minus the echo Dart time like that does that make sense so now I know how long was the Ping out there flying its Journey the Ping travel time okay now what we're going to do is we're going to print ping travel time print [Music] ping travel time all right now what I know is this number like it's going to take like something like microseconds to milliseconds in order for the Ping to make its journey and if I just print ping travel time it's going to be something like 0.0007 or something 74365 and so you're going to have these numbers just going by and they're going to be hard to read so what I'm going to do is I am going to change that to an INT because then it will well before I change it to an INT I'm going to report it in microseconds so I'm going to take that small number and I'm going to multiply it by a million which is 1 E 6 and now when it prints it out it's not printing it out in seconds it's printing it out in microseconds and we should see numbers like 200 or 300 or 400 or 500 okay so that that will be easier for us to read now what else do we really want 200.346789325 no we don't and so now that whole thing I'm going to make an INT and by making that whole thing an it I should get an output that is readable so hopefully it makes sense what I'm trying to do here now you don't want to just sit and Hammer this thing too hard because you've got the pings going all over and you know you might read like you might send out a ping and it might go out and bounce off of something and you send another ping and you could get like a second reflection of that first P ping so you really want to put a delay in here just so that you let the room quieten down uh you let the room quieten down and you're not reading multiple Echoes of that same ping so I'm going to say time dot sleep and I'm going to sleep 4.2 seconds and then that should be good could this really be this easy I think it could be now what are we going to get we're going to get a very simple but very valuable number and that is how long the Ping was out there uh doing its uh doing its thing I think we're ready to run this okay somebody tell me how many mistakes I've made before I try to run this thing I was really trying to be pretty careful I sometimes get my case wrong sometimes I don't get my case right but I will need you to hold your breath and then you're looking down here to see if we're going to see some travel but I guess what I should also do uh yeah let's go ahead and run it okay hold your breath oh keyboard interrupt what is this keyboard keyboard interrupt that looks so right that looks so right invalid syntax except oh except why did I put a dot there for it's silly me that should be a space let's try again the real issue is one of you guys did not hold your breath that was the real issue so this time we're all going to hold our breath oh gpio what is that gpio.vcm okay uh gpio dot set mode wow that sure looks right gpio not defined oh oh oh oh I hope you guys saw that as gpio you got to import it as gpio okay hold your breath boom look at that we are getting signals okay we are getting signals so at least the sensor is running we have no idea whether these signals mean anything or not we have no idea whether they're accurate or relevant but it is good that we are actually getting a signal and so I need to see I think I'm going to get like a ruler out if I can find a handy dandy ruler I love these little metallic rulers there's a set of three of them that I have but I'm going to get the big one okay now as we get better at this we're going to need to figure out where do you measure from on the sensor is it the front of the cylinder the back of the cylinder the front of the sensor you know where is our zero point as far as measuring distance but what I'm going to do is I'm going to come over here and you see these numbers that are going by down there I'm going to give you a view where you don't see the program you don't need to see that program anymore but what you need to do is you need to see the numbers and then you need to see myself so do you see the numbers going by up in the upper left okay and now I'm going to put the ruler down here and I think I should move this over I should probably get smaller I want you guys to see this as well okay so what I'm going to do is if I look at this in inches I like this ruler because it seems to measure to the very edge but I'm going to measure from about [Music] [Music] and then I'm going to put this at three okay let's see if I put this at three that's not three that's three right about there and also I'm gonna go ahead and I'm gonna scoop this over because I want to make sure that it has a good clear view of the Target and then I'm going to put it like that okay and now look at that we're reading about 385 milliseconds we're reading about 385 milliseconds when uh when the target is three inches away what if we go to about a minute about a inch and a half we go from 385 down to about 284 okay if I go to an inch and a half then okay there I'm getting about 159 which is about half of where we were before you can see that I'm not getting the distance exactly right there because I don't have a real perfect view of it but you see 235 is about half of where we were uh is about half of where we were before so if you go there and if I pull it back the number gets bigger and bigger and bigger okay and then as I move it forward the number gets smaller and smaller and smaller why the target is closer and the Ping returns sooner all right now I can tell you that let's say that if you're at about three inches you should be expecting to see something like about 400 okay at about about three inches you should be seeing something at about 400 if that makes sense okay now somebody tell me so we we now have ping travel time okay we now have ping travel time and that assignment in this homework assignment is going to be to use this number up here that is scrolling by and to use that to calculate the distance to the Target so your homework assignment is to take this and to make a distance sensor all right now what does this require you to do you see this is more than just coding and poking wires up this is requiring you to think knowing the Ping travel time how do you calculate distance right some of you guys this is really easy but some of you guys I'll give you a hint if you're in your car and you're driving 60 miles an hour for one hour how far you go 60 miles if you're in your car traveling 60 miles per hour for three hours how far did you go well you went 180 miles so in order to do this what you need to know is you need to know the speed of sound okay you need to know the speed of sound and then what I want you to do is I want you to calculate the distance to the Target in inches all right now you're not going to be able to find a speed of sound very easily in you know inches per second or inches per month you're going to have to take the mile per hour speed and you're going to have to convert it to something that's in terms of inches does that make sure so this is requiring a little thought and a little math but this problem is not too hard for me to give you as a homework assignment has to do is I need you to do the homework and then when you do the homework I want you to post your solutions to YouTube then in my comments down below link over to your project I look at every single homework assignment that you guys turn in and then in your description of your video make sure that you link back to my video so if someone stumbles on your video they know what you're they know what you're talking about the homework assignment the homework solution for this assignment and then if you guys want to kind of try to do something a little more fancy or a little nicer feel free to feel free to do that as well but post your homework solution the assignment is is to measure distance from the center to the Target and then you've got basically a you know an electronic distance measuring system now we'll tell you that these little low-cost sensors like this you can't go out and do 50 feet I would imagine somewhere you know you know somewhere also you can't get tiny tiny tiny small but somewhere between an inch and an inch and a half to maybe three foot four foot maybe seven feet eight foot feet but you can play around with it and see what your uh see what your uh you know useful range of your sensor is I'll give you a couple of other quick I'll give you a couple of other quick uh quick tips here one is make sure like if you're measuring distance make sure that your target is parallel to your sensor so you don't want it off at an angle like this because then are you measuring to here or you're measuring to here where you're measuring so you want a nice good Target like that and also on your sensor make sure that it's not pointing down like if you plugged it in and it's pointing down it could go down and hit the desk and Skip off and it could get on a strange path so make sure that your sensor is coming out perpendicular to your board straight up and down from your board okay make sure that you're doing that get your sensor in there good and then make sure that you've got a good Target also as you're doing this you know make sure that you don't have some extraneous thing in the path because it's going to bounce off of the first thing that it sees okay guys man I hope you are having as much fun taking these lessons as I am making them if you enjoy this video be sure to give us a thumbs up if you've not already like And subscribe to the channel when you subscribe make sure sure you ring that Bell and then leave a comment down below when the video is over leave a comment down below to help me with the old YouTube juice because the more people that interact watch the video like it and comment the more people that YouTube will show this video to and that's important because the world needs more people doing engineering and fewer people sitting around watching silly cat videos Paul McWhorter with toptechboy.com I will talk to you guys later thank you