Everyone hope you are doing fine today today is a new video in our arduino series. Today we will discuss what is the analog input and we will understand what is the difference between analog and digital and how we can read it and at the end we will make project together and check the coding how it can be done. So first lets try to understand what is the difference between analog signal and digital signal for analog signal. Its signal, like its totally random, have different values through the time, so i dont have any expectation. What is the value will be, maybe after five minutes or after even one second, we dont have any expectation its something like my voice. Maybe after next minute will be higher or it might be lower. We do know or like temperature sensor, maybe after five minutes, that temperature will increase or decrease we dont know. So if we try to like draw it or like have a sketch for it, it will be something like this. Something like always going up and down have different values through the time we dont have any certainty what it will be vice vice versa, for the digital signal, digital signal is always clear, always defined and its only have two value either its high or its low. So its always like whether its 5 volt or 0 volt, always clear. So here we have like one important question: if the analog signal is so clumsy like this and its hard to be expected, why we need to deal with it, why we can just ignore it and only deal with the digital signal, which is always easy.

Actually, we need to deal with analog signal, as so many sensors and so many applications in our life always need, like analog signal, like a temperature sensor like touch sensor like voice signal. All this signal is always so we need to understand how we can transfer this analog signal in a form, so my computer or my arduino can understand it so to read this one, we need to make something called conversion from analog to digital signal. We need to transfer this analog signal, this random signal to some kind form, so my computer will understand – or my arduino will understand it, so this operation done through three steps. First, steps is called sampling, okay and what is sampling mean its mean? I have like random signal like this going from low to high then low again, i will take some sample like every one. Second, i will take one sample and i record it. So if i have this record, i can translate this signal and i can expect okay. This signal is going up and going down and i can represent this signal again. So first point is sampling and the more sample. I have the better resolution i will have so i need to take like mini sample, as i can to be. Better second point is quantization quantization. For this point, i need to represent this symbol by value like from 1 to 10. I will have a scale, so i can present every sample with some value and the third point – and the last point is encoding – every point here like here this one like this point – is represented by value two.

I will translate this one to be zeros and one so every point from my sampling item. I will translate it to zeros one and all this operation is called transformation or converting for analog signal to be digital form. Okay, so for my arduino how my arduino can handle this for my arduino if we take, for example, arduino ono, here we have like five bin from a0 to a5. All this five band can deal with analog signal and why only these bends for arduino uno, because here we have inside our ship, we have something called analog to digital conversion. This one is like exactly hardware inside my arduino ship. This hardware task its to translate the random signal to make it like 0, 1, 0, 1, 0 and so on. So i can connect any analog signal. Maximum 5 input signal. I can connect here and i will translate it to like zeros and ones. My arduino will deal with it. This is done through one simple command, its called analog grid – and here i will put the when i have likewise its a zero or of your a5, and what this command will do, this command. It will try to represent any value from 0 to 5 volt. It will represent it from value from 0 to 1023, but lets imagine if my analog signal is higher than 5 volt, maybe its 10 volt. What will happen actually, my arduino will cannot handle 10 volt.

So i need to take a sample of my analog signal, take sample from it and then i can make the operation i wanted to do so lets. Imagine here i have maybe 4.5 volt so for 0.5 volt. Maybe it will represented by value 100. So if we have here value 0.5, then my arduino will understand it as its equal to 100 and maybe throw my code. It will be equal to maybe 37 degree. Okay, maybe after five or ten minutes more, it will be. The value for the voltage will be one volt, and this one volt will be represented here, maybe by value 200, and my arduino will understand and translate this 200 maybe to ‘ resource, and then it will take action. Maybe it will turn on the fan or or turn off the air condition, whatever the action you can do, you can like take action here on your digital bin and by this way we can easily understand and we can easily translate the analog signal to digital form. Now we have one simple project: we will check how it works and in this object we will try to control the light through the photoresistor lets see how we can do it so for our practical parts today, what we are trying to do. We are trying to use this module to control this light here, so this module is photo sensor, module photo resistance and if we have a look here, we can see d0, which mean like digital out of zero, and here we have analog output zero.

But this is analog module how we can have digital output and for this module. We have when the value of this resistance reach 1023. This one. This output will be turned to high, otherwise it should be low and the variable value will be reported through the analog output bin. This toolbar is the ground and vcc for this module and by using this variable resistance and this circuit, we can control the sensitivity of this photoresistance. And here we have our arduino. Well, try always to read the value from analog output here and translate it and take decision wiser to keep this one on or off, and we can see here the power, the main power for our arduino taken from here. This is through vn and the ground, and we can see here. The output is going through bin nine and i cannot actually use the output from here directly to uh drive this load. This load it take around like one amp, but heres. The output maximum output current through any bin – it might be like 40 milliampere, so how to drive one ampere load. I need to use mosfet here im using mosfet and im using transistor, so i will take signal from here and drive this transistor. Then i will use this transistor to drive the gate of this mosfet. Then this mosfet will drive my load. Actually, i can connect directly from a arduino to the mosfet, but its might be a little risky so to be safe and make sure that my arduino is safe and its not burn or anything im using transistor here for safety.

So lets try to connect my module here with this one. Here i have the main power vc on the ground and i have the output will be connected to bin a0. Lets try to connect it here. I have the analog output, i will use, and here is the vcc here we can see the plus sign. This is the vc, and this is the ground im connected im. Taking the main power supply from here so lets try to connect it, and here is my main power supply. It show like initially zero current as its not connected yet lets connect. Now, once it connected, i can see that im like taking around maybe 50 or 60 milliampere. This 60 milliampere used for my main power supply. Here we can see the light is on and also my arduino to be on and my load. This is my photo system, but currently we can see this lambo off. So, if im trying to hide like this, we can see it turning to be on once its on. We can see my amp is reaching to 0.6 and bare or 0.7 based on how much this one is dark, reach almost 700 or 650 am or if its little, we can see – maybe 300 200 based on how much? How much is the sensitivity of light here? Will control the sensitivity of this one so, and i can see here that im like reaching maybe 600 or 700. I cannot drive this one directly from here and also for my lamp im driving it from main bar supply directory im, not driving from this main power.

As this one maximum will take maximum output – maybe around 700 milliamp, so i cannot use it im using from main power supply, and by this we can see its easy to control the light. The outer wood using analog, signal its easy. We can see once its a little dark. It will be on and based on how much the light, how much the brightness here it will control the light intensity here. So this is my uh practical part lets check the code, how it can be done to control the light using photo resistance. Here is my code. My quote is very easy and very straightforward. So first i have like two integer value and its have initial variable. Have initial value equal to zero and what im trying to do? I always try to read the signal at a0 and i will store this value in this variable, which is sensor value, so i always all the loop i keep looping. I keep checking. What is the value for my analog signal, then? I will try to map this one, this value to have value from 0 to 255. So i will translate the value which i get from my analog signal to be equal from value from 0 to 255. Then i will take this value and try to control the bin number nine for this line and this line, if you dont understand it very clearly, dont worry about it. We will explain more about it in the next videos.

Well, explain: uh like in details. What is the map function do, and what is the analog write do, but currently, what i need you to understand is how we can read the analog bin. Okay, this is the main point in our code. Our code is like symbol, as you can see its almost like three line or four line and easily. I can control my light with this code, so at the end, this was our video.

https://www.youtube.com/watch?v=eyJ5hVV6j1Q