Paul McWhorter arduino lesson 30
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 amp 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 dueo 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.
Paul McWhorter arduino lesson 30 Video
Paul McWhorter arduino lesson 30 news
Posted on Friday August 19, 2016Books You Should Read: Basic Electronics Hackaday … Continue Reading »
Paul McWhorter arduino lesson 30 Social