Arduino Tutorial 12: Understanding Potentiometers

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 12 and our new and exciting and improved Arduino tutorial series today is a very important lesson and so I'm going to need you to pour yourself a nice big mug of iced coffee you hear that ice in there iced coffee delicious strong black coffee poured over ice no sugar needed I'm also gonna need you to get out your most excellent a lego super starter kit that Uno r3 project kit this is the kit that we are using for this entire series of tutorials and if you don't have one yet you can pick one up down in the link in the description 35 bucks will keep you happy for a long time okay so get your kit get your coffee and let's get ready to rumble no we're not gonna we're not gonna really rumble but we are going to learn about a very very important component in your Arduino kit in something that can be a part of a lot of your projects and that is the potentiometer the potentiometer and so if you are in your kit you can find it and then we'll talk about how it works I need to probably get out of your way and then we're going to come over and look at the overhead view and we're going to talk a little bit about a potentiometer a potentiometer this is really going to go back to a lot of the stuff that we learned in Lesson number nine associated with Ohm's law and circuit analysis and a very important component in a circuit oftentimes is a potentiometer and the potentiometer can be thought of as a variable resistor it's a resistor where you can change the resistance so you know that in lesson nine we used a 330 ohm resistor we used a hundred ohm resistor or what if you wanted a 2398 ohm resistor well you could use a potentiometer and then you could turn the little knob and then you could get the resistance that you want this is the one that comes in your this is the one that comes in your a Lego kit it looks like this okay it's got three legs two on this side and then one on that side you can see that it's kind of like - if I can get its fun right here where you can see it it's like - on the outside and then one in the middle three legs one thing all potentiometers have three legs this one has a nice little knob that you can turn and then also the nod comes out and you can deal with it just like this it has little pins so that you can plug it in tool into your breadboard okay and I'm gonna put the little knob back in because that allows you to turn it let me tell you something about the potentiometer that's really important make sure that you don't force it like a lot of times you're doing a reading and you turn it all the way to the left and you're expecting to read zero volt volts and maybe you're reading point one so then you think I'll just turn it a little harder these are very easy to break so just turn them to the end of their travel or turn them the other way but don't don't force them once you get to the end point trying to squeeze a little bit different value out of it and so this is the potentiometer that comes with the e Lego kit this is one that I had from earlier kits potentiometers come large small there's a lot of different shapes and form factors they come in but kind of the common thing is they always have these three legs and you've got to think of it as you've got the two outside legs and then you have the middle leg and so what I want to do is I want to talk to you about how these potentiometers work and then we're going to use one in a circuit and then we're gonna do a little bit of analysis with it so let me kind of try to get set up make sure that you're seeing what I am writing and so let's say that this is the way that you should think of a potentiometer I'll just kind of draw it as a black box then you've got the two outside leads like this okay and what you should think is you should think of these as I'm gonna start override it and draw that good I'm sorry okay so we're gonna think of the potentiometer as a black box and then you've got this outside lead you've got this outside lead okay so if we go back and look at our potentiometer again this would be like the top lead and this would be the bottom lead and the way to think of it is there's two resistors in here okay and we're gonna call this r1 and we're going to call this r2 all right and then from here to here you have our T for our total well what would knowing how series resistors add what would our T equal our T is equal to r1 plus r2 now you have a third lead that is connected inside the potentiometer between r1 and r2 so the thing that you have to know is RT is r1 plus r2 now you've got a knob on here you've got this knob that you can turn when you turn the knob all the way to the left and so let me see if I can illustrate that here if you turn the knob all the way to the left then r2 is equal to zero well if r2 is equal to zero what does r1 have to be equal to r1 has to be equal to RT the total now understand the total RT doesn't change different potentiometers have different artis like if we look at the little one here from the let me see if I can get you where you can see this if you look at the little one from the Oh doesn't want to focus on I'm gonna try focusing one more time where you can see that okay you see it tries to focus and then it focuses on the ground it's labeled 10k maybe you saw that so the total resistance from the two outside leads is 10k on other potentiometers it might be 1k or 5k or a hundred K but this one is 10k 10k ohms I should be more specific all right so if I turn the potentiometer all the way to the left r2 is going to go to zero so r1 has got to go to RT all right what if I turn the knob right the little knob on top what if I turn it all the way to the right well if I turn it all the way to the right the opposite happens r2 is equal to RT and r1 is equal to zero now in this case for this particular potentiometer right that's 10k ohms and that's 10k ohms all right what happens then if you turn the potentiometer in the middle when that case r1 is going to be equal to about five K ohms n r2 is equal to about ten about five K ohms so five plus five is one what if you have it almost all the way to the left well in that case maybe r2 is equal to 1 K and R 1 is equal to 9 K homes and homes all right so you see the way this works r1 and r2 always add to RT but as you turn the knob you can change how much of that RT ends up is r1 and how much of the RT ends up is r2 so r1 and r2 are constantly changing as you spend the knob but always but always the total is going to be the 10k or whatever it is rated at so let's do a little circuit analysis and let's just think about this let's put 5 volt it's across it five volts so we'll call vs is equal to five volts and then we're gonna come and I'm gonna try as best I can to draw this and then I'm gonna have an r1 I'm gonna have an r2 and then I'm gonna come back and then this is that third lead okay so let's go back and see okay do you see those two leads those are the up maybe I should turn it this way okay you see those two leads those are the top and bottom and then here you see the middle one that's the one that comes out here we're going to call this r1 we're gonna call this r2 and let's call this V out this is the voltage out this is the voltage that we measure from the center from the the center connector well let's think here let's think what could we calculate we could calculate the current through this potentiometer when it is connected to five volts remember the form of Ohm's law V is equal to I times R that's the one you start with but here we're really wanting to calculate I so we're we're really wanting to calculate I so we're gonna divide both sides by r and then what we find is I is equal to V over R alright in this case V is 5 volts the sources 5 volts and then the resistance is this rated resistance of the entire thing which doesn't change which we said is 10k which we will write here is 10,000 all right so now I can calculate the current using the calculator I have five divided by 10,000 and that is going to be 0.5 0.5 milliamps 0.5 milliamps we could also write that is 0.5 e to the minus 3 amps 0.5 milliamps or 0.5 e to the minus 3 amps you guys tell me if you understand scientific notation if I'm leaving if I'm losing you here maybe I need to make a video on doing scientific notation but what did we just figure out we just figured out the current in this device is always going to be I is equal to 0.5 milliamps alright now this is the other thing that we could do we could calculate V out now V out is going to be equal to I times what well that would be the voltage across r2 so it would be I times r2 okay so V out is going to be equal to point five milliamps times r2 okay if r2 is zero it's going to be an output of zero if r2 is turn if the knob is turned all the way the other way then r2 is going to be 10k and then at 10k it's going to be five volts if it's in the middle then it's going to be 5k 5k times 0.5 milliamps that's going to be 2.5 volts and so what you can see is the output voltage of this potentiometer is going to go from zero to five volts depending on whether you're turned all the way to the left or all the way to the right and so what these are used for it is their really clever little things that can be used is almost like controlling the brightness of an LED or setting the volume their knobs that allow you to put kind of like input into the circuit so let's build a quick little circuit here let's build this circuit that we just saw and so we will bring our most excellent Arduino in here and then we will program up some code and so you guys are probably wondering why I always have the arduino upside down that would bother me if I were you watching it I don't know if you notice those types of things but the reason I do it is it allows me to build the circuit a little bit simpler and that is because you know as I'm using these digital pins they're closer to the circuit board so I kind of just got in the habit of doing that some time ago and so we have this ready to go all right so we are going to need to plug this in now the problem is if you look at those leads if I plugged it in like this that's kind of neat but it goes from the top row to the bottom row I can't connect to it because it's covering up the holes if I connect it like this the problem is that I have two of the pins in a common column and they are going to short out so I need to plug it across that Center trench and when I plug it across the center trench everything is going to work now I'll try to show you here you see those two oh I wish I had a better focus in camera here let's see if we can focus that absolutely no focus at all try it one more time okay good focus okay so do you see that those two outside leads are in different columns so they're not connected together and then the center lead is in its own column so we can connect to it so now I'm going to go ahead and press it down if you have the e Lego one you've got to kind of press hard but it will go in okay so let's look at this that first outside one needs to go to five volts we're not going to be switching the five volts and so it can go just into the five volt output on the Arduino so I connect in the same column is that first outside lead and then I come over and I connect to the five viii the 5v you got to look kind of carefully it's easy to get in the wrong one the 5v output okay and now the other one and let me make sure you can see the other outside lead is going to go to G and D alright let's look you see top of our one goes to five volts the second outside goes to the ground or the - okay I've got that now if you look at this what are we going to do with the centre lead we're going to measure the voltage out of that so we're going to take that remember from the last lesson how do we measure voltage with an analog pin so we're going to connect that centre tap that we want to measure the voltage and I'm going to put it on a two okay so now what I can do is I can measure the voltage at a two and then I can turn the potentiometer to the left and the right I must get this neater this is just too messy okay so now I can turn the potentiometer to the left or to the right and I can measure the voltage that is coming off of that centre tap and based on our analysis if I'm always one way all the way one way I should ensure zero volts and if I'm all the way the other way I should measure five volts and in between I should smoothly go between zero and five volts we also have to remember how to do an analogue read so I'm going to come over here and switch to a different appropriate screen which this should be and let's write the code here real quick to operate this potentiometer alright I am going to be set up my variables well what's the first variable I need I am using a pen and what did we say we are using pen a - okay maybe I could switch here yeah that's pretty good you can see them both there okay I'm going to be using pen a - so that's going to be an int and I'm gonna call it my my volt pen and my volt pen is a - that's a little bit long of a variable but that's okay I'll use it animate my volt pen is a - now I'm gonna be reading a value from the potentiometer that's gonna be an int so that's gonna be read dowel and that is going to I'm not I'm gonna read and read Val so I'm going to declare it but I'm not going to put a value in it up there to begin with and remember when you read an analog volt analog value on the Arduino you don't read a voltage right you don't read a number between 0 and 5 you read a number between 0 and 10 23 and we have to convert that number to a real voltage and so besides read Val then we need another int and what do we call this we called this R - what would the voltage across r2 be let's call it V - that's pretty good capital V - and then I'm gonna say I'm not going to put any value into that because we're gonna calculate that so now I have my volt 10 it's a 2 I had Reva read Val and I have V 2 now probably as we read these we're gonna want to what we're gonna want to print them out and so I need to turn on the what I need to turn on the serial monitor so this is serial dot begin and then I need to give it a bod rate 9600 okay and now the serial port is ready to go so now what do I want to do I want to read from that potentiometer how do I do a read well I'm going to put it I've got to put the value somewhere where am I going to put it in my variable read Val and I'm going to say that is going to be equal to analog read get the happy little orange color what is the parameter where do I read from a - that is my bolt pin okay I'm going to read it there read Val it's going to come back from 0 to 1023 so I have to convert that to v2 and if you don't know how to do this go back to a couple of lessons ago and I show you how to do this but this is v2 is going to be equal to what if 0 is 0 and 10 23 is 5 I've got a scale it and so this would be 5 divided by 10 23 and the two oh my goodness v2 the voltages are in between values why didn't you guys yell at me here that the voltage I might have 2.1 2.2 2.3 this needs to be afloat I need the in-between values not an int and then this needs to be floating point math and to make that happen I need to say 5 point divided by 10 23 point now I know it sounds like I'm belaboring this point but if you do not put the decimals here it's going to say 5 is an integer divided by 10 23 is an integer 5 divided by 10 23 it's going to round it to the next integer which is going to be 0 and you're going to be sitting there trying to think your Arduino burned out to where you were just doing integer math where you should have done floating point math v2 is a float so these numbers need to be a float and then I multiply that by I multiplied that by Reed Val now let's see if Reed valve is 10 23 then that's 10 23 divided by 10 23 times 5 thats 5 so if there's 10 23 it's going to put in a 5 if Reed Val is zero it's going to put in a zero and so that's what we want a Ravel of zero is zero votes a reeve a love 10 23 is 5 volts and so we do a quick quick back of the envelope look at this to make sure that we have it in the right direction and that is going to work and then what do I want to do I want to print it out serial dot print L in what am I going to print v2 all right in it with a colon and then let's slow it down a little bit let's do a delay let's say 1/2 second 500 milliseconds is that good no no don't use numbers down here don't use numbers use variables you need to use variables M so this would be delay T and then I need to come up here and declare that so I'm going to say int is going to be delay T and then that is going to be equal to 250 milliseconds which is about 1/4 of a second so what they should do is this should sit and print the voltage that we are getting off of that center pin on that potentiometer and if things are coded right and if our circuit is set up right what should we see we should see if we're turned all the way one way we should see 0 and if we're turned all the way the other way we should see 10 23 so we are ready to download this program ok boom it is happy and now I need to go to a better screen where you can see the serial monitor I pop the serial monitor up I hope that I get something man it's not printing what did we do wrong ok I want somebody to tell me what we did wrong I'm saying serial print line v2 I started my serial monitor but it is not printing okay what did we forget this baud rate has to match the baud rate that the serial monitor is set to can you see this the serial monitor is set to a 115200 and that's set to 9600 so I'll just pop this over to 9600 and boom look at that that is printing out a nice value ok so I've got it turned all the way to the right I wish I could get out of your way a little bit I got it print turned all the way to the right and it's kind of like full volume I am really hoping that this will change yeah and you see let's see I really need I'm trying to think if I have a view that will show my serial monitor and I don't okay but as I'm turning this to the left you see how it's going down all the way to zero and as I start turning it back I get the knob to about half way and I'm about at two and a half volts so do you see that as I turn this as I turn this I am getting what I want d'arnot I am just so obsessive-compulsive that I really really really wish that I could show you me turning the knob in those things changing at the same time but I'll just show you like here you can see that what I'm doing when I'm all the way to the left I make the knob go all the way to the left and then what I have is I have zero volts and then I will come back over here and I will turn the knob all the way to the right and then what I have is five volts and then I come back over here and I'm gonna put it about in the middle man we kind of lost something there that's right I'm gonna put it about in the middle and then I'm gonna be very close to two and a half volts boom okay do you see what we're trying to do on this series of lessons we're trying to put a little more math in it where you're not just watching me do something on the potentiometer but you're you're kind of doing some circuit analysis and using Ohm's law in using simple circuit analysis we predicted that as we turn this I my production quality today is not what we would want it to be okay so what we predicted based on this circuit analysis was it could should go from smoothly from zero to five volts as we spun from all the way to the left to all the way to the right and that is exactly what happened and this is again a very very useful feature this is a very useful component and so I could use this to set values on let's say an LED so that's what we'll probably start getting into we'll start using this and using this knob to then control other things because we've got it where we got the knob connected the potentiometer connected we get different values as we turn the potentiometer and now we can use the potentiometer to control other things and so I want you guys to just play around with your potentiometer I don't mmm I'm not able to think of a real good homework assignment on this one will gets more homework assignments as we do the next lesson but we're gonna be playing around with this potentiometer for a while now I know some of you guys are getting antsy because it's kind of like I'm playing with real real simple components like LEDs and potentiometers why are we doing so much of our work with LEDs and potentiometers well number one LEDs are cheap and potentiometers are cheap so if you make a mistake you don't blow out a $50 GPS or something like that you're learning on components that are inexpensive and the second reason is almost no one has ever gotten hurt with an LED or a potentiometer so we're doing things that are simple that don't cost a lot if you burn them out and they don't they won't hurt you and so we're trying to kind of start nice and simple like that all right I hope you guys enjoyed this lesson I would love to hear a comment down below from you really like getting feedback from you guys really like interacting with you in the comments think about subscribing to the channel always appreciate a thumbs up if you're not getting notifications for my videos look at subscribe and make sure you click the bell and then you will get notifications okay this is polemic order from top tech boy comm and I will talk to you guys later that is really good
Info
Channel: Paul McWhorter
Views: 159,404
Rating: undefined out of 5
Keywords: STEM, Arduino, Tutorial, Potentiometer
Id: PUte1cmJ44A
Channel Id: undefined
Length: 28min 43sec (1723 seconds)
Published: Tue Jul 09 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.