# arduino voltage sensor

So what we want to know is what is the actual voltage being applied to one of the analog pins on our Arduino board now, in order to do this, we have to learn a couple things. The first thing you’re going to learn is how to use a conversion factor, it’s, actually extremely simple and extremely handy and we’re, just going to kind of talk through how to do some conversions. The other thing we’re going to do is learn about the float. Data type turns out also be extremely easy now Before we jump in. I want to note that this this sketch is very much like the last tutorial about using analog read, so we will run into a couple new things, but it’s going to be very. It should be very familiar to you but we’re going to go from start to finish. It’Ll, give you some repetition, and then it’ll also put put this new stuff in context all right. So without further ado, let’s go ahead and jump in for this tutorial. You’Re going to need an Arduino, you can use any Arduino, I’ve got the uno here or you could use a clone. I do recommend sticking with the Arduino brand. However, just so we’re on the same page, you will need a potentiometer so potentiometer, you know it’s like a volume knob, it turns left and right and it really doesn’t matter what resistance level you get I’m using a 10k potentiometer here, you’re also going to need, at Least three jumper wires and then finally, you’ll need a solderless breadboard.

So let’s go ahead and set up this circuit, all right, so here’s our schematic. So you can see. This is a really simple circuit. If you adjust your breadboard yard, we know like we have in the schematic and we’ll, be talking the same language as we go through this whole sketch. So you see the potentiometer, like the part that you turn is pointed away from the arduino and five volts. We have connected to the left side of the potentiometer ground is attached to the right pin of the potentiometer and then the center pin of the potentiometer. That is what goes to a 0, which is the first analog pin on your Arduino, so what’s happening here. Is that potentiometer is acting like a voltage divider, so on one end of the potentiometer you’ve got five volts and on the other end you have zero and when you adjust that knob, the center pin is going to change all the way up from 5 volts down To as low as 0 volts, so as you adjust that knob, you are adjusting the amount of voltage that’s going to be seen at that Center pin, which ends up being. You know what we have on our Arduino attached at a zero okay, so that’s, essentially how this is working so go ahead and open up your Arduino IDE and go to file examples. Basics read analog voltage alright, so the first thing that we come to the first block of code are the comments.

We’Ve got a multi line comment. It talks about how to set up the circuit and it talks about just roughly how the circuit works. It also lets us know that the code is in the public domain, so we know we can pretty much do whatever we want with this code, which is a good thing to know so, that’s it we don’t declare any variable, so there’s, no declaration or initialization of Variables, and so if that’s not there, it looks like we jump right into setup, so in setup all we do is we use the begin function from the serial library to initialize our serial communications and we send a baud rate of 9600 again, that is the standard Baud rate communication that you’ll be using and that’s pretty much it for setup, not too much to setup here so beyond setup. Now we get to the loop, so the loop again is going to run over and over and over again as opposed to setup, which only gets executed once so we’re in the loop it’s kind of the meat and potatoes of what’s going on so let’s. Take a look: what do we? What do we see that’s going on here? So the first thing we’ve got here – is a variable initialization and initialization. So we’ve got a variable. That’S named sensor value it’s an integer and we are initializing it to the output of the analog read function. So if you recall from the last tutorial analog, read, can be used on the analog pins on the Arduino, so those are a 0 through a 5 there’s 6 analog, pins and all we’re doing is passing analog.

Read the number of the pin that we’ve hooked up the center pin of our potentiometer, to which happens to be a zero, so the first pin is a zero. The last pin is a 5, so that’s how you reference it. You put an A and then the number of the pin. Now the output, if you’re call of analog read, is going to be an integer between zero and a thousand and 23. So zero is going to refer to zero volts in a thousand and twenty three is going to return refer to five volts, so that’s handy so basically generally handy. So, basically, when you turn your potentiometer, if you turn it all the way left it’s going to be a ground which would be zero and if you turn it all the way right, it’s going to be at 5 volts, which is going to be a thousand and Twenty three let’s say we’re, not interested in that generic number let’s say we want to actually know what the voltage value is. Well, we have to do a conversion on that number. So again, what this line of code is going to do is it’s going to give us the number between zero and one thousand and twenty three, but to get that actual voltage number. We have to go on to the next line of code, where we do a conversion, so let’s go ahead and talk about the conversion. So let’s talk about basic conversion.

Now we are going to do a little bit of math, but I promise you. This is the only math you’re going to run into in this entire course turns out to be extremely easy, so the first thing I like to do is kind of get in my head. What is it that I want as my output? What am I trying to convert to so, in this case we’re, given units all right so we’re, giving like these generic units? The number that we get from analog read is between zero and a thousand 23 like we’ve, like I’ve, already said four times all right, but what we want is volts. So what we need to figure out is how many volts are there per unit? Okay, so let’s go ahead and jump into this algebraic expression. Okay, so, on the right hand, side here, I’ve got my known. I know that if I’ve got zero resistance on the potentiometer so I’m, not dividing any of that voltage up. I’Ve just got five volts going straight to analog, pin a zero. I know that I’m going to get a thousand and twenty three units, so I know that five volts gives me a thousand 23 of units, but what I want to know is how many volts do. I get per X units, okay, so that’s what I’m trying to figure out. So how would we set up that equation? Well, if we want to know how many volts – and we want this to be on its own on the left – hand, side of the equation – all we have to do is multiply by X units.

So all I do is I put the X up here in the X up here. These are going to cancel out and we’re going to get something that looks like this. So now I’ve got number of volts on the Left X units times this conversion factor five volts over a thousand twenty three okay. So what this is going to do notice? That units will cancel out and we’re going to be left with volts. So this number right here well, what is this X? Well, this X is what we’re going to get as the output from analog read. Okay, so this is going to be a number from 0 to 1023 okay, so this number is going to get multiplied times. This conversion factor and we’ll end up with volts and then that voltage will get assigned to this value right here so that’s, basically how to do a conversion, so remember start out with will what is what is the unit that I want to end up with in This case it was voltage and then ask well, would it what are the knowns? What conversion factor can I use to cancel out the unit that I don’t want? Okay, all right so that’s it on conversion, let’s, go ahead and jump back, so let’s go ahead and drop down to this next line of code, where we set up this conversion factor all right. So again, we’ve got an initial or we’ve got a declaration and an initialization, so we’ve got float.

Whoa float hold on a second. What is float well float is just a datatype I’ll talk about that in a moment, but let’s just pretend that we know what float means. Okay, so float it’s a datatype, well what’s. The name of this variable it’s called voltage. Great name clearly says what we want voltage and we initialize it to now. This is where that equation from the conversion. We just talked about sensor value well, that’s, our X that’s. What we just assigned sensor value is the value that’s coming right out of analog, read and it’s, telling us where exactly our potentiometer is turned to and then when we multiply it by well, we multiply it by our conversion factor again: that’s five volts divided by one Thousand and twenty three units again so five volts per a thousand twenty three units. Okay, so voltage is going to be the actual voltage that gets applied at the pin. So before we move on let’s go ahead and talk about this float data type. So a float it’s a weird name, but really it’s it’s, really simple. So a float is simply a number that has a decimal point. So, like 3.141592, it has a decimal point. So any number with a decimal point, two point: zero, zero, two point: one zero! Those are examples of floating floats, they’re, also called floating point numbers so, like I think I like to think of a float. I think if the little you know, the decimal point is like a raft floating in the ocean.

You know it’s just kind of bobbing around. So you know it’s flow it’s, a decimal it’s floating in between the numbers somewhere that’s. How I remember it so it can be really big, like 3.4 times 10 to the 38th big, which is a deux Cillian. You know that’s, like 38 zeros. On the end of your numbers, that’s huge now it’s only precise to seven total digits and that’s, not seven digits to the right of the decimal point, that’s. Actually, seven digits, total now floating point math is actually really slow. So if you start trying to multiply and divide all that type of stuff with floating point numbers, you are going to slow things down, so I would avoid it at most cost. Now I mean sometimes it’s necessary in this case we’re using it, but definitely try to minimize it and floating or pardon me floating point options. They can actually return some pretty obscure results. Now there are some workarounds, usually that involves changing a float into an integer and then doing your math, but generally it’s not good to do comparisons with floats, because you just don’t know what you’re going to get so or between floats, for example. But you know that’s really we’re not too worried about that it’s, maybe a little more than you need to know about a float, but just so you again let’s just recap: real quick floating point number it’s a number with a decimal point. Well, what does that mean to you or us? Rather, it means that it’s a number that has a little more accuracy than an integer okay, so that’s it with float datatypes there’s, always more to read about if you want to check more out in the further reading.

So let’s go ahead and recap: the loop function. So the first thing we do. We declare a variable sensor value and we set it equal to the output of the analog. Read function, so we’re going to be getting raw data from analog read. Then we declare another variable called voltage and we set it equal to that raw data times that conversion factor that we set up so now voltage is going to end up as the actual voltage being applied at our pin and then what we want to do. We send it to the print line function from the serial library, and why do we do that? Well, we want to be able to see it. We can use our monitor, monitor on arduino, ide and check out what the actual voltage is and then the loop repeats itself. It goes, it looks, analog, read so we’re checking out what our potentiometer is set at again again, send that raw data to voltage, convert it and then print the output do that over and over and over again so let’s go ahead and verify the sketch and let’s Upload it and now let’s go tools serial monitor, okay, so right now, I’m at five volts I’m going to go ahead and adjust it to the left. Now you can see the voltage number changing. We’Ve got two digits of accuracy here, so you can see. We’Ve got a floating point number, as the output, and now there I’m at zero.

Okay, so somewhere in between, should be two and a half volts. Okay. So now you can see we’ve converted that raw data from analog read into the actual voltage number pretty cool there’s. A lot of uses for this, especially when it comes to testing what voltage you’re actually getting in off a sensor, so that’s one good reason to implement this type of conversion: okay, that’s it. I look forward to seeing you at the next tutorial and be sure be sure to do the try it on your own challenge, that’s, where you’re really going to learn. Something alright, see you next time have an awesome day.

[Tags]

# arduino voltage sensor Social

[flickr]
[pinterest]

Originally posted 2016-03-12 15:52:24.

(Visited 63 times, 1 visits today)