Raspberry Pi Pico W LESSON 5: Reading Analog Voltages Using a Potentiometer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul McCarter with toptechboy.com and we're here today with episode number five in our incredible new tutorial Series where you're unleashing the power of your pico W what I'm going to need you to do is for 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 Sun founder Sun founder is actually sponsoring this most excellent series of video lessons and in this class we will be using the Kepler kit for Raspberry Pi Pico W and the cool thing is the kit includes the Pico W and so most of you probably already have your gear but if you don't have your gear yet look down in the description there is a link over to Amazon you can hop on over there and get yourself a kit and believe me your life in my life are going to be a whole lot easier if we are working on identical Hardware but enough of the a shameful self-promotion let's jump in and talk about what I am going to teach you today and what I'm going to teach you is how to read analog voltages using your pico W now why would you want to read analog voltages well first of all a lot of the sensors in the kit are based on Resistance and voltages and so to read those sensors you need to be able to read analog voltages or maybe you're making a project and you want to have a user input from something like this little knob which is a potentiometer that you'll find in your kit and then as you turn the knob you will get different voltages coming out of this and therefore by reading the voltage you can read the knob position and you can add user input to your project something like maybe you would want to control the volume with the knob or maybe you would want to control the brightness of an LED or the color of an LED based on these little potentiometers and so one of the really important things to to be able to do in any project almost is read analog voltages and that's what I'm going to teach you how to do today now if you took my Raspberry Pi class you know that to read analog voltages from the Raspberry Pi it's quite difficult you have to use an external analog to digital converter you have to install libraries it's quite complicated the really good news here is the good news is is that on the Pico W it's very easy to read analog voltages you don't have to install any libraries and you don't have to go in and use any external componentry and so it really makes it very nice so let's jump in and let me show you how to do this what we're going to need to do is we're going to need to start by kind of understanding how these little potentiometers work this little uh this little knob you'll find it you'll find it in your sun founder kit and let's jump in and let's talk about how that little potentiometer works and let's see here we will see if we can get up very good and then I need to get out of your way and here we are okay and so what you'll notice first of all is is that the potentiometer has three legs there's the top leg and then there is the bottom leg let me move this over here a little bit more conveniently uh okay let's try that again there's the top leg there's the bottom leg now between the top leg and the bottom leg there is a resistor but the potentiometer has that magic Third Leg out here and that is the middle leg now what that middle leg is it acts as a variable resistor and you could kind of think of it as a wiper here okay so from the outside pins that always has the same resistance that always has the same resistance and we'll call that RT for R total and then there is a resistance R1 between the center leg and the top leg and then there is an R2 between the center leg and the bottom leg now as you turn the knob R1 and R2 change but what is always true R1 plus R2 is equal to RT the total resistance okay now I hope that makes sense now what you could think is as you turn the knob you're moving this little wiper blade up and down okay let's say that you moved it all the way to the left and the wiper blade is up here well R1 goes away it goes to zero so R2 is going to be what the r total or what if you moved the wiper blade all the way down then R2 is going to go away and you're left with R1 and R1 is going to be equal to R RT and in between those R1 and R2 change such that their total is always equal to the total resistance okay so now let me uh let's see if I can draw a few of these things a little better here okay so now let's imagine that we set the knob all the way to the left okay now what is the voltage that we're going to connect to this top pin it's 3.3 volts why that's the voltage output of the Pico W that's the voltage that the Pico W operates at so that is going to be set to 3.3 volts and then this bottom leg is going to be grounded and so that's going to be 0 volts or it's going to be ground okay and then what are we going to do we are going to read from the center pin and we're going to read that on one of the gpio pins on the Pico W okay so that Center leg the top leg is going to be connected to 3.3 volts the bottom leg is going to be connected to ground and the Magical Center leg is going to be connected to one of the gpio pins all right now let's imagine we moved the wiper all the way to the left and therefore it's up here what are we going to read at the gpio pin we're going to read 3.3 volts because it's going to be all the way up here what if we moved it all the way to the right and it comes down here then we would be reading 0 volts well what happens in between these two positions well it's going to vary between 0 and 3.3 volts and so as we turn the knob we should be reading voltage is all the way to the left 0 all the way to the right 3.3 3 volts does that make sense okay and then we can take that voltage value and we can do decide to do things like change the color of an LED change the volume of an audio output change the brightness of an LED we could do all types of changes based on that but in order to do that we have to first be able to what we have to be able to read it and so let's see if we can jump in here let's see if we can jump in and let me do a little Windows management here and what we're going to see if we can do is we're going to see if we can figure out how to hook that thing up well the first thing we're going to have to do is figure out which gpio pin to use to read that Center leg off of the potentiometer now remember when we were applying voltages we could use any of these light green we could use any of these light green uh gpio pins but if we're going to do an analog read we have to use one of the special gpio pins and that's one of the gpio pins that's designed to do analog read and in order to do that you need to look over here and this is like analog to digital converter 0 analog to digital converter 1 and analog to digital converter 2. so there's three different pins that we can use to read analog signals and so we could use uh pin physical pin 31 physical pin 32 or physical pin 34 and those correspond to gpio pins 26 27 and 28 well for the sake of setting up our circuit we're going to go ahead and we are going to use gpio pin 28 which is physical pin 34. and so with that let me kind of show you the schematic that we are going to be hooking up here and you should kind of recognize it as I've sort of already described it so we said that the top leg or the outside leg of the the outside leg of the potentiometer the the this would be like the bottom leg needs to be connected to ground now you could come over here and try to pull a lot pull a wire from here all the way over and have it land at one of the ground pins but in general when we're making a project I always like to create a ground Rail and so let's come back and see if we can find a convenient ground pin and you can see that to the right of the USB connector if we come down three pins that is a ground so we come back over here and we can see we go over one pin two pin three pins that is a ground and I'm going to bring that down to this second to the bottom row and then that whole row is going to become what it's going to become a ground rail now when I want ground for this outside leg of the potentiometer I just connect it down to the ground reel now similarly we're going to need 3.3 volts right we're going to need that 3.3 volts and so we're going to need to set up again we could go straight across right we could go straight across and pick up that 3.3 volts but I would rather create a 3.3 volt Rail and so if we come back to our pin out you can see that I have a 3.3 volt out on physical pin 36 which is two pins below the ground okay and so if we come back over here we will see that this is my ground setting up a ground reel this pin is 3.3 volts we'll bring it down to the bottom row and now that bottom row is a 3.3 volt Rail and so I've got the bottom leg of the potentiometer hooked to ground I have the top leg of the potentiometer or the right leg connected to the 3.3 volts now what do I need to do I need to read from I need to read from the center pin of the potentiometer and that is going to come over to pin 28 gpio pin 28 and we'll look at the pin out again and you can see that gpio pin 20 out 28 it's like you Skip One you skip 35 and then the next one will be gpio pin 28 and so we come over here we skip one and then or we come over here to the 3.3 volts we Skip One and then we are sitting here at gpio Pin 28 it comes over and it connects to the center pin of the potentiometer now physically in the real world we can come over here and you can see that I have that hooked up now as we're getting a little bit better and getting into these lessons I am not going to make you watch me hook this up but you can see that this I have hooked up which is exactly what we had right here so this is the schematic and this is the circuit in the real world and so that is ready to go so you need to get your circuit hooked up and then what we need to do is we need to learn how to write the code where we can read the voltage off of that center pin now what do we expect we expect that if we're all the way to the left right remember that picture we did when we're all the way to the left we should be reading what zero volts because we're going to be pointing at the ground okay if we move all the way to the right what do we expect we expect to be reading the 3.3 volts what if we are halfway between the two well you would be expecting something like about 1.6 about you know something like 1.65 half of 3.3 volts and so now we've got the circuit hooked up now what it comes down to is just writing the code and so let me come over here and see if I can get a convenient code view for you and so what we'll need to do is go ahead and call up Bonnie okay so I've got thoni here and now what I'm going to do is I'm going to show you how to write the code where we can read from that center pin on pin 28. well the first thing we're going to need to do we're going to need to import our gpio library and this time we need to import machine now last time we would in some of the earlier lessons we would just be importing pin from machine but this time we need to import the entire machine Library so we're going to import machine now we're also going to probably need to do a delay in there so I'm going to go ahead and say from time in port sleep okay now in the past we would directly be specifying our gpio pins but really it's a better habit if we start assigning variables to those pin those pins and so I'm just going to call the variable pot pin okay in the pot pin it's connected to which gpio pin it's connected to gpio pin 28 okay and you can see that gpio pin 28 is right there okay does that make sense so we are moving right along now I have to create an object I have to create that potentiometer object that I'm going to read from and so that I'm going to call my pot for my potentiometer and that's equal to machine that's the library that we just imported and then dot what the method is ADC for analog to digital converter because we're going to want to do an analog to digital conversion and then what do we connect to we connect to pin 28 right wrong wrong we're not going to use constants we're going to use what the variable because we already set pot pin to 28 so when we put pot pin here it's going to know that it's 28. what's the advantage of doing it this way the advantage is now if I ever want to use one of the other pot pins you know one of the other analog inputs I can just change it here and I don't have to be going through and searching through the code to figure out all the places that I need to change that number so that is a little bit of a pro tip there now do we want to make one reading or do we want to constantly read we want to constantly read so we read inside of a loop and we learned how to do that earlier we will create a while loop while true when is true true true is always true so when I do this don't forget your colon and now I'm tabbed over here anything that is tabbed over any code that is tabbed over is going to be looped through so what do I want to do I want to read I want to read from the center pin of the potentiometer which is connected up to the pot pin over here how do how do I do that well I'm going to say the pot Val that's what I'm going to read I'm going to read pot Bell you can call your variable whatever you want but it's the value coming off the potentiometer I'm calling it pot Val and then that is going to be my pot and my pot is the object that I created up here and so it's going to know that my pot is on 28 because my pot was connected to pot pin and pot pin was 28. okay so I need to do a pot Val is my pot okay and then I'm going to read okay I'm going to read and the way I do a read I say dot read underscore u16 and we'll talk a little bit later about what exactly u16 means but for right now we will just do the read and then what I'm going to do is I'm going to print potbell okay and now I'm going to put a little bit of a delay in so let's sleep by 0.5 okay wow could it really be could it really be that easy okay so now let's go ahead and set this all the way to the right okay we're going to set this all the way to the right and so when I run this now what voltage what voltage would I expect to see the voltage that I would expect to see is I would expect to see 3.3 volts so I need everyone to hold your breath and it is running the good news it is running but the bad news is the bad news is is that it is reading 65 535 which is what it is not 3.3 volts and so where did that crazy 65 535 come from well let's just see even if we're getting anything reasonable like if I come straight up the good news is that number is changing and if I set this straight up I'm now reading 33 000. so kind of the good news is it went from 65 to 33 so it's halfway and that's sort of what we expected that value to be half of what that was and then if I keep coming all the way over I don't quite go to zero I go to 432 but 432 is really close to zero if you're on a scale of 65 000. okay so what is going on here what is going on well what it is reading is it's not reading on a scale a voltage it's not reading on a scale to volt a voltage it is reading on a scale of bits and so it is going from zero bits or almost zero bits up to 65 500 bits something like that so where does that 65 000 come from well let's go back and look at this read what this is saying is we are reading a 16 bit number okay we are reading a 16-bit number now if I just had one bit how many numbers could I read I'd read two zero and one if I had two bits it would be four and then it would be 8 16 32 64 128 256 and so each additional bit gets larger and larger and larger and so let's come back over here and let's just see it's a 16-bit number well if I take the number 2 and I raise it to 16 for 16 bits what is 2 to the 16th well it's 65 536 which is almost precisely what we were reading here so let's come back over here and let's switch it all the way over and what are we reading for full scale 65 535. well now we were kind of expecting 65 536 right but remember we start at zero and so we end up one less than that so it kind of it kind of makes sense where we ended up here and so this is then what becomes the challenge we're going from almost zero to full scale of 65 000 uh what was it sixty five thousand five hundred and thirty five now what we need to do is we're happy because it's working right and it's scaling right but we have to turn that that binary number into a number that would be kind of useful to us okay something that we think in like voltage well how would we do that okay well first of all let's notice that it's 65 535 when it's all the way to the right that would be what 3.3 volts and then if we go all the way to the left left it is reading let's say let's say about 4 30 okay let's say about 4 30. and so we're going to come back over here and we're going to do some math okay now what I need you to do is I do need you to actually learn this math and if you have taken my classes before you have certainly gone through the math but what I need you to do is I need you to actually really learn this really learn how to do this let me get my sketch pad back up here [Music] that uh get that back connected always a question of my sketch pad is going to pop up there [Music] [Music] [Music] okay I think we've got it now I really do want to get this where it works a little bit better at some point okay so let's go ahead and let's clear this and how do we convert say 430 between a scale between 430 to 65 000 how do we convert that how do we do that conversion where we take 432 and make it zero and the 65 536 and make it 3.3 volts well we got to kind of think about this well what are we reading we are reading 430 like that and that 430 when I read 430 off the potentiometer that really corresponds to a voltage of what that is a voltage of zero right now when we read then 65 000 536 that corresponds to a voltage of what of 3.3 volts and so this would be one point that we have and then this would be a second point we'll call this point one and we will call this point two okay now let's kind of think of it on a graph so what are we reading we're reading pot vowel okay and pot Val goes from like ten thousand twenty thousand thirty thousand forty thousand fifty thousand and then sixty thousand okay those are the values that we're reading that is our x axis those are what we are reading we are reading like from here to here okay now what do we want to turn that into we want to turn that into one volt 2 volt 3 volt and then that would be about 3.3 well what is our first point our first point is this and so what would that be at 430 on the x-axis we would want the y-axis to be zero so we would have that point let me change my colors here so we would have this point here okay then what is the second point at 65 536 so this would be 60 this would be 65 536 we would want to be up here at about 3.3 volts so you can see I'm just kind of estimating it there so if I read 430 I want an output of 0. if I read 65 536 I want an output of 3.3 volts and then I want it to scale linearly between those two so what would I have I would have a line all right now if I want to in this this is uh this is voltage over here okay uh voltage that is voltage okay now if I want to take any pot vowel if I want to take any pot Val and turn it into any voltage I need the what I need the equation of the line now what I see you guys doing because you've taken my classes before somewhere along the way ninety percent of you have punted on the mat and the way most of you guys would do this would be oh well I'm going to take 65 536 divided by 430 times 3 point you try to just do a ratio and ratios don't work if you don't have a y-intercept of zero so you've kind of developed this little Knack of trying to do ratios and you apply that when it's not really appropriate and therefore you go through your life guesstimating I want to show you how to do it correctly okay and how do we do that by determining the equation of a line if we want to determine the equation of a line we need what two points and the first thing that we do is we calculate the slope and so let's come in here and do that how do we calculate the slope of this line well the slope is equal to Y 2 minus y1 Y2 minus y1 over X2 minus X1 okay and what would that be well Y2 is 3.3 volts so it's going to be 3.3 minus y1 which is 0 use your parentheses and then divided by use your parentheses X1 which is 65 5 36 minus X1 which is 430 like that so now what is the slope n is going to be simply 3.3 divided by 65 and then 5 minus 4 is 1 6 5 1 and then 3 minus 3 is 0. and then six minus 0 is 6. so the slope is going to be 3.3 divided by 65 106. so this is my slope now once you have the slope how do you get the equation of the line Y minus y1 is equal to M onto x minus X1 don't forget to use your parentheses there so this is going to be y minus what is y1 y1 is 0. and that is going to be slope which is 3.3 divided by 65 1 0 6. like that and then times what x minus and what is X1 X1 is 430. like that okay y minus 0 is 3.3 divided by 6 5 106 x minus 430. so now Y is going to be equal to because the 0 goes away 3.3 divided by 65 106 times x minus 430 times 3.3 let me make the times a little better there over 65106 all right and so now let me go ahead and change X and Y for the variables we're using which are pot Val and voltage therefore if I want the actual voltage the actual voltage is going to be equal to 3.3 divided by 65106 times x minus four hundred and four hundred and thirty I'm sorry 430 times so this was 430 times 3.3 divided by 65106 like that okay so now what do we have voltage is equal to 3.3 over 65106 times what is our x value it is pot Val minus 430 times 3.3 divided by 65106 okay so now what I can do for any pot Val for any pot valve that I read I can convert it very nicely to voltage okay guys we're going to be using this math all the way through this class and your old way of just trying to do ratios is not going to work I have shown you how to do the math when you have two points you can calculate the slope of the line and now without any funny business or guessing we can actually do this correctly so I need you to write this equation down and your numbers might be slightly different maybe your number was not 430 or 65 556 it might have been slightly different so you need to use your numbers but now what we need to do is we need to go in and we need to put that equation into our program over here so we are going to come back over here okay and then uh let's see [Music] what have we measured we have measured pot Val okay we have measured pot Bell but do I want pot Val no I want what I want voltage okay and so what I will do is I will come in now and what we're going to do is we're going to calculate the voltage and the voltage is going to be equal to the slope which is what 3.3 divided by 65106 like that okay and then times what times the pot vowel all right and then we subtracted what 430 times 3.3 divided by 6510 [Music] 6 430 and you couldn't see that could you [Music] let me switch over here okay now you can see it so what what we have is voltage is 3.3 divided by 65 106 times pot Val minus 430 times 3.3 divided by 65 65 106 and we went through the math now instead of printing pot Val I want to print what voltage like that okay so now what we have done is we've taken this arbitrary crazy business which was between 430 and 6556 and if we've done all this right we should now be getting what an actual voltage number that should go from 0 to 3.3 volts okay what kind of craziness is this but man what I really hope is I really hope that you can see that math is your print okay I hope what you can see is math as your friend and there is no way you could have figured this out just trying to wing it or act like a crazy man what I want you to see is that math that they taught you in high school it's actually useful math is your friend math is your friend now after all of that let's see how many mistakes I made in here I calculated voltage pot Val I should have that so I'm going to need you to hold your breath and ah oh well I'm actually getting numbers okay I'm getting numbers and the numbers are kind of looking good so let's come back over here to this view okay and where are we were all the way to the left when we're all the way to the left we would expect to be reading what zero but we're reading one one thousandth of evolved which is like pretty darn close to zero now what do we expect this to go to when I go all the way over I go to a voltage of 3.2999999 giddy up okay we have taken an arbitrary scale and we've mapped it onto something that is really meaningful which is voltage so we're going from 0 to 3.3 volts within the within the uncertainty of the measurement here now what would we expect if we point it straight up we would expect it to be somewhere around 1.65 5 volts and there it is 1.65 volts when it is pointing straight up as we come to the left we get lower voltages and when we come to the right we get higher voltages okay guys wow now a lot of you probably hate math and a lot of you have probably just developed ways of winging it but what I really need you to do is I need you to come in here and understand what I did because you see it took what two minutes to work through this problem and all the problems are going to be the same it's just you're going to have two points what you're reading and what you want that to be and then a second Point what you're reading and what you want that to be then you have two points and then the math is always going to be the same to create the the uh to create the equation of the line and don't punt on this you need to go in and you need to become comfortable with what I've shown here and then as you are comfortable with what I've shown here in future lessons you're going to be able to do the homeworks okay in future lessons you are going to be able to do the homeworks and so what I am going to need you to do is I am going to give you a homework assignment for next week and I need you to work it on your own the circuit is going to be the same as the circuit that I just showed you but when you're all the way to the left I want you to read the number 100 and when you're all the way to the right I want you to read the number zero so you're going to go all the way to the left it should be 100 all the way to the right it should be zero and when it's straight up the number should be reading something like 50. okay same circuit same potentiometer it's just a different set of output values why am I giving you this assignment so you can see that you can go in and you can really do the math okay and then as you do really do the math and you get the homework assignment to work then you'll begin to see that math is your friend now when you do the homework assignment I want you to post it to YouTube even if it is as simple as just filming your screen and then filming your circuit with your uh with your phone upload it to YouTube and your description leave a link back to this video and then down below leave a link that goes over to your homework solution then I want you to look at other people's homework solution and I want you to comment on their homework solutions why we want to start developing a little bit of a sense of community I want you guys to start to get to know each other and then as we start getting to harder and more advanced homework assignments you guys can kind of talk about it on the videos that you're posting does that make sense I really hope it does man guys I hope that you will learn the math if there's anything that I hope that you get out of this class is that you'll learn to do the math you'll learn that math is your friend and then the projects will start getting really really exciting because you're not just out there guessing and checking you're out there actually doing the real math okay guys I hope you're having as much fun taking these lessons as I am making them and then if you enjoyed the video be sure to give us a thumbs up if you haven't already subscribed to the channel when you subscribe make sure that you ring that Bell so that you get notification of future lessons and most importantly share this video series with other people show share this playlist with other people because the world needs more people doing engineering and fewer people sitting around watching silly cat videos Paul McCarter with toptechboy.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 16,986
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: ODWwErH_iGA
Channel Id: undefined
Length: 40min 29sec (2429 seconds)
Published: Tue Feb 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.