Arduino Tutorial 10: Understanding How To Read Analog Voltage using analogRead Command

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and I am here today with lesson number ten and our new our improved our exciting tutorial series on the Arduino one of the things we're trying to do in this series is we're trying to bring a little bit more math and physics into the lessons we're trying to understand a little bit more about what's going on under the hood in the Arduino what we are going to do today is we are going to learn how to read analog voltages using the Arduino so I need you to pour yourself a nice big mug of iced coffee refreshing that strong black coffee poured over ice no sugar none as needed try it it's refreshing and it is delicious so pour your coffee and then also get out your a Lego kit your a Lego super starter kit if you haven't gotten one yet look in the description click on the link hook a brother up get it there it's about 35 bucks and we will be using this for this entire series of lessons so we are going to learn a lot with that kit but today what we are going to learn is how to read analog voltages now let's review the things we've learned in the earlier lessons we've learned how to do digital Rights which is taking one of the output pins and setting it high or low we've learned how to do analog rights where we can sort of get the in between voltages and then we have learned how to do delay so the things we've done so far have been sending signals to the Arduino pins but today we're gonna learn how to read from the Arduino pins because you might have a sensor hook to it and the value coming off the sensor might be a voltage so you need to be able to read that voltage and so analog read is a very important function and to illustrate how to do analog read we are first of all going to get out of your way and then we are going to go back to that circuit that we built in Lesson number nine and in case you were not here for lesson number nine if you get lost here about this circuit go back and watch the lesson number nine or you can just kind of keep up with it here but this is the circuit that we in Lesson number nine we got 5 volts off the Arduino we then have resistor 1 at 330 ohms in series with resistor 2 of a hundred ohms and I showed you how to do the circuit analysis such that you learned that for this particular circuit the voltage across resistor one is going to be three point eight four volts the voltage across resistor two is going to be one point one point one six volts and then we built the circuit and we verified it worked with this most excellent mass tech digital voltmeter the digital voltmeter is a great tool but you don't want to have to go pull out your voltmeter every time you're doing a project so you need to know how to read a voltage using the Arduino so first thing I need you to do is if you don't still have your circuit from lesson 9 I need you to build it real quick it's very simple like we talked about here you can pause it and build it you're illegal kit has hundred ohm resistors and your aleko kit has 330 ohm resistor so you can build it with the the components from that but we bring 5 volts off of the arduino the 5 volt pin it's labeled 5v we come over to column 15 of the prototyping board doesn't matter which one you use we use column 15 but what does matter is the one leg of the resistor needs to be in the same column as the wire coming in then we go across the resistor we're kind of driving through the circuit here we go across the resistor second leg of the resistor is in column 20 and then column 20 also has the first leg of the second resistor r2 then we jump over this divot don't jump over this ditch where there is not a connection and then in the lower part of the column 20 we have the other leg of the resistor and then we have this white wire also in that column and then bring that back to the G and D or the ground it's the pin next to pin 13 on the Arduino and so when we probe this with the mast tech voltmeter we see that we actually get the voltages that we anticipate but now we want to ask how can we measure these voltages using the Arduino well what we want to do is we want to probe this point right here that connection point between the bottom of r1 and the top of r2 and then that will allow us to know what the voltage is between this point and ground which would be the voltage across r2 which would be v2 which we anticipate would be one point one six volts so we come over here and this is the point right here that I want to connect right I'm connecting to the bottom of the first resistor in the top of the second resistor and then I need to plug this wire this wire into the Arduino all right now what pins have we used so far the pins that we've used so far are the digital pins pins 2 through 13 we can do digital rights to all of those and then the ones with the Swick wigglies we can do analog rights too we cannot read analog voltages from those we need to read analog voltages from these pins on the other side which are labeled a 0 to a 5 any of those we can read an analog voltage from so just for fun I am going to choose a 3 so we will be working with 10 a 3 to make our voltage measurement so I have it plugged in and we are ready to read but in order to read now we need to do some coding ok we need to do some coding so we need to come over to our coding window here and we need to start writing our program so we will come up to the top and what do we do at the top we declare our variables all right so one variable we need for our pin we use ents or integers for our pin names and I'm just going to call this read pin okay because it's the pin I'm going to read from and what is that equal to that is equal to a 3 we are setting read pin to a 3 don't forget your semicolons then I'm going to be reading a voltage from that pin so I need a variable to read the voltage into and what would be a good variable name well what are we trying to find v2 so let's call it v2 you can name your variables whatever you want but just make sure that you name them something that is descriptive like just don't call it XYZ or your cat's name or something like that so I'll call this v2 and we'll just set that initially equal to zero just to set the variable up okay now we need to come down to our void setup and what do we need to do in the void set up our pin modes right how many pins am I using I'm using one pin so I'm gonna say pin mode which pin am i doing Reed pin what is this two different this time than all the other times all the other times the pins have been outputs we've been sending data to them or sending signals to them they've been outputs this one is an input all caps we get the happy little blue color alright so there we are and then we come down here and what do we want to do we want to do an analogue read okay but when we get that value from that pin we're gonna go out to that read pin and we're gonna get a value we got to put it somewhere where do we put it we put it in this variable V 2 so how do we do that we say V 2 is equal to what analog read and there's one parameter that you need for an analog read command and that is where you're reading from where you reading from a 3 a 3 no no you don't use constants there you always use your variables what did we call it breathe pin okay and then we put our semicolon all right so this is going to sit here it's going to loop and read analog read analog read analog read and so it's going to be reading into V 2 V 2 V 2 V 2 it's going to constantly be updating this V 2 what is our problem now we don't have any way to see it okay because you haven't learned how to do that yet so in the next lesson I'm going to teach you all about using arduino print commands where you can use the arduino to print numbers to your screen that you can see all kinds of exciting things to learn there but I got to give you a little bitty preview I have to give you a little tiny little preview in order that we can print out this this this v2 that we're reading here so this is your quick mini lesson on how to do a print in arduino you need to come up to your your void set up and you need to set up your serial monitor and you do that with serial notice the capital s serial turns orange happy little orange color and then dot begin oh look begin turned orange to happy little color serial dot begin it recognizes that and then you need one parameter which is the baud rate that's how quickly you are going to be talking to your serial monitor and 9600 is a good one we can talk more about what the different baud rates are that you can use in the pro's and con's later but here very simple we're setting up our serial monitor where we can print to it now what we do is we come down here and after we have read v2 we need to what we need to print v2 and we do that with serial a happy little color capital S dot print L in that means print a line that means go to a new line each time you print print L in and then tell it what you want to print what do you want to print v2 okay so what is this loop going to do you're gonna read v2 from pin read pin which is a three and then you're gonna print it and then you're gonna read it and then you're gonna print it it's just gonna read print reprint reprint what's one thing that might be a little problem it's gonna be reading and printing so quickly that you might not be able to see it so we need to put a little delay in there so let's say D delay you know this command and then what would you say about half a second that would be 500 milliseconds right are you happy no no what did I just do I used a constant down here we use constants we use variables we need another variable up here at cop int and we call it King call it delay time like that okay so now and we can initially set that to what 500 that we wanted all right so then down here instead of putting in 500 we put in delay time because we're going to be good programmers and we're going to be using variables and we are not going to be using constants so what is our void loop going to do it's going to read from read pin which is a and a 3 and then that's going to put it in the variable V 2 and then we're gonna print V 2 and then we're going to pause so it's like read print pause read print pause read print pause now we are going to download this into the Arduino and we're gonna hope that we get the happy little bar and then we are going to download it hold your breath boom it downloaded our code is correctly our code is correct and so now we want to look at these values that we're printing and the way that we look at it we open the serial monitor with what looks like this little looks like a little magnifying glass up here you click on that and then you will pop open your serial monitor which can now be seen here and look at that we are sitting and we are reading values and we are printing values in the number we are reading is 290 292 houston we have a problem because we could not be getting 290 volts out of the Arduino ok I will explain it the analog read does not read the actual voltage it reads a scaled value of it and it reads a number between 0 and 10 23 okay and that's like court 10 23 is what let me do that real quick should know this off the top of my head you know I should know this off the top of my head too that is a 10 bit number okay because 0 to 1023 that is 1024 numbers and 1024 is 10 bits it's 2 to the 10th okay so that's how you end up with these strange numbers like 255 and 10 23 well 10 23 0 to 1023 is 10,000 24 different numbers which is 2 to the 10th okay does that make sense and so what you have to see is is that if you had 0 volts if you had you can't see what I'm writing can you let's go back over to a different view here where you can see what I'm writing okay so if you had 0 volts what would you read with an analog read so this is actual voltage actual voltage if you your actual voltage was 0 your read value would be 0 if your actual voltage is 5 your read value would be 10 23 so let's see if that is really the case and so what I am going to do is I'm going to take this wire from a 3 and I'm going to hook it up here to the top which is 5 volts and then I am going to come back over to the other view and what are we reading or reading 1021 that means that this isn't quite putting out exactly 5 volts it's a little short but you can see that the 10 23 would represent exactly 5 volts where can I also put the pin I can put the pin to ground and if I put the pin to ground I am reading almost zero and so again it's not perfectly 0 at 0 1 now I am pretty much reading zero so let's go back and look at our math okay so would it help if I used a bigger pin maybe it would okay so let's look at this and let's think about it you get a little more paper here so what we just saw is is that 10 23 0 to 1023 this comes from the number 2 to the 10th and CC'ing computers you always kind of end up in these things that are 2 to the something 0 to 1023 is 10 hunt 1,024 numbers which is 2 to the 10th we know that we have voltages and then we have read values read us what we read so if we have a voltage of zero our read value is zero if we have voltages of 5 volts which is the maximum we're going to read 10 23 and so really we need to do a calculation we can't just print out what we're reading we have to do a calculation so what we want to do is we want to say voltage and we'll call it voltage 2 which is what we actually want that is equal to read Val that's what we're reading times what well 0 is 0 5 is 10 23 you can see that the slope is what 10 23 over 5 I did that wrong it is 5 over 10 23 right it's y2 minus y1 over x2 minus x1 and so v2 I hate it when I'm rotting and you're not seeing okay so V 2 is 5 divided by 10 23 times read Val okay so let's see if this works if read val is zero we put in zero and zero times five over ten twenty three is zero that is correct let's say our read value is ten 23 10 23 divided by 10 23 makes 1 times 5 is 5 V 2 is 5 so we see this works that if we do this calculation instead of reporting this strange seemingly meaningless digital number that we're getting off of our analog read we're converting it to a real and useful number so let's come back over here and remember I've got this thing hooked up right to be reading from that I've hooked it back up where I've got to keep my windows straight I've hooked it back up where I am reading right here I'm reading that V 2 point and then I will come back over here so you can see me program alright so what do we need to do differently on this program well we need a new variable and it's going to be int and we're gonna call it read Val okay and we don't have to give it here because we'll read it later we'll just clear it and then it'll be happy so I don't want to read into voltage because it's not voltage I want to read into read Val and then I want to take read Val and I want to turn it into voltage what is that nonsense what happened here okay here we go okay so I declare read Val and then down here analog read is not going to go into V 2 because analog read is this strange read Val so I'm going to come here and I'm gonna say put that number into read Val now what I've got to do is I've got to calculate V 2 what is V 2 go back and look and we said that it was 5 and I'll explain this later but you got to put a 5 / 10 23 point okay you got to put your decimals in and then times the x is the little asterisk and then times what times read Val so you take you read and read Val and then you multiply it by five and you divide by 10 23 and then you get a real voltage and actual voltage and then you print that voltage out okay let me explain just a little bit why do I have to put 5 divided by 10 23 with points in Arduino if you just write the number 5 without a decimal it interprets it as an integer if you write 10 23 with just as 10 23 it evaluates it as an integer and if you say one integer divided by another integer what kind of answer is it going to give you it's going to give you an integer and 5/10 23 in integer math is what zero this is one of the most common problems that I see when your program doesn't work it just be sitting turning zero zero zero zero well why would it be doing that it would be doing that because it would be doing that because you are doing integer math when you put a point at the end it makes it a floating-point number and then 5 divided by 10 23 is going to be a nice floating-point number of 4 point 8 9 e to the minus 3 does that make sense ok let's give this a try and let's download it now oh it is unhappy what is wrong here Oh were you guys yelling at me what did I forget to do here I said V 2 is 5 divided by 10 23 times read Val what does it say down in the error it is saying that it was expecting a colon before serial I look here what did I not put I did not put my semicolon okay let's try it again okay we download it okay and the voltage is coming out to be zero mmm wonder what's going on here all right wonder what's going on here let's think about it what did we declare v2 to be but did we declare v2 to be we declared it to be an int and if we have something like point nine six seven it's going to round off to what 0 so this v2 instead of making that an int it should have been a what a float okay because now you can see that 5/10 23 is going to be a floating-point number so we need to make v2 what a float because you wouldn't just have 0 1 2 3 4 5 volts you would have the end between voltages so we need to make that a float okay I hope when I did this you guys caught on but I kind of do things sometimes to put mistakes in just just to see if you're paying attention all right so let's download this the suspense bill look at that boom point 3 to point 3 to point 3 to point 3 2 okay so we are actually reading a voltage here and this would be a real voltage okay so what have we learned today we've learned how to turn on our serial monitor okay that is good well why is this so small hold on okay I got that in a 3 I've got this here I've got this grounded I've got this set to 5 volts we've got this I am NOT getting a good reading let's see what I mean okay I'm sorry I've got this hooked up on okay let me try again this is hard to do work like this in real time okay now it is working guys I had the wire in their wrong and that's why we were not reading the right voltage okay so now we are printing out a real-world voltage of 1.18 volts and so what that is saying is if we come over here if we come over where am i if we come over here you can see that I'm looking at this voltage across r2 because I connected a three into that connection between the two resistors which is measuring this voltage right here across r2 and what did we say we expected that voltage to be we expected that voltage to be 1.16 and right now we are reading 1.15 boom we have done an amazing thing here so let's think about and review what we have learned we have learned how to do an analogue read when we do an analogue read we do not read actual voltage we real read a scaled number between 0 and 10 23 if you read 10 23 you actually have 5 volts if you read zero you actually have zero volts so what you have to do is you have to take your read value and you have to multiply by 5 you have to divide by 10 23 and then you get a real and actual voltage and so we've learned how to do an analog read we've learned how to convert that analog read into an actual voltage we have learned how to do a pin mode for an input pin we have learned that you do your analog read through the a 0 through a 5 pins we have learned how to turn our serial monitor and we have learned how to print things from our serial monitor and I find it just absolutely amazing that we are actually getting very good readings and very good voltages using this analog read okay guys this has been a fun lesson we've learned something really new and important would love to hear your comments down below have you been able to get this thing working are you guys actually following along and building these things as I'm doing them you know I hope I hope so this is your your assignment for tomorrow what's your assignment for tomorrow is is to pick out two different resistors in the e Lego kit like find two different resistors like maybe there's a 10k and a 2k or a 10k and a 1k and do go through this whole exercise where you do you use two different resistors go through the circuit analysis predict what that voltage is going to be and then come in and measure it all right and just show that you can kind of do this start to finish on your own okay Palmer quarter from top tech boy comm I would really appreciate your comments down below I would really appreciate you subscribing to the channel hey I've been getting a lot of emails from people of if subscribe to the channel and say hey I'm not getting the notification there by the subscribe button you need to click the bell and then you'll get notifications okay poem a quarter from top tech boy comm I will talk to you guys later that is good coffee
Info
Channel: Paul McWhorter
Views: 206,158
Rating: undefined out of 5
Keywords: Arduino, Tutorial, Analog, Read, STEM
Id: 5TitZmA66bI
Channel Id: undefined
Length: 28min 22sec (1702 seconds)
Published: Tue Jul 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.