Arduino Ir Sensor Working of IR Sensor | Arduino
We are going to create a project using ir sensor, so this is a sense so like, if i put in my hand in front of it, it will detect it. So it is like obstacle it. It uh senses the obstacle in front of it, so i uh, so you might be thinking of what does this ir means? So this means infrared rays, race, infrared rays. So what does this sensor actually do? This white led sends some rays and when those rays hit an object and come back, those rays are absorbed by the black led and that the time it took the speed that it have is something which it which it calculates. And then we get to know about it so using this potentiometer we can adjust up to that like we want a really long distance or a really short distance, its just up to us. Okay. So, like uh like, if you have created a project and your ir sensor is like you might not working, then try to adjust this put into meter, okay. Firstly, let me tell you about the working of this and yeah before that. Let me show you: how does it actually work? So i have this nine voltage battery and connecting it to my arduino. I already have uploaded the code in the arduino so now im just giving it power, but if you have not uploaded the code in the arduino, then firstly you have to upload it then okay, so this is the ir sensor when my hands come in front of It so the white, the white led sends some rays and when those rays hit my hand and goes back that black led receive it and the and my red led is something in the code i have written if something comes in front of it, glow the red.
A little if something is not, it will not glow, and this is the working of this okay. So now lets go to the connection and the components that we need. Okay. So, first of all, for that you need the arduino board, which is the brain of our project. Secondly, you need one breadboard and you need some jumper wires. Dont get panicked about seeing these many eyes. We are going to use few of them and you need one led. You need one ios sensor and you need one resistor. Okay, now uh, let us connect them. Okay and the value of this resistor is 220 k. Ohms resistor, it is 220 k, ohms resistor. Remember we always read the value of this resistor by seeing its color bands – okay, uh, you know in our upcoming videos ill tell you about how to read them as well. Yes, now lets just focus on this project, so firstly, lets give the power to our breadboard. So, for that ill take up two wires im digging two wires, which is gray and red. Color, now see your arduino board and that you will get some power pins youll get some power pins. What do you actually do is connect the wire to the five volt of your power pin to the five voltage pin now it is connected to the five volt. Okay, now take another wire and put it in the gmd. Put it any g, and there are three gnd present in this whole whole arduino board two are present over here and one is present over here, which is the ground.
Okay till here we are done with over here now coming to the breadboard. So you could see one plus and a negative sign, so this red is the five voltage ill put down in the second row of this, and this is the gray y, which is g and d ill connect it over here now. Let me tell you about this breadboard, so we have these these columns and these columns are connected to each other, not the rows. Only columns are connected to each other like this is the first column second, and they are different columns and in this the rows are connected. The first row is connected to each other and the second row is connected to each other. So i have given red wire in the second row means five voltage to the second row and means in the second row you could get five voltage and in the first row you can get the g and d okay, i hope its clear. Yes. Now let us take this ir sensor and connect it so in this ir sensor. What do you see you see three pins you are able to see three pins right first is the out. Second, is the gnd? Third? Is the vcc? Now you are going to connect this out to the digital pin, so, firstly, what ill do is ill connect it over here and yes now i have out gnd and vcc. We are going to connect out to the digital pin gnd to the gnd, only and vcc, to the phi voltage, and this is all and make sure your potentiometer is at the right place like if you have connected uh.
If you have, if your connection is done and still the highest sensor is not working make sure to adjust your potentiometer okay and yes, now lets connect it so take this out, pin which is green, colored, pin uh green color wire and connect it to the im Connecting it to the third pin of my arduino ive, connected to the third pin of my arduino, then the second pin that we get is the blue, which is the gnd pin? Okay, so this is the gnd pin ill, simply connect it in the first row, because i i have powered this first row with the gnd and we have the vcc pin, which ill connect to the phi voltage and yes. So second row is, with the five voltage and ill place it over here. Yes, now our is sensor is completed now. Lets take the led yes, so this is our led. You will be able to see, there is two pins longer and the shorter pin. There is two pins longer and the shorter pin now what actually uh they tell is the longer one is the anode means that is the positive. The shorter one is, the cathode means it is the negative, i repeat again, the longer one is the positive. The shorter one is the negative. Okay then, coming to my breadboard place it like this. So this is my positive. This is my negative ill place it over. Here and now my led is connected to the breadboard, now ill.
Take this resistor so in this resistor. What actually you see is there is a golden color or a shiny color in your resistor, and there is this terminal, and this terminal is the terminal number two. This is the terminal number one. I repeat again, this is the terminal number two. This is the terminal number one im coming over here, so this side is the positive. This is the negative, so ill connect this resistor to the positive side. Now the terminal 1 of this resistor will be connected to the positive of my led and terminal 2 will be connected to the other column of my breadboard lets connect it. So this is the this one is the positive side and yes terminal. One is connected over here. You could just uh connect like this as well. Okay, yes, now this is connected with the positive. Yes, it is connected and i have connected to the other column as well. This is the another column. This is they both are not similar, but now they are connected to each other. I have added a resistor to it. I have added a resistor to it, okay and then ill. Take these two eyes: yellow and white. Okay, ill connect this white to the negative of my led and this yellow wire with the terminal two of my resistor um. Yes, let me tell you about this resistor now, so what this resistor actually is doing like, for example, i give this led a power of 5 voltage.
Okay, for example, i give this led uh five voltage power and my led can take only up to 3.3 voltage. Now the problem occurs now uh like. If i directly give you the five voltage my led could burn up for that. What im doing is what i added a resistor. Now the current will not directly go. It will go slowly. It will grow slowly and like if uh 5 volt is coming using this resistor there will be a resistance and 5 volt will not be coming over here, so it makes my led safer and uh led will not like boom up. Okay, yes, so, as i was here so connect this white, which is the negative to the gnd first row and the yellow to the 13 number pin of my arduino to the 13 number pin of my arduino. Yes, we are done with all the connections now lets power. It uh, as i already have uploaded the code, ill, just power it and show you up and like if you are creating it for the first time it will not work because we we have to write down the code and upload it inside okay. So, yes, i powered it. Yes now it is powered now lets see the working. Yes, the connection is okay, our led is glowing my hand is coming in front of it. It is working Music. Okay, now lets jump to my screen. Yes, so now we are on the screen, lets write out the phone.
First of all, we are going to be laying out the things the first pin that we have is the led or you can give it out as red. You could give it as red, then it is connected to the pin number 13. Okay. Next, we have ier10, we have connected the ir to the pin, and that was all the declaration of pins now over. Here we have to get all the things that we happen and we can just use this means to call them. We dont have to write the these 13 and well be confused evenly. When we use this, when we use variables, we do not get confusion. Okay, so i hope thats clearly now in the void setup. What, after you do the code run only once, but over here youll be clear that this thing is going to give me output. This pin is going to give me input and like if you want to begin a serial monitor, then you go up with this okay ill tell you thing ill write, pin mode. So i want this red led to give me output because it will glow. It will emit light, which is a output. Okay, then what actually will do this ir sensor is going to take the input. Okay – and i put semicolon – and always remember, to put a semicolon after that. So we have declared the thing that this is going to be output. This is going to be input, and this was all the declarations.
If you even want to begin a sales monitor, you could begin uh lets just before. First, we do with this ill. Tell you, with the senior monitor after dont, worry about it now in the while loop. What we do is well write, the, if condition like digital uh, digital read, what we it needs to be i written, is equal to no. So what is happening is when some object comes in front of the ir sensor, so it produces low signal so when it will be low signal, we are able to get that there is something of the obstacle is in front of the ir sensor. So what we will do, we will say digital digital right and we wont read a little growth, so ill write red to be as high just with all the food and then you could give out the delay as well uh. It would give a nice bit to your everyday means when it will glow and then it will not long. It will give you a nice effect of cloning yeah. You could go up with this, so uh. This line also im. Writing it to make it look better. Then just else condition you dont have to write anything else in this practice. Its very simple in this just the opposite of this will happen. Red will become low, so this was all the code that you would like void. Loop is something that runs again and again and again: okay, okay, now lets connect our arduino with the computer and then just go to the upload button and upload it.
So, firstly, it will be compiling the sketch, it will be checking if there is an error. So there is no error, so now our code is done and you can see it is working. Fine, okay and you could give it a 100 as well uh its a little slower. If object comes in front of it delay, it is in microsecond ill write down. Micro seconds it is in microseconds, i hope, thats creating okay. This was all with that. Now lets try it with the serial monitor. Yes, you heard right serial, monitor ill show you. What is the serial monitor, so? Firstly, we write down cereal dot, begin cereal or begin in this. We write down at what rate it will. What actually it will do is it will start communicating? Our computer will communicate with the arduino at this rate and this at the speed, okay and then ill write over here serial dot print. There are two ways of trying printing. This first is the print. Second is the print element. The first is now. What is the difference between both of them? Okay, single print, it will uh if i write serial print ill ill, show you with the example, but let us firstly do with the ln print ln and in this i write down in the double quotes because im writing a string. So i its high thats the same thing over here, but it will be low lets upload the code.
Now yes, now uh, when your code is uploaded, i upload it again and like when your code is uploaded. Okay, now do come over here. You will see us see the monitor a magnifying glass click on it. Yes, so we are getting value load now, when i put my hand in front of that now, it is showing me how this is my serial monitor. I hope, uh its clear to me. So now we have done it using the element now lets. Do it without any printing lets. Do it with only pin and now upload the so it is code done uploading. I go to my screen, monitor and see this without print telling. It would look like this yeah, so thats the difference so ill. Tell you now what is this doing this? Firstly, it is printing this line. Firstly, it is printing the height. Then it gives a uh. Then it presses the enter key and comes on the next line, and then it is on the next line. It prints this line and when this line is printed, it goes to the next line, and this is what is happening again and again and again yes. So this was for todays video.