arduino Get Voltage From Analog Pin


Example

Analog pins can be used to read voltages which is useful for battery monitoring or interfacing with analog devices. By default the AREF pin will be the same as the operating voltage of the arduino, but can be set to other values externally. If the voltage to read is larger than the input voltage, a potential devider will be needed to lower the analog voltage.

#define analogPin 14    //A0 (uno)
#define AREFValue 5        //Standard for 5V Arduinos
#define ADCResolution 1023    //Standard for a 10bit ADC

int ADCValue = 0;
float voltage = 0;

void setup()
{
    Serial.begin(9600);
}

void loop() 
{
    readADC();
    Serial.print(voltage); Serial.println("V");
}

void readADC()
{
    ADCValue = analogRead(analogPin);
    float = ( ( (float)ADCValue/ADCRange ) * AREFValue );    //Convert the ADC value to a float, devide by the ADC resolution and multiply by the AREF voltage
}