So you get some accurate results. Um, so ive got an arduino uno here and you can see over here. Ive got my bench: power supply and ive, set it for 4.00 volts dc output and im feeding that to this breadboard. Now you can see ive got a meter hooked up off by .001, maybe that bounces around, but you can see they both pretty much agree. Ive got four volts coming out and im feeding it into the arduino into one of the analog inputs. Analog input zero, so ive just got these two wires going to analog input, zero and ground, and you can see its being fed by usb for my computer. So we would expect that um, if i put were going to put together a very simple sketch, that you see the same sketch that you see in many places on the internet, about how to grab data from the analog input and convert the 0 to 1023 value. Back into a voltage, and it should match our 4 volts so were going to put together that simple, sketch run it and see how accurate the results are on the computer compared to what our meters are saying over here. So lets roll over the computer and see what we get okay. So here we are on the computer and i put together a very, very simple, sketch, its probably very similar to a sketch. You see all over the place when people are showing you how to read from an analog pin on the arduino and ive, basically set up the analog pin as a0 and the void setup.

I just do serial.begin 9600 and im setting that analog pin a0 as an input very simple. Now in the loop. What im doing is. I am doing an analog read from that pin and im setting that equal to a variable called log, val and thats an integer because, as you probably know, the analog read converts the actual voltage value coming into the analog pin into an integer and that integer is Between 0 and 1023, and then what i do is i calculate i go back and from that value figure out what the actual voltage is and we were. We were inputting about 4 volts, so this float value of voltage will be the 0 to 1023 log valve times 5 volts over 1023, and this is the equation that you see very often uh associated with the arduino, where you assume, okay, five volts coming in will Give a value out of the arduino of 1023 so were going to take. The integer from the analog read multiply it times 5 over 1023, and that shift should give us a voltage of around 4 volts, okay and then were just going to do a serial print line of that voltage. Wait for a second and um redo it! Okay, so lets take a look at the serial, monitor and see what results we get wow so were feeding in 4 volts and were getting 3.85 wow. Now something is wrong right, i mean you, shouldnt be getting 3.

84 3.85 when youre feeding 4 volts into the arduino, and when you use this equation um. This is what you get. So what were going to do is were going to see whats going on here. Why am i only getting 3.85 when i should be getting 4 im using the right equation? Right well were going to step back and look at how this works, how the arduino works, how it does this whats called analog to digital conversion and figure out whats wrong with this, and why? What you see very often on the internet, just isnt, true so lets step back and take a look and see whats going on okay, so heres a simple representation of whats going on with the arduino when we feed it a voltage, and we ask it to give Us a number that represents that voltage so lets say, for example, ive got this varying analog voltage thats appearing at the analog input pin of the arduino, and i tell it to sample that voltage at a certain period of time and it samples 4.83 volts. Well, the job of the arduino is to convert that voltage value into a number that represents the voltage value. Unfortunately, it cant directly tell us 4.83 because of some limitations, but its going to give us a number that represents that voltage, so its basically a voltage to number converter. When you do an analog, read it converts a voltage into a number, and the number is something that we have to agree on with the um arduino.

So we know what that number represents in terms of voltage. Now we could, you know the arduino could convert it to a color or a range of colors. It doesnt matter. As long as we understand what the colors mean in terms of voltage, we need to convert back whatever it sends us. We need to be able to convert that back into an actual voltage, so now theres some problems we need to deal with. As i said, it basically converts a voltage to a number, but the converter, the analog to digital converter in the arduino doesnt deal with decimal points. Okay, so its not going to tell us 4.83 because it doesnt know what a decimal point is so thats one of the big problems we have to deal with and if we cant deal with decimal points, youre going to also see that youre going to need a Lot of numbers to accurately describe voltages so, for example, if its 4.83, it could even be 4.82 or 4.81 or 3.24, and if youre going to represent each of those little increments, youre going to need a lot of little increments to represent even 0 to 5 volts. So you know how were going to deal with it if you cant do decimal points and you need a lot of numbers, how are you going to deal with it? Well, if you think about it, how about, if we multiply the value we get times a hundred, so instead of we get say we get 4.

83 coming in. We multiply that times. 100 thatll give us 483.. So now you dont have to deal with decimal points. That could be a good solution right 4.83 times. 100 is 483 and maybe the arduino just sends to us the number 483 and we know how to convert it back to 4.83 um. So maybe you can have like 500 values that represent from 0 to 5.00 volts that might be a solution. Then we dont have to deal with the decimal points. Well, actually, the arduino does something like that, but it does it even better. So, instead of multiplying times 100, it multiplies times greater than 200 and as a result, it gives us 1024 values to play with. So it will convert that 0 to 5 volts to a value between 0 and 1023. Keep in mind 0 is a value. The total number of values, including zero, is a thousand twenty four, but the maximum value is represented by the number of thousand twenty three. So what it does is. It will take, for example, an input that goes from zero to 5 volts and when it sees 5 volts, it will return the value of 1023. When it sees 0 volts, it will return the value of 0.. So if 5 volts equals 1023, then 4.83 volts is less than 1023 and if you do, the math 4.83 comes out to 988.. So if it sees 4.83, it will send the value back to the computer, the value of 988.

Unfortunately, if youre a user on the computer, you need to figure out what does 988 mean, and how do i convert that back to 4.83, so there has to be an agreement between you and the arduino of what that 988 means and what the 1023 means in This case we have to agree that 1023. If i get a number of 1023 from the arduino, i have to know that that is equal to 5.00 volts. So we have to agree on what this conversion is, what the arduino does is it uses whats called a reference voltage or a vref internally to make that conversion. So, for example, if the vref is 5 volts, it will say: okay. Vref is 5 volts. Therefore thats. What im going to use as my 1023 to send back to the computer? However, we have to tell the arduino what that vref is. We have to agree. Is it 5 volts? Is it 5.1 volts? Is it 4.9 volts? We have to know because were going to have to convert back, were going to have to take that 988 and if its not 5, volts then were going to get an incorrect value. If we convert the 988 back to voltage, if that is 5.1, for example, its going to give us 988 isnt going to be the correct value okay, so we have to agree with the arduino on what the conversion is between voltage and 1023 numbers um. One of the big misconceptions or misunderstandings around the arduino is that it receives on the usb port to power the device it received.

5.0 volts, okay, theres, a lot of discussion and a lot of code and a lot of uh. Assuming that the value coming in from the usb is 5.0 volts, that is wrong. All right, in fact, its rarely 5 volts its all over the place, its not 5.0 volts. So if we think its 5 volts – and we do our conversions based on 5 volts like this – were going to get wrong readings. Okay, so lets step back and look at what we can actually expect from usv voltage, because the arduino uses that incoming voltage on the usb to determine the references and what it is going to set equal to 1023 here is a table from the usb 2.0 spec. Now you can look at the more recent usb specs. I just happen to have the 2.0 spec hanging around and in section 7.3.2 under bus timing, electrical characteristics, table 7.7 or 7 7 dc electrical characteristics. It defines what the range of usb voltage can be supplied to. Your device and its called see here it says vbus all right and if you look at the connector for usb 2.0 youve got four pins. Youve got a ground. Youve got two data. Pins and youve got a power which is the 5 volt nominal voltage. But as you can see from the chart, its not 5 volts for a high power port that v bus can be minimum 4.75 volts up to 5.25 volts. So you can start to see if im using 5, if im assuming 5 is equal to 1023, but im actually getting 5.

25 im gon na have incorrect readings all right. So this is one of the important things you need to know about the analog to digital conversion process. The arduino could be working on incoming voltages from 4.75 to 5.25 or, if its a low power port, it can be even lower 4.4. So we have to understand this and we have to apply it when we are doing readings. So what ive done is. I have gone through a bunch of my devices that have usb ports that supply voltage to devices, and i have measured what the no load nothing else connected im just measuring the voltage. What the no load voltage is on those ports, so ive got a battery pack. Its basically a 10 000 milliampere battery pack that you can use to charge cell phones and that kind of thing the no load voltage is 5.09 volts, its not 5 volts. A usb charger that i plug into the wall. Outlet is 5.14 volts. My laptop usb port is 5.09. My bench power supply that we looked at previously is 5.1, a usb hub that i use to power. All my devices is 5.25 desktops ive got three desktops with usb ports. One is 5.12, one is 5.06, one is 5.03. So now we know that the voltage coming over the usb into the arduino couldnt vary between 4.75 to 5.25 volts. Now what the arduino does with that is. It uses that voltage to generate a reference.

The reference is called vref and that vref it sets equal to 1023. So, for example, if my vref is 5.1, if it determines it takes in this, usb voltage converts it to a v ref of 5.1. If i feed in 5.1 volts into this analog input its going to send to the computer 1023 because it thinks that vref is 5.1 and that is equivalent to 1023.. If vref is 5.2, if it sees 5.2 coming into this analog input, it will send 1023.. So we have to be very, very, very careful to understand what vref its using to equate to 1023, which is the analog read value. It will send back to the computer heres an example. If i have 5.25 volts coming in to the usb from my usb hub, the vref that it will generate and thats the voltage you measure at this 5 volt pin is 5.18, so it thinks based on this input voltage, it thinks that vref is 5.18. So, as far as the arduino is concerned, 5.18 volts equals 1023.. So if it reads 5.18 on this analog input, it will send back to the computer okay 1023, and if the computer user thinks that 1023 is equal to 5 volts hes going to get a very wrong value. What that means is this code on the arduino that you see everywhere in a lot of examples is not right. The voltage is not whatever the red value of a thousand zero to 1023 times five over 1023.

In the case, we just showed its 5.18 over 1023. Okay, so you need to know what that v. Ref is so he can fill in this number in the equation. So its very, very, very important, so lets go back and see if we can figure out what the vref is and fill that into this equation and see if it fixes our initial errors instead of four volts were getting 3.85 volts. Okay, so here we are back to the bench and ive got the pretty much the same setup as i had before. I got four volts coming out of my dc power supply and im feeding that into the analog zero input of the arduino uh. And what ive got here is ive got a usb current and voltage meter, and you can see that it is showing that ive got five point about 5.2 volts coming through the usb into the arduino. What ive got here on my meter now is: i am measuring the 5 volt vref from the arduino im connected up to the 5 volt and ground and im measuring 5.14, so its taking 5.2 in from the usb and converting that to a vref of 5.14. So from what we talked about before, i should go back to my equation in the um sketch, and i should change from five to five point one four, since that is what the arduino is using to signify a thousand twenty three. So lets run back and change that to five point one four and see how that improves the accuracy of our readings.

Okay, so here we are in our sketch and well go down to this equation and now that we know that the vref is 5.14 well change that from 5.0 to 5.14, and then we will upload it. Compiling and uploading now. Well, look at the serial monitor and see what its reading from the arduino right on the money. 4.0 3.99 thats matching almost exactly what were getting on the um dc power supply. Again, you need to be very careful if youre going to use the usb port for powering your device and youre not going to have any external references. You need to understand how this works. If you want accurate results now in future, video well look at other ways to provide a reference.