 Wait for it, it's gonna come into the shot. Look at that. If I put my phone out of the way, right away it goes to the wall, instantly. This is an ultrasonic sensor. It uses sonar to detect a distance from one object to another. What happens is super high frequency sounds come out of this side, the T side, the transmitting side, shoot onto an object, bounce back into the R side. And using the amount of time that the sound frequencies were in the air until they were received, we can detect distance. This is kind of just like what bats do. They don't actually have vision. They don't have eyes they can't see. So they use sonar to fly around, which is quite amazing thinking about how that all works. Like they just use sound to figure out visually how everything around them looks. The typical range of these, at least these cheaper Arduino ones are one inch to 13 feet or two centimeters to 400 centimeters. Most materials seem to do fine. Weird shiny materials had a weird reading for me, but usually it's soft dampening materials that really ruin this. So if you were to shoot this at like a blanket or something even softer than a blanket, it might not come back as a good reading because it's so soft that it'll dampen the sound. It emits the super high frequency, by the way, that we'll never be able to hear. Cars tend to use these kind of sensors as proximity sensors. So when you're going to park behind another car and you're backing out, backing up and you're getting too close to the wall or to whatever the car it's gonna start beeping and as you get closer it beeps more. That's what they use. This is kind of the same technology there. An alternative to these sensors, I guess the only other one would be using like an IR sensor. Like you wouldn't use this as a, like you wouldn't, an alternative to a ultrasonic sensor would probably be an IR sensor, like we used in that outdoor light video where we had a light activate based off of our movement. Right, we're walking, we're walking. You do your poopies. Oh, it turned on. We have light. We can see. We have light. That light worked off of heat radiation. This works off of objects. This would activate if anything goes in front of it. The IR sensor activates if it sees heat in front of it. All you're gonna need for this project is actually pretty simple. You're gonna need the sensor, four cables and then I'm gonna be using a breadboard to kind of prop it up like this. Like always, we've got to plug it into our Arduino. So we're gonna go and just plug in, first of all, the four ports. If you go and look at it, if you look on the back, you can see ground, echo, trigger, VCC. We're going to be using all ports this time. We're going to be plugging in our red into the VCC. We're gonna plug in our ground, which in this case is a blue cable into our ground. And then we'll have brown on echo and yellow on trigger. Now on the Arduino board, you're gonna be plugging in your red and ground first. So red is gonna go to five volts. Ground is going to go to ground. Then we'll be plugging in our echo, which is the brown cable into port six and our yellow, which is the trigger port into port seven. That's all you gotta do. Plug it into your computer, open up your Arduino editor and then we can just write the code for it. When it comes to the code, this is actually probably one of the most straightforward videos we've done for an actual sensor. All you gotta do is declare, first of all, your ports. So we used port number seven for the trigger and then we used port number six for echo. In your setup, you're gonna have to set up your console like always, so just do serial dot begin. And then we always like to do 9,600. The one complicated part is we're going to be taking time and turning it into distance. So I already have functions, I'll show you how those work. I'm not gonna make you guys do that, but we're gonna have to write the code, first of all, to capture the data coming out of the sensor. The first step is gonna be to grab the duration. First thing is we're gonna want to declare duration. We're going to want to add inches and then we'll do centimeters for the Canadian boys in chat. Usually we do pin mode within the setup, but what we're gonna be doing is on every loop, we're going to be capturing, usually we do pin mode in the setup. We set up our pin and then we activate it in the loop. This time we're gonna be doing everything in the loop because we're gonna be constantly turning on the sensor and turning off the sensor. We're going to be turning it on, sending out an input, waiting for it to reflect back in the receiver, turning it off and repeating the whole process. So that's pin mode, trigger and then output, then digital write, trigger and low. And then we're gonna want to delay it a couple microseconds. Let's do two seconds would probably be enough. Digital write and then we're going to be putting inside of this the same thing, the trigger. Well, we're gonna be turning it on this time. And let's go and just copy paste this delay microseconds. And we have to do a longer delay here. Shorter delay seems to give me an issue, so we'll do 10 this time. And at the bottom here, we're going to just copy paste this because we're lazy and repeat it with another low command T. We'll actually move that over for you. Then we need to use pin mode on our second pin, which is our echo. And we're going to make that an input pin again. We're going to use this one method in the Arduino library that we've never used before. It's called pulse in, just call your duration. And then we're going to do capital I pulse in. Pretty much what pulse in does is it waits until we're on the reverse power. So whenever we want to trigger a pin, we have to put in the word high. And then when we want to turn it off, we have to put low. It's going to record how long it goes from high to low or from low to high, depending on what you tell it. Here we're telling it from high to low. So what we're going to be doing is turning it on. It's going to be recording for how long it's waiting for a signal. I want to receive the signal. It's going to turn itself off, which is going to be recorded in the pulse in. Down here we have two functions we're going to be using called microseconds to inches and microseconds to centimeters. These are just going to convert time to distance. So you're going to want to copy paste these functions, maybe pause the video for a second, but these two functions are going to be taking our time and turning it into a distance. So the amount of time that the sound frequency is reflecting off of a wall and then gets received that amount of time is going to be captured using pulse in. And then we're going to put that into these functions and get a distance back. So let's go and first do inches. We're going to have to call the function here. I'm just going to copy paste and then we're going to throw in there duration because that's how that's what we're going to be recording. Next up is centimeters. It's the same exact process. Grab this function and then inside of it throw duration. The last step is we're going to be printing out these values in the serial monitor like we've done on every single video up to now. So we could just see the distance and we're going to have inches and centimeters as options for Americans or Canadians or whoever's watching the video. All you got to do is make sure it compiles which I'm hoping for crossing my fingers. Looks like we're good. We're going to upload it to the Arduino. It might break. It didn't break, but if it did for you, go to tools, board, make sure you're on the right board. I'm using an Arduino UNO and then make sure you're on the right port. For me, it tells me I'm plugged into COM 6 so I have COM 6 selected. We're going to save that one more time. All right, it's uploaded. We're going to open up the serial monitors, tools, serial monitor or control shift M and we can see a bunch of numbers printing like mad. So I'm going to grab my sensor and I'm going to point it a couple different spots. I'll actually show you on the screen here. Here's my hand and I'm going to move my hand closer and closer. So it's at six inches, which I would say is accurate. Move it closer, move it closer, move it closer. See it's going down three inches, two inches, one inch. We can actually try and see how accurate this thing is. So I have my phone here. We're at 12 inches right now. It's saying 12 inches, 31 centimeters. Let's move it in a bit. We're at 10 inches right now. It's saying about nine, 10, it's right on the edge. Wow, look how accurate that is. See that? It's on five inches. Move it up to three inches. It's on three inches, move it just a bit in and it's going to jump down to two and then all the way down here. And we're at one. That's pretty crazy accurate. Move my phone out of the way and everything else out of the way and it's getting the wall on the other side of the room. And as I move my phone back in from all the way far here, move it back, move it back, move it back. Wait for it, it's going to come into the shot. Look at that. If I move my phone out of the way, right away it goes to the wall instantly. All right, well, if you liked the video, please do give it a like. If you want to see more of this content, you want to see more sensors. Any specific sensor you want to see, let me know in the comments. If there's anything you didn't like, always let me know. Trying to get better with these videos and I'll see you guys in the next one.