 Hi, today we're going to learn about the ultrasonic distance sensor. This sensor is one of the most commonly used sensors in any sort of tech, electronics or robotics projects. What's really cool about this sensor is that bats and dolphins use the exact same technology. They use something called echolocation to see. What echolocation is, is basically bats and dolphins, they'll emit some sort of high frequency sound, they'll wait for that sound to go, bounce off some sort of object and then come back to them. And by measuring the amount of time it takes for their sound to bounce off an object and come back, they can basically see and determine how far an object is and they can map out the environment around them. What's really cool about this as well is that the speed of sound is constant. So because we know the speed of sound, which is roughly 330 meters per second, we can use this sensor to determine basically how far an object is based on the amount of time. So if it takes a long amount of time for the audio source to go leave the sensor and then come back, that means that the object is far and if it takes a very short amount of time for the signal to come back, that means the object is very close. So if you look carefully at your sensor, you'll see that there are two cylindrical shapes attached to your sensor. These sort of look like eyes. What these two shapes are, are basically an emitter and a receiver. That means like a transmitter and a receiver. What they do is that one of these eyes, right, will basically send out an audio source and the other one will be listening for the audio source. So it's sort of like how dolphins will make sound with their mouth and listen with their ears. This is how this works. One of them is a mouth and one of the other cylinders is a receiver. Moving on, next let's see the actual electrical connections and the pins used on this sensor and how we can connect it to read data off of it. First of all, let's look at the extreme left and right of our sensor. You'll see that there's a VCC pin and a GND pin. These are basically a power pins. VCC is standing for positive and GND being your negative power pin. In the middle, you'll see that the two more interesting pins. These are trig and echo. You probably wouldn't have heard of these pins in any of the sensors. What trig and echo stand for are trigger and echo. Trigger is sort of what it sounds like. When your microcontroller, such as an Arduino, microbit or a Raspberry Pi, send a trigger signal. That means that they're asking the sensor for a value, a distance value. So imagine you've attached and you want to get a distance value. The microcontroller will send a trigger signal. Once this trigger signal is received by the sensor, the sensor will emit an audio wave. So it'll emit an audio signal and then it'll wait for it to bounce back and come back to the receiver. And once it receives it, this is what gets interesting. You see the other pin that's called echo? That echo pin will send back a signal to whatever device you're using. Okay, so now we're going to learn how to actually write code for it. I'm going to teach you how to write code on an Arduino based device. You can also use the same technique to write code for a Raspberry Pi or a microbit. Basically, what you need is a compatible library for your device. So on the Arduino, how we're going to do it is we're going to go into Sketch. We're going to include library, manage libraries. I'll pop up like this will come. We'll put in our distance sensor. It's CSR04. I've already installed the first one. I'll just go ahead and install the second one here. Any library is fine. We're going to go into File, Examples, and find a library here. Click on it, and this is an example code right here. So over here, we can see, first of all, we create an object. In this case, the guy has called the object, 8C. And in brackets, what we're going to do is we're going to initialize our trigger pin and our echo pin in this order. First trigger pin, second the echo pin. So imagine my trigger pin is connected to pin 8. I'll put pin 8 here, and my echo benefits in pin 9. I'll put pin 9 here. Let's give it a nice name. 8C is not a great name. Let's call it Distance Sensor. Distance Sensor, right? And what we're going to do is once you go inside our loop here, what this code is doing is basically printing on the screen our distance value. So let's make this a little bit more easier to understand. Let's create a variable here. I'm going to create a variable called double distance meaning. And this variable will hold the value of the distance that we're currently reading. And how we get that distance is by putting an object which we initialize here, Distance Sensor. So put that down, Distance Sensor.Dist. So that's the function that this library uses to read the value from the distance that we've attached. And add a semicolon, and that's it. So now our variable distance reading will contain the value from that Distance Sensor that it's been read. And the good thing about the library is it does all the processing for you. So you can do all the electronic signal processing and let's give you a value of a distance in centimeters. And then we print on the screen. See what we're doing is basically printing on the screen our variable distance reading. And that's it. That's our code. If you have any questions, you can drop them down in the comment section below. If you like this tutorial, do hit like. And subscribe for more tutorials like this. Thanks.