Arduino: Read Voltage Accurately

You can find the full sketch code at the bottom of the page or you can download the .ino file from DropBox

In many cases it is necessary to read the voltage of a pin. In this tutorial I will show how to write a function to accurately do that using integer variables.

To get an accurate reading we need 2 things. A good function to read the analog value, and the voltage at VREF pin. Each Arduino has a different VREF voltage. The voltage I measured was 4.90 volts. I used the variable vref and multiplied 1000 to convert it to mV.


I use the variables mv_val and tmp_val. mv_val will store the mVolts read, and tmp_val will store the value while it is in the function.


In the setup function, I add serial printing the values, and set the pin mode to input.


I assigned the name read_mv to the int function. The analog value is stored in the variable tmp_val, and then re-mapped. 0 to 1023 is 0v to Vref. We can get the voltage in mVolts by re-mapping the range from 0 to 1023 into 0 to Vref. The function then returns that value.


I setup the loop so that mv_val will store the mVolts, and serial will print the value in the serial window. delay 1000 so we can keep track.


Source Code

//Voltage measured on Vref pin
int vref = 4.9 * 1000; //multiply 1000 to convert to mv

//variables to store values
int mv_val, tmp_val;

void setup() {
pinMode(A0, INPUT);

void loop() {
mv_val = readmv(A0);

//function to read voltage
int readmv(byte tmpPIN){
tmp_val = analogRead(tmpPIN);
tmp_val = map(tmp_val, 0, 1023, 0, vref);

return tmp_val;
Leave a Reply

Your email address will not be published. Required fields are marked *