LESSON 10: Reading Analog Voltages with Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys I'm Paul McWhorter and I'm here with lesson number 10 on the Arduino microcontroller and what we're going to be doing today is we're going to be looking at how to read analog voltages off of your circuit using the Arduino and if you think about it with all of our exercises so far we've been working with these pins down here those are the pins 1 through 13 these are pins that we write voltages to we remember that the the pins that don't have a squiggly line we can do digital rights to that would be a digital right to that pin high or to that pin low but if we use the pins that have the squiggly line by them we can actually write an analog voltage and we write an analog voltage by writing a value between 0 and 255 if we write the value 0 to one of those squiggly line pins we write a 0 volts if we write a 255 we write 5 volts so we can write voltages between 0 to 255 on pins 3 5 6 9 10 and 11 along that along that bottom row the other pins were able to just write only able to write digital values to higher low ok but so far all we've been doing with our circuit well we've been talking to the user over the serial port printing messages to him over the serial port and then getting input from him over the serial port so we can interact with the user and then we can make things happen to the circuit we can apply voltages to the circuit we can apply digital voltages or we can apply analog voltages so those are the things that we've learned so far now what we're going to do today is we're not just going to be talking to the circuit we're going to be listening back from the circuit we're going to be going out in making reads we're going to be reading voltages off the circuit and to see how to do this we want to start with like the simplest simplest example possible so basically what we have here is a potentiometer if you're not familiar with how a potentiometer works go back and work video go back and watch video number 9 and video number 9 we go through some of the circuit diagrams for how to how to work with a with a potentiometer and how to make a simple voltage divider being this is just a quick schematic of the voltage divider that we're trying to achieve the voltage in it's basically just a fixed voltage it's five volts the way in the circuit that we're getting the the voltage in is we're coming off and we're getting 5 volts off of the Arduino from the 5 volt pen coming down and just establishing a 5 volt rail on our on our breadboard and then we bring that 5 volts and we go to the top leg of the potentiometer so the 5 volts comes then goes to the pop-top leg of the potentiometer where we're going to read is and we're going to read the voltage off the center leg of the potentiometer so you can see this is the center pin on the potentiometer we're going to bring that up to the analog pin 0 which is right here so by coming from the center pin of the potentiometer up to a 0 it's going to be that a 0 pin that we use to read a voltage off of the potentiometer and then down on the lower leg of the potentiometer we bring that over to our ground rail and that ground rail is our top row and so we bring that third leg up to the top row and then that top row we connect to ground on the Arduino and so basically what we create is a very simple voltage divider that's shown here and what the resistance this RP is the resistance of the potentiometer let's say for this it's a thousand ohms well basically that thousand ohms is split between r1 and r2 depending on where you have the knob set if you have the knob set all the way to the left all tidge appears as as r1 there is no r2 and therefore the voltage out is going to be zero it's going to be like you're looking at the ground if you turn it all the way to the right for this particular particular potentiometer it puts all the resistances R - there is no r1 + V out seized the end so it would see 5 volts so basically with this circuit our fee out is the center leg of the potentiometer just like you see here the center leg of the potentiometer and by hooking this circuit up like this what you should see happen is if you read a voltage on pin a zero you should be able to vary that voltage between zero all the way up to five volts by turning this little knob okay if I turn it to the left I should be getting zero volts you know if I turn it all the way to the right I should be getting five volts okay now one of the things to understand when we do an analogue read it's kind of like remember when we did the analog right that things were scaled and so if we wanted to write zero volts we would write zero we would write a value of zero if we wanted to write five volts we would write a number of 255 to that pen so a right value of 255 corresponding to a voltage of 5 volts and so we had to scale things well when we do analog readings we have to scale things as well although the scale factor is a little bit different the analog numbers the numbers that we will read off of that analog pin will be between 0 and 10 23 okay if we connect and try to read 5 volts the read value that we will get will be 10 23 if we read zero volts the revalue that we will get will Z will be 0 so basically for voltages between 0 & 5 we will get read values between 0 and 10 23 and so you've got a scale that number that you read that's between 0 to 1023 you've got a map it on the number 0 to 5 and we'll go through the math on that in a minute but for right now let's just see if we can make of a measurement on this voltage divider on this potentiometer and let's see if we can get values that are what we're expecting let's see if we can make this thing work so in order to do that what we need to do is we need to call up a nice new Arduino and integrated development window and we will start writing some code here well if we're going to talk to pin Z a zero if we're going to work with pin a zero we probably should assign a variable to it so that we can go through and write our program based on using that variable and so what we can do is we can set it up as an int and then what am I going to call it well it's hooked to my potentiometer so I'm going to call it my pot pen all right and that's going to be equal to a zero you try to display a zero all right now the thing to understand is normally anytime you do an integer declaration you would just put a number in here but it's unique that it will recognize as integers those pin names and we are connected to an a zero this we are ok and then what we are doing is we're assigning a SS assigning a pot pin to a zero we try to be good and put our we try to be good and put our our comments in here now we're going to we're going to be reading from that 10 so we're going to need a variable to put that number into so we need to declare a variable it's going to be an integer and we are going to declare it we're going to call that our read value so the value that we read from 10 8 0 we're going to call that read value we're declaring the variable here but we're not assigning a value to it because we'll get the value from the pin we'll go out and we'll read it from the pin so we're not assigning it here but we are declaring our read value variable ok now let's do our pin modes let's come in our void setup which pen are we going to be doing we are going to be working with our pot pin okay that's pin a 0 but what is it going to be always up until now our pins have been output because we've been outputting to the pins this pin we're going to read from so this is going to be an input so we're going to say 10 mode and then it is the pin pot pot pen and it is going to be an input I think this is the first time we played an input variable also we are going to be working over the serial port interacting with the user so we need to turn the serial port on so we'll do our serial dot B again and I'm going to go to a 9600 baud this time right just make sure that whatever baud you use it matches the bod that said in the serial in the serial port and the serial monitor okay look I've declared my variables I've done my pin mode and I've started my serial port so what can we do down here well let's see if we can make a reading off of that pin so the way to make an analog reading is you do analog read notice the bumpy font and the R is uppercase so analog read with our uppercase you see it turned orange it says I recognize that command it's happy and then what pin are we going to read we're going to read pot n okay now that does the read that the value that we get we've got to put somewhere well where are we going to put it we're going to put it in our variable read value so read value is going to be equal to analog read of pot pin so read values our variable that's our bucket where do we get our number to put in that bucket well we get it from over here at this analog pin so we do an analog read a pot pin so I go out to a zero pot pin I read a value and then I stick it in my bucket in the bucket is labeled read value so the Arduino has the value at that point but let's print it out so the user can see it will do a serial dot print line println and we're going to print the value read value that we just read in boom now this can go so fast we want to slow it down so we can see the numbers coming up so we're going to put a delay just so that we can sort of see the numbers going by 250 milliseconds is a quarter second and to me that's that's a good value to use okay so we declare our pin we create a variable called read value we do our pin modes we turn our serial port on and look at I have not done comments I better go back and put my comments on and geek and it's just good practice right let's let's get in this habit whether it just seems silly sometimes we're not declare pot pen and input and then start your serial port okay read 10 read pot pin and put value in read value okay so that's the bucket we're going to put it in we're going to come here and print results to serial monitor and then delay 250 milliseconds all right so what they should do is when we download it it should just go out and start making measurements on this potentiometer and what value it will read should depend on where the little knob is set at so let's see if this is going to work let's see if I did my code could look at that lot of green down here it looks like it's happy everybody's happy so let's turn our Syria tur on and let's see what we get it's reading zero and the reason it's reading zero is I'm turned all the way to the left and when I'm turned all the way to the left all of this resistance R P shows up is r1 this is down here and you're just seeing zero volts what's reading zero volts and it's reporting that as zero what happens if I start turning this to the right if I start turning it to the right you see I start seeing bigger and bigger and bigger numbers as I slowly turn it to the right I'm getting more and more and more resistance coming from up here down to here so this is like not pointing here it's pointing further and further and further towards the top 526 I'm about halfway there because remember 0 to 5 volts will read 0 to 1023 and so a value of 526 527 corresponds to about 2 and a half volts so I'm about halfway to turning it all the way to the right and so I'll continue to turn it here you can scroll in growing growing and then I turned it all the way to the right and I get it all the way up to 10 23 when it's reading 10 23 that corresponds to a voltage of 5 volts you know everything scales from 0 to 1023 represents numbers from 0 to 5 volts ok so now what you can see here is is that I have a little input device and the number that I get on my serial monitor depends on where I set this potentiometer like I have a little knob like that could be a volume control now I could set the brightness of the LED based on where I put this or I could set other things based on where I put that so now I don't have to just get user from the input over the serial monitor I could set things based on where I put this potentiometer so now all the sudden it's like a little product where I could start turning that knob and getting the LED brighter or dimmer I could turn the knob and that could affect how fast the LED blinks I could set the parameters for the operation of the circuit from this from a potentiometer like this so even though this is real simple this is extremely powerful extremely powerful because it lets a device works sort of autonomously that on the circuit you can start getting your input directly from them directly from the circuit ok let's just take a quick look though at what you might want to do is you might want to turn this read value like I think in terms of 0 to 1023 so I can look at and I can just see what the voltage is but if you were going to give it to a user you might want to give them a number from 0 to 5 volts and so let's see how that math works all right well again we go back and we use what we learned with linear equations let's think about what you read you read a value from 0 to 1023 and so we're going to make this axis the horizontal axis your independent variable that's what you read it's your read value and then that corresponds to a real-world voltage between 0 and 5 volts and so we make that the vertical axis voltage between 0 and 5 volts corresponds to read value of 0 to 1023 let's see if we can find two points on here point number one we know that a read value of zero corresponds to a voltage of zero so we have one point the point zero zero alright what's the second point we know the second point we know up here is that a read value of 10 23 corresponds to a world voltage of 5 so we have a second point of 10235 so we have two points for this line if we have two points we can calculate a slope slope is y2 minus y1 over x2 minus x1 that's going to be equal to let's have this as as the second point in this is the first point makes it a little easier I like to do my linear equations to try to avoid negative numbers so I have 5 minus 0 over 10 23 minus 0 so that's Y 2 minus y1 y2 minus y1 over x2 minus x1 and that comes out to be 5 over 10 23 now we know the slope the point-slope form of a line is y minus y1 is equal to M over m times X minus x1 your point x1 y1 you could pick this point where you picked that point and you'll get the same answer but it's a lot easier if you pick the easier point let's pick this one all right now let's put in the values Y minus y1 is M onto X minus X 1 Y minus 0 right ok that's 0 is equal to 5 over 10 23 which was the slope times X minus x1 all right times X minus next one so we have 5 minus 0 is equal to 5 over 10 23 the slope on 2 X - 0 and that's because we chose this point of zero zero to make it easier the other one would have eventually given us the same answer so Y is equal to 5 over 10 23 times X that is the equation of this line now the only thing I'm going to do now I should switch it to our y-axis Y was voltage and X was read value so we change Y for voltage and X we make read value so what we see is the real-world voltage is equal to 5 over 10 23 times the read value okay and these are the read values that we're getting here and so we can use this equation that we got from using that equation of a line that you had to learn place in seventh grade and wondered when you were ever going to use it well you're using it now to allow you to really create an equation that will take this read value this read number that we're getting that's between 0 to 1023 and turn it into a real-world voltage a real voltage that's between 0 and 5 so let's go ahead and see if we can do that so we've got our revalue let's create another variable called integer and it's our voltage and this is our real voltage we're going to calculate this so we are not going to assign a value to it we're just going to declare it ok declare our volt voltage variable okay now notice how didn't it just out a habit but I want you to think about this are we interested in just the voltage 1 2 3 4 5 there could we have in between voltages well very much we could have in between voltages and we could have a whole lot of in between voltages we could have 0.1 volts point to point 3 point 4 we could have very very very small increments in voltage and so really we want the in between numbers so we should not declare this an int we did should declare it a float and again when we declare something as float that means a floating point number if we go back to math class that would be equivalent to a real number it's all in between numbers and we call them floats in programming language so we have a float variable now we're going to calculate what is our real-world voltage will the voltage the voltage is going to be equal to remember it's going to be 5 over 10 23 but now this is very very very important and I mean over and over I see students forget to do this if we are declaring something to be a float and if we are using floating numbers we have to write our numbers as floating numbers and sit so instead of saying it's 5 over 10 23 that's 5 over 10 23 when our dueƱo sees 5 it sees it as an int and when it sees 10 23 it sees it as an int and so if you do the int math integer math of 5 divided by 10 23 it's going to come back with a 0 because it's going to round it to 0 or it's going to truncate it to 0 and so you don't want it because you want a real floating-point number well we've said voltages floating points so we need to make these floating-point numbers and we do that by putting a decimal five point tells Arduino that you want the floating point number 5 not the integer 5 and similarly here point so when you are doing floating point math on the Arduino make sure that you put your decimal in or you're going to get some surprising results that's going to take a long time to figure it out so what was it voltage is equal to 5 over 10 23 times Reed value and so we have 5 as a float divided by 10 23 as a float and then we're going to go times Reed value okay and then we're going to do this is what is it doing it's calculating the real-world voltage okay now let's download this and see if it's still happy looks like it's going to be happy see if we can get our serial monitor back up call that thing up all right now look that does not look let's see let's try downloading it again ah I see what I did I was still printing read Becky so even though I calculated voltage I was still uh I was still printing read value so that was a little bit of a shocker to me so what we've got to do is we don't want to print out the read value anymore we want to plot out serial dot LS serial dot print line and then we want to print voltage voltage okay write out the real-world voltage okay this should work this time so this time we should be getting this value soon somebody should have told me semicolon at the end of almost all lines okay so now we should be getting real-world voltage isn't this out okay that looks happy let's call this up okay so remember how that was a read value of about five hundred and twenty something well that corresponds to about two and a half volts so we're just sort of almost halfway up so if I turn it all the way let me get the circuit back here where you can see the circuit I get this all in here so if I turn this all the way to the left now you can see that I have zero volts I can turn it just a little bit and look at that I'm getting about me see if I can get it okay that's about as close as I can get it but I'm getting about a tenth of a volt there so you see why we needed floats because we need the numbers between the integers and so now I can turn it up and I can get 0.5 volts let's like let's see I can get about I'm trying to get about 175 it's kind of hard to get it exactly about 7-5 I can take it up to one volt take it on up keep turning to the right keep turning turning keep turning I get five volts yes you see what I'm doing is I've got this potentiometer okay I did the math and it was based on this voltage divider that we talked about okay let's see if we can get it all showing here and I'm reading now an analog voltage off of that voltage divider and I'm reading it in real volts from zero to five volts so I'm reading an Arduino number that's between zero and ten twenty three and I'm converting that to a real-world voltage between zero and five volts in CC it's like a little volt meter that we would that we built here and the voltage corresponds to whatever voltage that this that this pin that this thing analog analog pin reads well let's just do an experiment okay what if I just unplug this from the potentiometer okay and I put it on my ground what should it read if I put on the ground it should read zero volts I didn't put it on the ground if I put it here on the ground right there it should read zero volts and it reads zero volts because ground is zero volts what if I put it on the next one down at the positive rail and when I put it in there it reads five volts so you see all the sudden here with this little pin I have a little voltmeter that will read anything between zero and five volts okay so I'm going to come back in here and put it on my potentiometer again but really you would not use this as a you know to build a voltage supply really really what you're doing is you're trying to read basically read and understand where the potentiometer is set to and what you're really going to use this for usually is for an input on your circuit so like let's say that I could write a little program that if I have it turned to the left the LED blinks really slow and as I turn it to the right the LED starts blinking faster and faster and so basically this is a way that you can start controlling things with knobs there's all types of different reasons that you would use that you would do these analog reads just to recap remember when you're doing an analog read you have to you one of these six pins a zero to a five you have to use one of those if you're going to do an analogue read these pins down here we'll do digital writes the ones with the squiggly lines will do analog writes and the ones that do not have the squiggly lines will just do digital rights and so squiggly line on the on this bottom set of pins will do analog writes or digital writes the ones without the squiggly lines will do only digital rights okay so this is a powerful tool a new tool we have now we can talk to the user over the serial monitor we can read values from the serial monitor we can apply voltages digital voltage depends we can apply analog voltages to pins and now we can read analog voltages so our toolset is getting pretty pretty sophisticated and we're going to be able to do some pretty impressive things with these simple tools fairly quickly okay again I am Palmer quarter this has been lesson number ten and hope you will tune in to lesson number eleven very shortly
Info
Channel: Paul McWhorter
Views: 202,315
Rating: 4.9284863 out of 5
Keywords: Arduino, STEM, High School Science, Engineering
Id: QsYcYknKbB0
Channel Id: undefined
Length: 27min 46sec (1666 seconds)
Published: Mon Jun 30 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.