I confuse about convert analogRead() to voltage divided by 1023 or 1024?

Sorry for a basic question. When convert analogRead() to voltage, many peoples said 1023 and 1024. Which one is really correct ?

int level = analogRead(A0);
float voltage = level * (5.0 / 1023.0);

See here for an extensive discussion on the matter:
https://forum.arduino.cc/index.php?topic=303189.0

Unless your 5V rail is accurate to 5mV or so, its really just academic... Knowing the details of
the ADC implementation will give clues. With a successive approximation ADC like in the ATmega
processors, the nature of the feedback DAC is what determines the levels. If this is a simple R-2R network
it will be 1024. Since the details of the innards of chips are seldom given away by the manufacturers
your best method is to first read the datasheet discussion of the ADC errors, or even measure a
sample of chips. Variations between chips may dominate the issue (particularly for 12 or more bits).

The processor datasheet says 1024.
I go with the datasheet, but it really is academic. (not that "academic" is a Bad Thing)

Thank you for that thread.

From datasheet ATmega328P:
http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf

The command analogRead() give the value between 0 - 1023.

• 0x000 is ground (not count as 1 step)
• 0x3FF is max value which is 1 step under the Vref.

Conclusion

  1. Convert ADC back to voltage must use 1024 (not 1023).
int level = analogRead(A1);
float voltage = level * (5.0 / 1024.0);
  1. Max ADC value = 1023 = 4.9951171875 volt

  2. It's so easy to remember that Vref = 1024 = 5.0 volt but ADC just can't give that value because of not enough byte.