Paul McWhorter arduino lesson 20
We are going to create a device. This builds on lesson number 19, where you learned how to use the LCD and on lesson number 18, where you learned how to use the ultrasonic sensor in this project. What we’re going to do is we’re going to use the Arduino microcontroller we’re, going to have it hooked to an ultrasonic sensor and have it hooked to an LCD display and what this device is going to do is it’s going to measure distance to a target, and Then report that distance back to a back to an LCD display. You can see that I have the project running here and you can see what and what it’s going to do. We’Ve got the LCD display. Here we have the target here and the ultrasonic sensor is sending a signal to the target and then it’s bouncing back in it’s measuring distance. I have a little ruler there just for your reference, so you can see that if I bring it up to about 3 inches, we are measuring right at 3 inches. If I go to 2 inches, you can see that we’re measuring right at 2 inches. If I come back to 4 inches, we are measuring right at 4, and what you can see here is is that we have a little device. I have it just measuring small distances, so you can all see it. You can see it all in a in a field of view, but this will measure our distances up to about probably three or four feet fairly accurately.
I think the spec sheet says that you can go to an absolute maximum of 15 feet. I think for that. You would probably need to be shooting at a wall. You know you wouldn’t necessarily see a smaller object, but certainly for 3 or 4 feet. It works really good plus it lets you go in and really start learning the concepts of echolocation and it’s a pretty neat project to learn a little bit more about using the LCD display. The truth is, is that if you’ve gone through lesson number 18 and lesson number 19, you pretty much can go off and put this project together on your own. If you haven’t gone through those or if you’re a little confused and little need a little help, I will step you through it in this video step by step. Okay, you can get additional resources by going to top tech boy comm and looking at lesson number 20. The first thing you want to do is you want to hook your circuit up. This is showing the circuit here. This is for the type of LCD that comes in this part inventor kit. If you LCD or a similar LCD, you can just fire it follow this wiring diagram in my code completely, if you have a different one, you’re gon na have to figure out how to hook it up yourself, but I give you sort of a little clue down Here, if you go back to lesson number nineteen there is a table and what you can do is if you don’t have the spark fund LCD, like the one I have.
The thing is, look on the spec sheet and it should tell you which pin is VSS and you want to hook that to ground and VDD. You want to hook to five. You want to hook these names on your instruction sheet to these pins on the Arduino, and if you do then my code and my tutorial should work for you. I’Ve done this with several different LCDs, a lot of them a lot of them work very similar to this, particularly the ones I mean, specifically the ones that are the sixteen columns by two rows. These little ones that are this size often work work very similarly to very. Similarly, some of them even have the same pin out, but they don’t all have the same pin out so like I say you should really. If you gone through these lessons, you should be ready to do this without any help from me, but if you will it’ll help, I will go ahead and step you through. It start by clicking your circuit up, that’s, the trickiest part, and you can see there are a lot of wires going everywhere. You’Ve got the ultrasonic sensor, you’ve got the potentiometer. The potentiometer is setting the contrast on the screen. I can show you how that works. If you turn it down too far, it’s sort of blacks out, if you turn it up too far, it sort of goes bright out and if you get it, just the right amount to get the maximum.
You know the maximum ease of reading and is what you are is what you were shooting for so go ahead and get this hooked up. I find a lot of times. It really works well to work with a buddy where you have somebody that is sort of looking at the schematic and you’re looking at the circuit and going through it step by step by step a lot of times where I’m working with students they have trouble. With this project, because they are just not being careful enough, but if you hook it up right and you have this type of LCD, then things should work. Alright, I think we are ready now to actually jump in and start trying to write some code and so let’s see if I can get rid of some of these windows and I will open up a new sketch window here. Okay, so let’s get started in writing the code. We got to be thinking as we go through here. We need to get everything set up for the LCD and we need to get everything set up for the ultrasonic sensor, and the first thing we always do is to think, if there’s, any libraries that we need will to use the let’s see to use the I’m Trying to get this set up give me just a second here to get my windows organized okay, to use the liquid crystal display. We need to load a library. There is not a library for the ultrasonic sensor, but we do need the library for the liquid crystal sensor, as you learned in the last lesson.
The way you do that is, you do pound include, and then the name inside a brackets of the library liquid crystal pay attention to the upper case, lower case that matters dot, H and close. That off and and this is one of the few places that you don’t put a semicolon at the end of the line in Arduino, so we’re going to load the liquid crystal library, okay, the next thing we need to do is we need to create the object That we’re going to be working with this object called the liquid crystal. We got to create that object and we give it the command liquid crystal and notice. When I do that, it turns at orange it’s recognizing that as a command. Now we name our liquid crystal display, we could call it my display, we call could call it whatever we wanted. I’M gon na call. My LCD, like I did in the last video now we have to tell it how it slipped up, and if you followed that schematic that I gave on top tech boy calm in lesson, 19 or lesson 20, you will want to set these to 10. 10. 9. 5 4, 3 2, and this corresponds to that table or that schematic telling telling the arduino how it is up. We create the liquid liquid crystal object named, which we named LCD. Okay, that gets us going there now we’ve got to think about our variables. Well, let’s start thinking about our ultrasonic sensor, there’s two key pins on our ultrasonic sensor.
We have the trigger, pin, which I’m going to call trig pin and in the schematic we had that up to 13, so we’re gon na say, look sense or rig, pin to Arduino pin 13 okay, then we also had on the ultrasonic sensor, the echo pin and We had that hooked to pin 11 okay, so we’re gon na hook, I’m gon na say, connect connect. What am I doing here? This is crazy. That’S connect, okay, connect, sense or echo. Pin two are ten eleven alright. What else are we gon na need? Well, we’re going to need we’re going to need to measure. You know part of the thing that we’re measuring is we’re measuring the ping time to go from the sensor to the targeting backs. We’Re gon na need a variable for that I’m gon na go ahead and make that a float because we’re going to be doing some math on that so I’m gon na make that a float and I’m gon na call that ping time and that we are just Going to declare our variable for measured time, so we’re gon na measure ping time we’re gon na put it in the variable called ping time. We also what we’re doing is we’re reporting the distance to the target, and so we need to make a variable for that. Okay target distance and we don’t need to set that to a value we’re measuring that or calculating it, but this is the distance to the target in inches.
Okay, I just try to think of as many variables as I can and just put them in up front. Sometimes you forget one, you got to come back later and do it, but I try to kind of get these things set up to begin with all so what are we going to be doing? We are going to need to know the speed of sound in order to calculate the distance and so from our earlier lessons and make sure that you’re paying attention to this capitalization you don’t have to do it the same way. I’M doing it. But, however, you do it, you’ve got to do it that way throughout the code, and what we learned in the earlier lesson is the speed of sound is 776 0.5 and that’s in miles per hour, speed of sound in miles per hour and we’re going to need To do is we’re gon na have to need, remember that, because we’ve got to keep these units straight when we measure a ping time it’s in microseconds, and we want the speed of sound in miles per hour. So we’re gon na have to do some. Some unit conversions, we’re gon na, have to do some dimensional analysis to make sure we end up in the right set of units, but that is to worry about later right now, we’re just trying to get things set up. I think that’s, all the variables that I’m going to need and the void set up, it’s always good to turn the serial port on, and I might not even use the serial port this time because I might just you know, be using the LCD display.
I might not be printing anything to the serial monitor, but I go ahead and at all, because sometimes you might need to use that serial monitor to do a little debugging. So I go ahead and turn it on, and so what we’re doing here is turn on serial monitor and just in case we need it. What else do we do in our setup? We do the things that we need to do one time, so we need to do our pin modes and so the pins that we have to worry about or that trigger, pin and that echo pin here all of these pins over here we have already taken care Of in this library, the library and this creating this object takes care of all of that. So we only have to worry about the two pins that are the kind of control and measure pin the control and measure pin coming off of the ultrasonic sensor. So here we go we’re going to set the pin mode of the trig in. This is something that we are sending a signal to. So it is an output, okay and we are setting here, set the trigger pin to the output. Okay. What else do we have? We have the echo pin – and this is the pin that we’re listening to to try to see that pulse coming in it gives us our measurement, and so that will be an input set echo into B and okay.
Now that looks pretty good as far as what we need to do for the else for the ultrasonic sensor, but we do need to go in and kind of get get our get our LCD going. If you remember from the earlier lesson, there’s a few things we need to do, we got to kind of turn it on. So we say LCD dot begin and then we’ve got to as part of the LCD begin. What we need to do is we need to let see we need to tell it how big it is, and this was a sixteen by two, so that’s 16 columns by two okay. So we turn on the LCD until we know how big it is and remember. The reason we say LCD is because that’s what we named the object. If you named your object, something different up here, then you would need to call it something different down here. Okay, we, if you notice here that on the first line I print target distance and so let’s go ahead and take care of that, because we only need to do that one time, and so we need to tell it where to print. So we want an LCD set set cursor that looks good, turns orange. It recognizes it and I want to go to the first column, which is column zero, and I want to go to the first row, which is row zero, so LCD set cursor to zero.
Zero puts cursor in upper left corner of display, yeah, okay, and since I only need to do it once why don’t we go ahead and just print that first line so that we’ll have it there get that out of the way. So we can do LCD print and what am I gon na print I’m gon na print target distance, that close it print? First row you could say print first line but I’ll just say print first row I think that’s, the stuff that we really need for our setup. So we are well on our way to getting our program running here. You see you can do some pretty impressive stuff. I mean you’ve learned some very powerful techniques and a lot of times it doesn’t take that much code. Now we need to measure our distance and how do we measure our distance? We need to measure our ping time and how do we measure our ping time? We go to the trigger, pin, we set it low and we pause, we set it high and we pause and we set it low and we set it pause. I think if you go back and look at lesson 18, you can get some more details on that, but you can just follow along with me and take my word for it at this point. If you want to so digit all right, we’re gon na send that trig pin we’re gon na, send it high because we’re going to send it low, because we want to start in the low position, if we’re going to make a.
If we’re going to make a little pulse okay, so what are we doing? Set the trigger and blow and what is our trigger? Pin our trigger pin is pin 13, which means that you should have the pin here labeled trigger connecting over to Arduino pin 13. You should understand that by now did you now what I’m going to do is I’m going to delay and I’m going to delay in microseconds I’m gon na delay about 2000 microseconds, so we’re just gon na briefly pause. The reason we do that is, we pause to give the signal of time to settle, make sure the Arduino sees that it’s down there low okay, so now we’re going to digital right. What are we gon na write, we’re gon na write to creep in again we’re going to put it high, set trigger pen high. So what we’re doing is we started low, we’ve now written it high? Now we want to pause again and it doesn’t take that long. We just got to pause long enough to make sure that signal stabilizes in the Arduino has a or the the sensor has a time to see it. The ultrasonic sensor has a time to see that it’s gotten a trigger pulse, so we’re going to delay. You will do delay microseconds again and we don’t have to delay very long at all we’re just going to delay 15 microseconds, so we’re just going to pause briefly. Okay. Now, what we’ll do is digital right, we’re, going to bring big, pin low, bring bigger and back low, okay and let’s.
Wait there for just a little bit to make sure it has time to settle a delay. Microseconds and we’ll do about ten. Microseconds is usually fast enough, briefly pause, okay, so what we’ve done here is we created the pulse that will allow us to now make a measurement so set it low pause, go high, pause and go low pause, and so that creates a pulse from the Arduino to The ultrasonic sensor telling the sensor to make a measurement it’s that trigger pulse, to tell the sensor to make a measurement. Now that we’ve told it to make that measurement Bart told it to send the ping. We now have to go measure the ping and when we do, we measure ping time and we measure ping time how we look at the echo pin and we look for a pulse at the echo pin in the length of that pulse tells us what the ping Time is in microseconds: how do we measure a pulse with an Arduino? We do a pulse in measurement, we got it till it. Where do you want to do the pulse in well, we want to do it at the echo pin and we want to look for a high pulse because that’s the way the sensor works. Okay, so we’re going to measure ping time and we better put that as a comment we went through this and quite a bit of detail in 18. So go back if you, if you need more deep, more information there.
Okay, ping time is reported in our the pulse in response gives us the ping time in microseconds, okay, but we don’t we aren’t using microseconds up here in the speed of sound we’re using seconds or in fact we’re using hours. So we need to convert microseconds to hours so that our units match, and so what we can do is we can take ping time is equal to ping time and how many microseconds are in a second there’s, a million, and so a second is a million times. Bigger than a microsecond, so we need to divide ping time by 1 million that’s one two, three four five six and I always put a decimal point on my numbers to make sure that it is doing floating point math. If you don’t do that, you can get some nasty surprises, sometimes so this converts microseconds to seconds, but in fact we did not want seconds. We wanted what we wanted hours and so how many seconds are in an hour. There are 3600 seconds in an hour, so we need to come in and we need to divide by 3600 now we’ll put a decimal there as well. This converts seconds to hours, okay and one way to think about it. You really need to do dimensional analysis, but if you’re just doing something simple like this, you had that one second is a small part of an hour. So one second, you would divide by 3600 and you would just get a you know, a small fraction to to end up with the number in hours.
Okay. So now we have ping time as in hours, that’s good. So now what is the target? Distance? Okay, the distance to the target and let’s make sure yeah. We declared that up there distance is equal to rate times time. Well, what is our rate? How fast is the ping going it’s an ultrasonic ping it’s going the speed of sound, so distance is equal to rate times. Time rate is speed of sound okay, and then we multiply that by what distance is equal to rate times time, what is the time? How long did it take for the ping to go from here and back that was ping time. We just measured that okay, what does this do? Sweep measure distance, traveled body, pink, okay, no it’s, the ping travel. What did it do it came from here, and it came to the target and it came back. So it went twice as far is what the target is away because it goes in returns, and so, if we want the actual distance to the target, we take the distance that the ping traveled and we have to divide by two. So the real target distance is equal to that. I have target distance. We just calculated divided by two and what this does is I better go back and put my semi colon up here. So what this does is accounts, for fact, pain is making a round trip. So we divide by two to just get the distance that it is to the target all right well now and the speed the the thing so far is going to be in inches because or the thing so far is going to be in miles, because the reason It’S going to be in miles is because we put the speed of sound in in miles per hour, and so we put when we put speed of sound here in that setting the units at miles, and we really don’t want miles.
So what? How do we convert target distance from miles to inches distance is is equal to target distance divided or times sixty three 360? Okay, how come because there’s six thousand sixty three thousand three hundred and sixty inches in a mile? How did I know that? Well way? You can do that is you can just go to Google if you haven’t learned this, this is a good time to learn it and what you could say is like just say one mile in inches just type in one mile in inches, and it says one mile Is sixty three thousand three hundred and sixty inches and so that’s pretty cool, and so, if I have miles that’s a whole bunch of inches so to convert 1 mile 2 inches, you multiply by 63 6 60, and so this converts miles to inches now. Why was this in miles? It was in mouths, but could entered our speed of sound in miles per hour and so therefore that’s setting the units to be in miles. Okay, no, I think that now we have the target distance in inches like we want, because we have been good and have done all of our conversions. If you get a crazy answer, chances are that you probably made a mistake in here somewhere so now, let’s see if we can report that back to the back to the sensor back to the LCD, so we’re gon na say LCD dot set cursor because we got To tell it where to print it.
Well, where do we want to print it? We want to print it on the second row in the first column, okay, then we start with column so column. What first column is column, zero and then the first row is Row zero. The second row is Row one. So we go to. We go to column zero Row. One is this to set cursor to second row. Let me say to first column second row all right and you get used to this business, of how you start counting with a zero a lot of things in programming. You start counting with zero it’s just the way they do it. Okay, now remember: what we want to make sure is: is that we don’t want this thing to end up with those ghost characters. If you need to understand that better go back and watch lesson number 19 again, but if we’re doing something that we’re constantly updating what we need to make sure that we do is we go in and clear the line, so we don’t end up with ghost characters And so to clear that line, what we do is we say: LCD dot print and then what am I gon na print I’m gon na print 16, blank spaces, 1, 2, 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. That ensures that we clear that row completely out: okay, so that there’s Row 2, which is called 1 all right, so we’re clear Road 2.
Now what do we want to do? Well, if I, if I do, that print statement, that’s gon na leave my cursor way out here. So I printed that string of blanks to clear the row and now what I got to do is I got to put the cursor back up here at the start, and so I got to tell it again: LCD dot set cursor and we need to go back To 0, 1, again, okay, so again put cursor back to the first column of second row. All right, you know who do I want to do right, LCD dot print, and I want to print what I measured. What did I measure? I measured target distance like that distance to target LCD and then what I want to do. I want to put my units and then I put that space because I don’t want the inches crammed right up against the number. So I put a space and then inches, and then that should be good, so we’re gon na be nuts like that. Ok, I’m gon na get rid of some of these extra blank spaces, and then that ends my void loop, so it’s gon na sit and loop through here. The other thing I need to do is you don’t, want this thing to run so fast that the display looks in jittery, and so you want to put a delay in there and, if you put a too big of a deal like, if you put a second Delay then the response is sluggish, but if it’s too fast its jittery now in something like this, usually about 250 milliseconds, which is a quarter of a second works, pretty good to you, it seems responsive, but it doesn’t get so so much to the point that it Gets jittery okay, so let’s see here that looks good.
How many mistakes do you think I made? I let me put commas here pause, so my display is not okay let’s see. Oh, did you guys catch that I didn’t put the semicolon there. Okay, let’s see what else we got. Oh it’s gon na be happy this time. It’S gon na be happy. Okay, so let’s see here, let’s see how well we’re doing I’m gon na set it at about 3 inches and look at that I’m reading, just almost dead. On three inches at that point, yeah very good. If I put it right at about two and a half inches I’m reading, let’s see I need to come back a little bit there. I need to get this lined up a little bit better it’s reading right at two and two and a half inches. If I come out to five inches and a little off your screen there, you can see it’s reading five, and so this thing is really working. Now so we created a little device just with a few dollars worth of components where we can measure distance and we can display it now. You could do this fairly easily, just showing it at the you know, just displaying it on the serial monitor. But the thing about this now is that we could go portable. We could go mobile with this thing. We could hook it up with a battery and we could walk around and make measurements now. I am kind of curious how how far this thing can read and so let’s see if we can kind of point it over here, mm let’s see well I’ll, just point it back over my shoulder.
I guess you can see, see that, and so, if I point it back over my shoulder I’m reading fifty inches and that that looks about right, let’s see if I pointed up at the ceiling, what we can get and it’s reading about 62 inches, and so that Looks good I’ll point it over there at the wall and that looks like it’s 42 inches that looks about right, let’s, see if I got it, get it about a foot from the screen and see what it reads. It reads about a foot, so you can see this thing is just reading I’m gon na try to shoot it across the room and see what we get here, not working too good, as we start getting out towards more than 10 feet and also like I said I think that the further off you get the larger the target, the larger the target needs to be, but I hope what you can see is: is that there’s, some really pretty neat stuff that you could that you could do with this? You know you could use it to make a simple distance measurement. You could put it on a robot so that the robot doesn’t run into things. If your robot is driving along and say, if you get within a foot of a wall, it stops and kind of backs up in turns or if it sees something in front of it, it can back up and turn it gives you a way to kind of Give a robot an ability to know where it’s going and not run into things.
Also, you could make some security devices. What, if you wanted to know if someone would come in your you know in your room, you could put this up in the corner and you could be looking for motion. You know we’re, like let’s, say it’s, reading a distance of 14 feet and then all sudden it sees six feet. Well, you know something’s moved into the field of view, so you could do some pretty sophisticated things as far as your security devices and so I’ve I’ve really stepped you through this kind of gave you the baby steps on this one. What I want you to do now is: I want you to take some combination of of this ultrasonic sensor in this display, and I want you to take this in your own direction. I want you to come up with your own project. Maybe you could make a security device that could tell you that if someone entered a room then maybe you could see how hard is it to circumvent that maybe see if you can get one of your friends to sneak into the room and have it not see It and so let’s start start making your own product think about what your own ideas. You can measure a distance and you can display it. What can he do with that? You know robotics, you could do it for security devices. You could make a measurement instrument out of it, but what I want you to do is not just learn to program.
I want you to be creative. The thing is now you’ve got this tool, go, go, build something, go, do something useful with it so I’m going to give you two or three days to work on this and then what I want to see is a lot of different ideas in the class and Maybe a couple of people will work on one idea together, but I want everyone in the class to do the same thing. I want you working on your own ideas. Okay, this is lesson number twenty coming to an end and we will see you shortly with lesson number twenty one.
Paul McWhorter arduino lesson 20 Video
Paul McWhorter arduino lesson 20 news
Posted on Friday August 19, 2016Books You Should Read: Basic Electronics Hackaday … Continue Reading »
Paul McWhorter arduino lesson 20 Social