Ultrasonic Sensor HC-SR04 with Arduino Tutorial | Ultrasonic sensor Arduino code #09
You must know what is ultrasonic sensor, then only you can understand what it is and all so you can see here, uh suppose. If you want to interface ultrasonic sensor with any arduino, you know you can type like this. Like connect ultrasonic sensor to an arduino, you know, then you will get like this images and you can. You can pick any image and you can do the connection that ill. Let you know so before that see uh. This is the ultrasonic sensor why we use this ultrasonic sensor. It will monitor the distance suppose in front of in front of this ultrasonic sensor. If any object is there, it will monitor ill show you in camera. Also, i have connected c. This is this. Is my ultrasonic sensor? I have taken this okay suppose in front of this ultrasonic sensor. Any object is there. It will monitor the distance this distance. Okay, there will be a two pins. One is echo and trigger echo. What it will do, first trigger will see trigger what it will do. It will monitor the distance okay, it will merge the distance and speed. Also then uh. Consider this is my hand, is one object so my hand or what it will. It will take that uh transmitting signal and it will bounce back to the echo signal. Okay, it will bounce back and it will monitor this. We can can locate, transfer and receiving will calculate using formula that distance is equal to speed into time.
By using that formula will find out the distance between object and ultrasonic sensor, okay, so for that purpose we use this sensor. So we can use this see if any blind persons are there, then there we can use it will monitor to that person like whether any object is there uh there we can give one buzzer. So any object is that it will that buzzer will get beep. So the band passer will understand how something is there so for that we can use other many applications. Are there in the robotics? Also, you can use automatic robot there. Also you can use me lots of benefits. Are there of this ultrasonic sensor? Okay, so well see this, how it will work first of all it it it will, it will have four pins. Okay, one will be the ground, one will be the vcc vcc. It will be a five volt. Okay, it needs five volt only and ill show you that also see uh. If you can see, i dont know whether it is visible, because that is a very small see here. Vcc is there then after vcc trigger is there trigger means, transmitter, pin and eco means receiving pins and last is ground. This ultrasonic sensor it needs 5 volt to work okay. So this is the sensor. So i have did connection like this. You can see here see like this connection. I have done uh, i have cut. I have connected vcc to 5v of this arduino.
Then this is the ground and one trigger and eq. I have taken two and three pin. You can take any pin any digital pin. You can take because this sensor is a digital sensor. Okay, so like this connection, you need to do so after doing connection. Okay, ill show you the connection first see. Oh, this is the sensor okay, so i have connected here. This is ground. One is ground and one is vcc. This white is vcc, then here you can see. I have connected one pin that just a second okay echo pin. I have connected to two and trigger pin. I have connected to three okay like this. I have done the connection. Okay, you can do like this then well move now to programming. Okay. So already i have written the program just i will explain how it will work so see here. As you know, first, we need to declare all the pins okay see. Trigger, pin is two. I have connected trigger, pin to second pane of arduino, and echo pin is third pin of arduino okay, so these are the in variables. Pin i have declared okay, then void setup invoice setup. What will will declare will declare whether it is input or output? See trigger? Is a transmitter, so it is output and echo is receiving by receiving the signal from that object. So it is a input. So we need to give this as output, and this is input.
Okay and this you know why we are using this to pre to see the output on serial monitor. Okay, then, next in void loop see. We need to declare the variables we need to take duration and distance. It should be long because value will be anything. So you can take in terms of but try to take long. Okay, then, duration and distance see here. This sensor is a digital sensor, so we have written here. Digital drive, trick, pin comma means we are sending high signal to this trick. Pin okay, it will go! Okay hyphen, then we are giving some distance means it will go to that object and, while coming it will be low. Okay, so by uh doing this pin trigger and trigger, pin, high and low will calculate the distance. Okay, then, for that we are using a formula that is pulse duration. Here we will calculate pulse in eco, pin, comma high. Okay, then distance duration divided by 2, divided by 29.1. So here we are printing the distance. This formula you need to use to calculate the distance, then you need to print. You know how to print that, for that we need to use serial.print. Okay, then, okay, here you can give unit centimeter or meter, and then delay means after one. Second, it will continue. Okay, then next, you need to go in tools select board which what we are using. We are using arduino board, then, okay, what we need to select this com3.
It is already selected, then what we need to do verify. First, okay: it will take some time to verify so after verifying, then we need to what we need to upload. Okay, so were uploading. Now it will upload upload, upload, okay, ill open this okay see it is uploaded now. Okay, now i will open serial monitor here. Well see the output see it is showing 17 centimeter, because here there is one book. So what i will do i will take this like this – will monitor okay, so im keeping my hand here. So you can see here it is seven meter, seven centimeter showing okay. I will increase, it is 10. Okay, just a second ill take sensor here: okay. 11, see 12 centimeter, 16, 18. Suppose, if it is not monitoring properly, then you need to fix the position. Okay. 32. Centimeter. 18 see it is constant. No 18. 18. 18.. So like this, you can monitor the distance and you can find out the distance of object Music. Already, i told you the applications where we can use this in robotics for blind people also, we can use this okay, so i hope you got all the things regarding this project. Okay, okay, so this is the output you can check once again see it is eight centimeter and you can take scale and you can monitor also like this. Okay yeah, it is eight only okay, so i hope you got all the things uh.
If you have any queries or question, please comment it and dont forget to like share and subscribe to our channel and thank you so much everyone.