Reverse ADC for voltage

I understand when I read the voltage on an input pin the ADC converts it to a number between 0-1023. (and how this makes it a digital representation of the infinite number of possible analog inputs)

Can someone help me understand how to reverse this to get the voltage value? 1023 = 5.0 0 = 0.0 etc.

figure it out, had to modify the map function to return a float. This will give you the voltage on an analog input

void loop()

int raw = analogRead(analoginput);
float val = fmap(raw, 0, 1023, 0.0, 5.0);
Serial.println(val); //voltage

float fmap(float x, float in_min, float in_max, float out_min, float out_max)
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

int value;
float voltage;


void loop()
  value = analogRead(analogPin);  
  voltage = (value  * 5.0) / 1023;
  Serial.println(value, 3);



@robtillaart's code has a bug...

Serial.println(value, 3);

This line should be...

Serial.println(voltage, 3);

Thanx James, you are right :slight_smile:

It may seem a minor point, but we might as well get it as close as we can.

The calculation for Vin is given by

Vin = (ADC_reading * Vref) / 1024.0

The denominator is 1024, not 1023.

The voltage indicated by a full scale reading (1023 decimal = 0x3ff hex) is one LSB less than Vref.