Arduino Uno R4 WiFi LESSON 12: Read Analog Voltages on the Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
guys this is Paul mcarter with toptechboy dcom and we're here today with episode number 12 in our incredible newe tutorial Series where you are learning to think like an engineer what I will need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at sunfounder sunfounder is actually sponsoring this most excellent series of video lessons and in this class we will be using the sunfounder elite Explorer kit now most of you guys probably already have your gear but if you don't look down in the description there is a link over to Amazon you can hop on over there and pick your kid up and believe me your life and my life are going to be a whole whole lot easier if we are working on identical Hardware but enough of this Shameless self-promotion let's talk about what I am going to teach you today and what I'm going to teach you is I'm going to teach you how to read analog voltages using the Arduino now what have we learned so far we've learned how to do digital wrs where we can set a pen to zero or 5 volts we've learned how to do analog wres where we can go from 0 to 5 volts simulating the in between numbers using pwm we've learned about binary we've done binary counters we've learned about delays digital rights analog rights pin modes variables all those things we've learned but we've always been sending data to the pin but what we're going to do today is we're going to read data from the pin now what I should say right off the bat if you're going to read voltages using your Arduino you can read voltages between 0o and 5 volts if you try to read higher voltages very likely you're going to damage your Arduino so right up front don't try to read voltages that are greater than 5 volts okay so what are we going to do today we are going to need to create analog voltages how are we going to do that with a potentiometer what is a potentiometer a potentiometer is a variable resistor okay so far we've used fixed resistors like our 1,000 Ohm resistor every day it's 1,000 ohms it's a fixed value but the potentiometer lets us create a variable resistor and with that variable resistor we are going to create a voltage divider where we can adjust the voltage from 0 volts up to 5 volts and all the voltages in between and then we're going to use the Arduino to read that voltage sound good okay okay so let's start by building the circuit after we build the circuit we'll switch over to the sketch pad and I'll talk to you in a little bit more in depth kind of get under the hood and see how these potentiometers work then we'll switch over and we'll start doing the coating okay hopefully that sounds good so get your kit and then I need you to look up here in the corner and you will find this little bag of goodies okay I need you to get your little goodie bag out and what you're going to be looking for is that little Blue Knob okay that little Blue Knob is your potentiometer so we're going to go in there we're going to get that out you see it looks like that we're going to seal our bag back up we're going to put it away see keep our kit neat okay now we are going to come in and we are going to plug and you're going to need to now see what I'm doing overhead here I'll get out of your way all right so now what we are going to do is first of all you're going to look and you're going to see that your potentiometer has how many pins it has how many legs coming out it has three two of them are in line and one of them is not in line two of them are in line and one of them is not in line okay well what I am going to do is I'm going to put the two that are in line up at the top to the right okay and then the one that is not in line that Center one is going to be pointed back to the left and you see it's going to fill in three different columns make sure that each one of those legs is in a different column so what do we have we have the top leg here we have the middle leg and we have the bottom leg all all right now I'm going to hook it up the top leg needs to go to 5 Vol so I'm going to come over here and that top leg is there I need to connect that to what to 5 volts and I've got a convenient 5vt pin right there so I've Got 5 volts connected to the top leg now the center leg that center pin is what I am going to be reading from so since I'm going to be reading from it what I am going to do is I am going to connect that to the pin now as we were doing digital and analog outputs we were always using these pins 2 through 13 we were always outputting today we're going to use an analog read and if we are going to do an analog read we need to connect to one of these analog in pin and those are the PIN a0 to A5 those are suitable for reading analog voltages now I want you to think if I have analog in pins a0 to A5 how many analog in pens do I have six because if you count start counting at zero and count up to five you've counted six different numbers so hopefully that makes sense so we can use any of these analog pin that we want I think I will use analog in pin 2 A2 now we just got to finish up our Circuit by coming to the third leg the bottom leg which is right there the bottom leg of that potentiometer the bottom leg of that potentiometer and we're going to bring that back over here to ground or gnd D okay so you see I've Got 5 volts I've got gnd D and I've got pin A2 so you can see all of those things hooked up very nicely I'll get those out of the way and that potentiometer is turned over to to the left so I think that thing is put uh hooked up I think that is going to be pretty good we are going to take just a second here and make sure that we try to get that a little bit straighter and we are going to come over and we are going to see if we can hook up to our sketch pad I'll need to plug the sketch pad in while we're waiting for it to warm up I will take a sip of coffee and now we'll come over here and we should see the sketch pad pop up here at any minute there it is okay now to talk about what the potentiometer is as I said before it is a variable resistor well let's think of the resistors that we've used before the resistors that we've used before have had two legs or two leads to them and then between those two leads we have a resistor like that and the resistor is always the same value it's always the same value now what's different about the potentiometer the potentiometer had how many legs three it has that mysterious Third Leg out here now how does it work that third leg is connected to a wiper and that wiper wipes across the resistor and so let's look at it this way let's say that we had the wiper turned all the way to the left we've got the knob turned all the way to the left in that case that third leg that wiper comes down here all the way to the very bottom of the resistor now let's remember how we hooked this up we had five volts up here and we had this at 0 volts or G and D zero volts are ground okay now let's think about this what would we see at this leg this leg comes down below the resistor and it's basally basically hooked up to 0 volts because there's no resistance from where it connects down to 0 volts and so what would it read it would read 0 volts let's also think about it like all the current is going through that resistor the entire resistor is above the connection Point therefore all of the voltage drop is above that connection drop uh that con connection point and so the five volts all drops above the connection point and therefore it would read 0 volts now on the other hand let's imagine that we turned the knob all the way to the right and that wiper when we're all the way to the right would come up here all the way above the resistor in this case how much resistance is between the connection and 5 volts 0 ohms there's no resistor up there we're above the resistor voltage drop is current times resistance above the resistor how much voltage drops zero because it's current times resistance resistance is zero and so the voltage drop is zero and what would we see in that case we would see 5 Vols in that case all the voltage drops beneath that connection point so if I'm all the way to the right I am going to see 5 volts if I am all the way to the left I am going to see zero volts well what if I'm halfway what if I'm halfway well in that case that wiper would come into the resistor here and half the resistor is going to be above the connection and half is going to be below the connection and so how much is going to drop between the 5 volts and the connection half 2.5 volts and then how much it's going to drop beneath it another half 2.5 volts so what would I read between here and ground I would read 2 .5 volts okay what if I had it almost all the way to the left almost all the way to the left if it was almost all the way to the left it would come and connect right here now most of the voltage is going to drop here that might be 4.9 volts would drop here how much would drop below the connection maybe 0.1 volts okay okay on the other hand how much it how about if I was almost all the way to the right almost all the way to the right would be here okay and only a little bit of that resistor is above my connection so only a little bit of that five volts is going to drop across it so maybe a tenth of a volt would drop across it therefore 5 minus a tenth I would be reading 4.9 volts Okay so now as I turn from all the way to the left to all the way to the right I'm going to read between zero and five volts in nice smooth increments does that makes sense I hope it does okay so now let's come over and let's see if we can start making those measurements now that we know how it works I should also say if for some reason your potentiometer is doing the opposite of mine that just means what I called top you called bottom and what I called bottom you called top and you would just switch the red and the black wires you would switch those outside ones because you're probably plugged in upside down from what I did here okay but I think we are ready now to come in and start coding this thing and so let me get my keyboard here let me get myself a little sip of coffee as always you want to make sure that you're connected the board so you'll come in here to tools and you'll go to port and you can see that my Arduino Uno R4 WiFi is connected to com 5 so I'll select that I always like to make sure I've got the right board selected so I come to board and I come to Arduino Uno R4 and I am on the Wi-Fi board which is I which is the board that I am on yours might not be on com 5 select whichever comp Port that it shows your Arduino showing up next to we've gone over that so hopefully you understand that by now so if we are going to if we are going to connect to that pin we need to set up a variable now what kind of variable it's going to be an INT and what am I going to call this well I'm connecting that analog pen to the potentiometer so I think I'll call it pot pin you can call yours whatever you want I think pot pen is a pretty good name and then I'm going to say integer pot pin was pin A2 wait you say A2 is not an integer yes it is in heximal heximal goes 1 2 3 4 5 6 7 8 nine but then it adds extra characters AB bcde e like that and then it switches back around to one Zer and then 1 a so AB bcde are part of hexadecimal so A2 is actually a heximal number and it is is an integer in hexadecimal so in fact A2 is uh an integer okay now what we'll do is we will put our semicolon there now we're going to be reading from that pin so we need a variable to put that reading into I think I'm going to call my variable I think I'm going to call it pot Val and that's going to be the value that I read from the pot pin since I read it I'm not going to assign it a number here because I'll read it from the pen later but I do need to declare it I do need to declare it as a variable if that makes sense now what we're going to need to do is our pin modes okay now do we want to do our pin mode over and over and over no we do it one time so we do it where in the void setup so I will do the pin mode and it is on what it is on pin a0 or A2 no no don't use constants what do we use we use our variable pot pin okay now it is a what this time for the first time in this class it is an input we are reading from a zero okay now once we read it we're going to want to print it so we need to what we need to turn our print uh our serial monitor on so I do that with serial DOB again and 9600 no what do I do I'm going to come up here and I'm going to set up a variable int and I'll just call it BR for B rate and I'm going to say that is 9600 like that and then I'm going to come here and I'm going to set my serial monitor to BR bod rate and then my friend the semicolon I've done the pin mode I've started the serial monitor now I am ready to make a measurement so I'm going to read the number into the variable pot Val how do I read it I do an analog read be careful of case uppercase R where am I going to read I am going to read from pot pin like that okay now I don't want to just go too fast I want to slow it down a little bit so I want to put a delay of let's say 100 no what do I need I need a variable weight t for weight time all right like that and now I'm going to come up here and declare weight time and I will say int weight T and I will set that to 100 milliseconds like that that looks pretty good and so we've got our pot pin our pot valve B VR in weight T we've done our pin mode we've turned on our serial monitor we have read the pot valve but what do I want to do I want to print the pot V so I'll say print pot Val do we like that no it's going to print across I want to each time come to the next line so I need to do a what print Ln like that okay let's just run this thing and see what happens okay so we're going to come here it is compiling ah what did I do oh this is not micropython you don't say print who is your friend Mr serial do print line like that okay so you see print line it didn't know what it was because it didn't have serial in front of it so we're going to come here we are going to run it no ugly little orange messages we'll take a sip of coffee it's uploading it's uploading and it's uploaded now I'm going to turn my serial monitor on up here and then boom look at that now what is this goofy number here what is this goofy number here that is the timestamp and I'm going to turn that off by clicking here now what am I reading I am reading 1023 I don't think that we have 1,23 volts I don't think we have 1,23 volts so what we have to do is Arduino thinks in computer land it thinks in terms of zeros and ones it thinks in terms of switches in binary and what is the number 1023 it is the number two right on and off zero and one is the number two raised to the 10 so it is a 10 bit number 2 raised to the 10 is 1,24 well why did this start at 1023 why did this stop at 10:23 because we started reading at zero we started counting at zero and if you have 1024 numbers and you start counting at zero you can count up to 1023 which is really 1,024 numbers make sense okay what you have to see is you have to see that it is giving you a binary number that is being displayed as a decimal number 1023 but it is 10 switches on on on on on on on on okay out to 10 2 to the 10 minus1 1023 okay because it does not think in people terms like voltage Z of 5 it thinks in terms of 10 bits from no bits up to full scale which is 1023 it's working but it's not very useful now what happens if I turn the knob to the right it start starts going down and when I'm about straight up and down halfway when I'm about straight up and down I've got about 570 which is about half of 1023 I keep going to the right I keep going to the right and it comes all the way down to zero so with the potentiometer I am reporting from 0 to 1023 very smoothly that's the good news what's the bad news it's not reporting voltage now the other thing that I don't like I don't like that when I'm at the left I'm starting with the biggest number and as I'm cranking it up the number is cranking down I always like the low number to be at the left and then as I turn it up the number gets bigger now we could code this differently but what is the easy way to do it I told you that it could be upside down so we're just going to trade the red and the black wire so the red wire is going to come over here to the bottom to the bottom leg and the black is going to come over here to the top leg and we're just going to switch those out now if I turn the knob all the way to the left it reads zero and now if I turn the knob all the way to the right it reads 1023 and it goes smoothly between those values now what do we need to do the Arduino is going to think the way the Arduino does so I need to convert that Arduino number to a person number so I want to convert from 0 to 1023 into a number from 0 to 5 how do we do that we do that with a line we do that with the equation of a line okay does that make sense so let's see if we can come down here and let's see if we can talk about that a little bit because we are going to be first of all you were taught lines in junior high and in high school and in college and you didn't think you'd ever need them so you didn't pay attention but I'm going to teach you what you already know but I'm going to show you you've got to use it now if you're going to think like an engineer you got to use the math to do the hard work for you and what you're going to see is is that the math is really the easy way to do it the math is really the easy way to do it and so what we're going to do is we're going to come back over here to the sketch pad and we're going to think about this okay now what do I read when I'm all the way to the left I read a value of zero and that's the pot valve from 0 to 1023 it reads Zer when I read a zero what voltage does that represent that represents a voltage of zero now if I turn it all the way to the right what do I read read I read 1,23 I want to turn that 1,23 into what five so if I read zero I want to turn that into zero if I read 1023 I want to turn that into five so now what do I have I have two points with two points I can create a line with two points I can create a line so let's come down here and let's see if I can drop a coordinate axis on you here let's just see if that will work got that that looked pretty good and then we'll come out here one two three four one two three four five like that okay now this is going to be 1 two 3 four five that's going to work okay now what is on my horizontal axis my horizontal axis is my horizontal axis is pot Val okay what that's what I read I read pot Val and that's going to go between 0er and 1023 so this would be like 100 20000 300 400 500 600 700 800 900 this would be a th000 and I'll put a few more in here this is 900 this is 800 this is 700 like this okay that's my horizontal axis that's my pot Val what is my vertical axis my vertical axis is my vertical axis is volts okay and so this would be Zer volts one Volt 2 volt 3vt 4 volt 5 volt 1 2 3 4 5 I have how many points I have one point two points so this is 01 this is0 2 okay let's go with 0.1 0.1 is 0 0 so that would be the point right here 0 0 what's the next Point 1023 so I come all the way over to here and then I go up to five and so this is the point 10235 all right now what do I between those two points I draw a line between those two points okay now this allows me to take any pot Val take any pot Val and turn it into any voltage so I can take any pot Val and I can turn it into any voltage so let's say that I had a pot Val of 400 you can see that that would be a voltage of about two so you want to turn pot Val into voltage well how do you do that you need to create the equation of a line how do you create the equation of the line you start by calculating the slope how do I calculate slope m is slope and that's equal to Y 2 - y1 over X2 - X1 well what is Y2 it is five minus y1 is zero over what is X2 it is 1023 okay minus X1 which is 0er okay what is that equal to that is equal to 5 over 1023 so what do I have I have my slope now what do I need I need the equation of the line well the equation of the line if you remember back to Junior High y - y1 is equal to M onto x - X1 okay what is y y is y - y1 y1 is 5 so y minus no I'm sorry that's Y 2 y1 is zero so y1 is 0 is equal to M which is 5 over 10 23 I like to use parentheses time x - X1 what is X1 X1 is all always also zero so when you have that 0 0 the math gets a lot easier and we end up with simply Y is equal to 5 / 10 203 * X now what I like to do is I like to go in and rewrite it with the values that I'm using for me the x value is pot Val and the Y value is volts and so I'm going to rewrite it with Y is going to become volts so volts is equal to five divided by 5 ided 10 23 times what x and x is what x is pot Val okay what did I read in my program I read pot Val what can I do now I can change pot Val into volts and it should be a real voltage from zero to 5 volts how does that sound so you need to look at this equation and we need to now go in and we need to program that thing okay we need to go in and we need to program that thing so keep that in front front of you and then we're going to come back over here to our code and what do we need now we need bolts okay but what is volts going to be is it going to be an integer 1 2 3 4 5 volts no you want all the in between value so volts we want to be a float because we want you know 3.26 5 volts maybe we want the in between value so volts is going to be a float and I'm going to calculate volts so I don't give it a value up here I'm going to calculate it so now how do I calculate volts I come down here I have pot Val and then I look at my equation volts is equal to open parentheses 5 / 1023 like that 5 ided 1023 I'm going to turn that back to zero okay 5 divided 1023 times what times pot Val just like that now did I remember my semicolon up there uh yes I did very good okay so now I should have volts so now what I want to do is I want to kind of print things a little bit nicer so I'm going to print a label for pot Val and that print for pot Val is going to be the string okay pot Val and then I'm going to put a colon in a SP space and I'm going to put my semicolon and then on that same line on that same line I'm going to print pot Val and then I I want to leave this on the same line all of this so I'm not not not going to do a print l in I don't want to go to the next line yet so it's going to say pot Val is the pot Val and now also on that line what on that same line I'm going to want to do a serial. print and I'm going want to print the label voltage like that and then I'm going to print serial.print this time I'll go to the next line Ln and I'm going to print bolts so do you see how I'm printing labels and values and labels and values so as I go across it's going to have the label pot Val and then the pot Val Reading and then it's going to have the label voltage and then it's going to have the voltage reading okay and then after I do that then I want to go to the next line so that should be a it should be a print print print and then a print Ln okay could it really be that easy let's see if we can run this thing and see what happens okay ah we have a problem ah what did it like oh did I do that again this is not VP this is not python this is not micropython you can't just say print you have to do serial.print hopefully you guys saw that but I know that's the mistake you make so I like to kind of do the mistake that you guys do all right since it's a popular one all right right so now we're running it we're not seeing any little ugly orange messages we are uploading we are uploading and boom it's done we'll come over here and click on this serial Monitor and we look at that okay what I don't like is I've got the label and I've got the value but then the next label is run into that Val uh into that uh that value is run into the next label so before on the label voltage I'm going to put a couple of spaces just because it doesn't so it doesn't run into that zero all right let's try this again okay that looks good so I've got a pot Val of zero and I've got a voltage of zero is that what we expect yeah zero pot Val should be 0er volts now we're going to turn this up what is that we're going to turn this up and what do we expect to see see we expect pot Val to go from 0 to 1023 and we expect voltage to go from 0 to 5 so we begin and what do we see pot Val going up voltage isn't moving pot Val going up maybe I got to turn it more bot Val bot Val all the way to 1023 and voltage doesn't move Houston we have a problem so what do we got to do we got to troubleshoot this okay did I read pot Val yes I did how do I know I'm reading it because I'm printing it and it is there did I calculate volts yes I did it is 5 / 1023 time pot Val that was the math we did that was right but then am I printing volts yes I'm printing volts why am I printing zero okay this is the reason that airplanes crash this is the reason that buildings explode this is the reason that Bridges collapse because you have not been mindful of your variable types and your number types okay so let's think of the number three okay and let's think of the number four if I say three divided by four what is the answer well it depends on what type of variables they are if it's the integer 3 divided by the integer 4 you would think 75 but that's not an integer so it would round down to zero so in integer math 3 / 4 is 0er well the way we wrote this the five doesn't have a decimal point so it is an inte integer so I have the integer 5 divided by the integer 1023 that's like a really small number rounded down goes to what zero you see I thought I was doing floating Point math but I was really doing integer math so what do we have to do if we want a floating Point answer we don't want to say five we want to say five point five by itself is an integer 5 point is a what it's a float divided by 1023 that's an integer but 1023 point that is a float now 5id 1023 is going to be a float times pot Val is going to give us a volts which is a float and so now we've done our variable types this should work so let's go all the way back to zero all the way back to zero 0 0 Let's download this thing again okay done compiling uploading looking good there we are and we've got 0 0 but now the real question is as I begin to turn this pot Val comes up and voltage comes up okay let's go to about straight up and down when I go to about straight up and down right about there I have a pot Val of 523 which is about half of 1023 and I've got a voltage of 2.5 which is half of voltage when so when I'm half a full scale pot Val I'm half a voltage and then as I turn up voltage continues to go up and I get all the way to five boom I have converted the number 0 to 1023 which makes no sense into the number 0 to 5 volts which makes perfect sense so now we are reading in real volts in real voltage okay guys I hope that math made sense if it didn't go back and look at it again for the rest of this class you're going to have to know how to do the math for a line you get two points you calculate the slope you plug the slope into y - y1 is equal to M on x - X1 and then you have the numbers that you have and you turn them into the numbers that you want what numbers did we have 0 to 1023 what did we want 0 to 5 what we have 0 to 1023 that's the X values what we want 0 to five that's the Y values so go back and watch that part because you're going to have to do that for the rest of the class okay we need a homework assignment so what your homework assignment is is to get your potentiometer also add your red LED don't use the blue LED it is only for special occasions connect your red LED and what I want you to do is to have a dimable LED that is controlled by the potentiometer when the potentiometer is all the way to the left I want the LED to be off as I begin to turn the potentiometer to the right I want the LED to begin very very dim and become brighter and brighter and brighter and right when I get all the way to the right I want the LED to be full brightness okay I've taught you what you need in order to be able to do this homework but you're going to have to do some what you're going to have to do some math you're going to take the numbers that you have and you're going to turn them into the numbers that you want and I'll give you a hint what you want is not 0 to 5 okay go back and watch the pwm in the analog right class if you don't remember that okay so that is going to be your homework when you do the homework make a short video of it in the video show me your code and the video can be 30 seconds or a minute or two minutes or something like that show me your code then show me your circuit and then show me your circuit working as you turn the knob I want to see your LED go smoothly from off up to full brightness okay that is your homework assignment guys I hope you're having as much fun taking these classes as I am making them as always I want to give a big thank you to you guys who are standing with me on patreon it is your support and your encouragement that keeps this great content coming you guys can also help me out by giving me a thumbs up it also helps if you leave a comment down below if you've not already subscribe to the channel when you do ring that Bell so you'll get notices When Future lessons drop and most importantly share this video with other people because the world needs more people thinking like an engineer and fewer people sitting around watching silly cat videos Paul mcarter with toptechboy do.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 4,182
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: Vp5u_8KAzFw
Channel Id: undefined
Length: 41min 9sec (2469 seconds)
Published: Thu Apr 11 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.