Placeholder Image

字幕列表 影片播放

  • please go to the line the computer guy dot com, in order to view schematics, code and Maur for the projects that you are learning about, welcome back.

  • So in today's video, we're going to do a basic setup and coding for an ultrasonic distance sensor.

  • So ultrasonic distance sensors could be very valuable for Aren't we know projects?

  • Because it gives a relatively accurate reading of how far a way a physical object is from your projects.

  • You're able to put an ultrasonic distance sensor on something's, let's, say, an Arduino vehicle, and then, depending upon how close the vehicle is to another physical object, you can then set if else statement on what the vehicle should.

  • D'oh.

  • So maybe if the vehicle is getting close two to a physical object.

  • Let's say the physical object is a foot away.

  • Then maybe you want the art we know vehicle to start turning very slowly.

  • So you wanted wanted to start turning out of the way of the obstacle.

  • On the other hand, maybe if the vehicle is within six inches of that object, you want the vehicle to turn very quickly.

  • So the cool part about the ultrasonic sensors is that you can set parameters so you can say if something is two feet away, do X.

  • If something is one foot away, do why, If something is six inches away, doozy and Oh golly, golly, if something is one inch away, absolutely do l.

  • Right now, right?

  • That is what makes the ultrasonic distance sensors valuable as sensors, unlike something like the infrared obstacle avoidance sensors.

  • Where with them?

  • Basically, they have a bubble.

  • If something gets within the bubble, then then the vehicle will react or the artery no vehicle react.

  • But if the object is out of the bubble, then it won't react.

  • You don't?

  • Actually, you don't have the same accuracy.

  • You don't have the same precision that you do with an ultrasonic distance sensor.

  • So that's why these sensors to be very valuable because you can set in a different parameters for different distances.

  • And then and then your project will do whatever you've coded for.

  • So let's go over to the work bench show so I can show you the basic components of this particular project on, and then we'll coat it and we'll show you how it works.

  • So here are the functional components that you're going to need for this particular project.

  • As always, we're using the basic Arduino, you know, board so you can use another Artemis.

  • No board.

  • But this is our standard default for when we're doing these projects were using a bread board here.

  • Now it's important.

  • Understand?

  • You can actually connect the ultrasonic distance sensor two yard we know board simply using jumper wires without using a bread board.

  • But one of things I like about the bread board is the fact that I can put this just directly into the bread board and it actually gives you a stand again.

  • When you're doing these projects, you're going to run into stupid little problems such as Okay, how do I How do I get it to stand up?

  • And so one of the nice things with bread boards is you can simply just slop that directly that bread board.

  • And now not only can you wire to it, but you've actually got a stand so you can work with the sensor and it makes it a lot easier on.

  • Then finally, of course, we've got the ultrasonic sensor.

  • Now it is important The ultrasonic sensor that I'm showing you today is the HC hyphen s r 04 So there are different ultrasonic sensors out there.

  • So is this ultrasonic sensor.

  • I forget what model this is, but this is a different ultrasonic sensor.

  • You'll notice the eight c hyphen s r 04 has four pins on it, and this particular ultrasonic sensor has five pens on it s o.

  • It will work slightly differently.

  • So this is one thing to remember whenever you're dealing with sensors.

  • There are a lot of sensors out there where where basically the technology they use is the same.

  • So you'll see this with infrared sensors to where you'll go out and you'll buy an infrared sensor.

  • And the infrared technology part is the same.

  • But in the actual board, the actual module may be different, and so then you may need to wire it and code it slightly differently.

  • So this is just one of those things to keep in mind.

  • So if you're gonna be doing this particular project, But that too aside, go for the eight c hyphen s r zero, for this is going to be a model that we're using.

  • So these are the main functional components to it.

  • But those two aside and then this is the actual project itself.

  • And it's a very, very, very simple project.

  • So if you uh if you pull off your ultrasonic sensor on the back of it or possibly on the front of it, it will show you what the pins are.

  • If your particular censor it does not have this, then you may just have to go thio the documentation and take a look.

  • But these pins should be the same.

  • So basically, you've got ground on one side, see of ground.

  • You have eco, you have trigger and you have vcc.

  • So ground and vcc they're going be plugged into ground is gonna be plugged into the ground on your doing aboard.

  • VCC is going to plug into the five volt on your Arduino board and then the trigger and echo they will go to your digital pens and then all you're gonna do is make sure your lawn this up properly.

  • And now this is literally everything you need to basic t build the simple little project.

  • So with that, let's go over.

  • Take a look at the code to see how this works.

  • So here we are.

  • This is the code that we're going to be using for this particular project.

  • It is important to remember what's particular project is.

  • We're not going to trigger any physical events were not turning on Ellie desire turning on buzzers or anything like that.

  • All we're going to be doing is we're going to be reading from the cereal monitor.

  • So what this shows you is that the sensor can detect distance, and it could turn that distance into the value for a variable going into other projects in the future.

  • We will then be able to test against the value of that variable, for if else, if l statements on, then actually be able to do something based off of that number.

  • But for this simple project, all we're going to be doing is we're going to be printing out to a serial monitor.

  • So we go up here.

  • The first thing, of course, that we need to do is we need to define the digital pants.

  • We have the trigger pen and we have the echo pen, so pound define trigger pin goes the digital pin £8 to find echo pin goes digital pen nine that tells the Arduino board where where these pens are on the Arduino itself.

  • The next thing that we're going to do is we're gonna have to create three variables we're going to do a long four duration.

  • So there are different variable types when you're dealing with hardly knows when numbers are concerns.

  • You have ent, you have flowed and you have long.

  • So an end is a whole number of between 32,000 something and negative 32,000 something, right?

  • So 10 is an end.

  • 32,000 is an end.

  • Negative 32,000 isn't it?

  • Float float is a decimal, so 10.1 is afloat.

  • Ah, 1000.2 is a float, 30,000 0.5 is afloat.

  • What long is long is a number that's larger than an end, eh?

  • So basically, if you need to be in the store a whole number that's larger than 32,000 then what you're going to do is you're going to have to have a long variable time.

  • So for the duration here, we're gonna have a long day variable type.

  • And so we're doing you sicko long and then we're going to create the variable duration.

  • Then the next two variables that we're gonna be creating our floats.

  • So again, decimal points, we're going to a float for distance and centimeters float for distance and inches.

  • So what's gonna happen is we're going to create the value for the variable duration, and then we're going to turn that value in two centimeters and end the inches in the code below.

  • Then all we do is we go down here, we set up the environment.

  • So we're under pen mode, trigger pin.

  • So the trigger pin is going to send the ultrasonic signal out.

  • And so we make that output.

  • We do pin mode, echo, pin.

  • So echo PIN is receiving the ultrasonic signal.

  • And so we're going to set that to input and then cereal that began at 9600.

  • So we can actually read from the cereal monitor.

  • Then when we go down here, this is just the basic code that you're gonna need to put in if you're going to be dealing with ultrasonic sensors.

  • So what we have is digital, right?

  • Trigger pin lo delay microseconds.

  • So it's not delay because delay is in milliseconds this is in microseconds, so this is faster, so delay microseconds to so delays for two microseconds.

  • Then it goes the digital right trigger pin high delayed microseconds 10 for 10 microseconds digital right, trigger pin lo.

  • And then what we're gonna have here is we're now going to set the value for the variable duration to the value for the variable duration equals pulse in function, echo pin high.

  • And so this this is how we're able to get the value of the variable duration and then we can actually start working with that value.

  • So once we know what the value of duration is, we don't need to turn that into something that is useful to us.

  • So it's useful to us is distance and centimeters or distance and inches.

  • So we're going to do for centimeters.

  • So flowed this variable distance centimeters equals duration times 0.34 divided by two.

  • And that is going to give us how many centimeters away.

  • If the object, it's then distance inches equals duration times 20.133 and divided by two.

  • So that will give us how many entries away an object is.

  • And so that's the important thing again, whenever we're dealing with are you know, many times we're going to get a value for a variable.

  • But that that value itself isn't doesn't mean anything to us again.

  • Duration, duration isn't something I can work with.

  • I need either centimeters or I need inches.

  • I can work with centimeters.

  • I can work with inches.

  • Just having that that that duration number doesn't mean anything to me.

  • So then all we're going to go do is going to go down here and then we're going to serial print everything out so you can see it's a serial dot print, so distance.

  • So it's going to say distance, colon space and then it will print the distance in centimeters, the value for distance, cm.

  • Then we're gonna print out a couple of spaces, put CM another couple of spaces, but hyphen put another couple of spaces again.

  • Just basic formatting stuff got put in the code.

  • Then we're going to do serial dot print distance the value for the variable distance inch that's gonna print out they were on a serial print line, so print Ln what's gonna happen is going to print this out and then it's gonna go to a new line.

  • And then, you know, basically we put a couple of inches there or we put a couple of spaces there.

  • Then we say inches and then we'll go to the next line.

  • The next thing that we do here is we're going to do a delay.

  • So the delay here is in milliseconds, eh?

  • So we're going to say 1000 milliseconds.

  • This means one second.

  • Now, with this delay, you can put this delayed.

  • Whatever the hell you want.

  • I put it to one second so that I can actually read what the hell's going by on the screen.

  • So it's important, Understand?

  • With something like this again, if it's gonna be an autonomous vehicle, you would want the delay to be very low.

  • You'd want the delay to be maybe 100 milliseconds or 50 milliseconds, something like that, because you want the loop to go through as quickly as possible.

  • The thing is, is when I'm actually trying to physically read what what's going on on the screen.

  • I need the output to be slower than that.

  • So that's why up 2000 milliseconds or one second so that all you do is you then upload, do the board.

  • Everything should go through.

  • And then with that, we can go to tools and we can go to serial monitor and we can see the distance the object is away from.

  • From from the ultrasonic sensor going to this shot, I think I think everything will be a little bit easier to understand.

  • So we have our ultrasonic sensor here and again.

  • When I put my hand right in front of ultrasonic sensor, you can see how close it is.

  • And so it's reading at 4 to 5 centimeters away and about two inches now.

  • One of the things that you have to realize with the ultrasonic sensor is different types of material will reflect the ultrasonic signal differently.

  • So that's one of the reasons why I say the ultrasonic sensor is relatively accurate.

  • The if the material is made out of out of different things that can affect how the signal works with material.

  • So you may get different readings, depending on whether you're dealing with a very hard material or made where you're dealing with cloth something like that.

  • But basically, if we go here, I mean we can see.

  • You know, if I put this thing's box in front of it, you can see it's reading of about 4 4.5 centimeters, about 1.6 inches.

  • And then as I pull this away, you can see the number goes up and up.

  • Uh, one of things to be re thinking about when you're dealing with the ultrasonic sensor is to realize that what's happening is basically, it's sending an ultrasonic sound waves out one.

  • It's reflecting off the object in coming back, so you have to be thinking about how the sensor actually works.

  • And so if it's going and reflecting, one of things you have to think about is what happens if it starts hitting, hitting the object at an angle, right?

  • So So the signal goes out, it hits the object at an angle, and then it veers off into a different direction.

  • So that's why we're now starting to get weird.

  • Weird sayings are weird readings, and so this is one of those things you're gonna have to be thinking about whenever you're dealing with sensors is what are their limitations.

  • How do they interact with different materials?

  • How do they interact when they're looking at objects from different angles, and then you're gonna have to coat.

  • You're gonna have to figure out how to design your project appropriately so that you don't run into problems there.

  • But that's basically the overall of how the ultrasonic sensor works.

  • How Thio action, How to build the project and then How to Code for.

  • So there you go.

  • You learn a little bit about the ultrasonic sensors.

  • Like most stuff with the Arduino world, it's really easy to set up.

  • It's really easy to code for.

  • What becomes complicated is when you're trying to build this into a much larger overall project and then figure out you know what?

  • Sensors take precedence over other sensors creating those, if else, if statements doing all those kinds of things.

  • Basically, that's the important thing you have to understand.

  • With technology, a lot of people get confused when they're learning.

  • Technology is that each individual thing is very simple.

  • Ik individual thing and takes you 10 or 20 minutes to learn.

  • Where it gets complicated is you.

  • Then learn ah 100 or 1000 different things and then figure out howto build a product based off of all those things and having them interacting.

  • Yeah, there, that's that's where it gets really complicated.

  • So basically, these little sensors are absolutely great if you are going to be using them for projects, though, the thing that I would recommend is that you experiment with them and you play with them and you realize how they interact with the world again when they're when they're looking.

  • When they're looking at physical objects again, how do those objects have to be to make sure that they're actually seen by the censor someone and so forth?

  • Because that's one of the things that you'll run into with these sensors is they work?

  • When they worked for when they work exactly how they're designed, they were perfectly.

  • But as with everything you know, if if they're doing something a little bit differently, you may get some really weird readings because of that, and then you have to figure out how to design your overall project to be able to compensate for that.

  • So again, if you're gonna be using ultrasonic sensors on something like on a time his vehicle, what you realize is having a single sensor on that autonomous vehicle probably is not going to be a great idea.

  • You know, if you're gonna be using ultrasonic sensors, maybe you need an array of three ultrasonic sensors on the rate of five ultrasonic sensors.

  • So all those sensors could be reading from different angles.

  • You can bring in five or Maur different inputs and then figure out what the vehicle should do from there.

  • So these are some of the things to be thinking about it.

  • You're gonna be using the ultrasonic sensors, but they're a great little tool.

  • It will definitely be using them in many projects to come.

  • So is always enjoyed doing this particular video and look forward to seeing the next one.

please go to the line the computer guy dot com, in order to view schematics, code and Maur for the projects that you are learning about, welcome back.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

HC-SR04 超音波距離傳感器,帶Arduino。 (HC-SR04 Ultrasonic Distance Sensor with Arduino)

  • 4 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字