Bit resolution

To resolve 5V into 10 bits, is it 5/1023 or 5/1024 ( since for 10 bit the range is 0-1023, but the number of parts is 1024, so the divisor should be 1024, no?) If not lease explain.

It is 1024 steps, but the the next step is at the analog value at that step (not half way).

I mean this: At 5V there are 1024 steps. A single step is for the analog value is 4.88mV. The ADC changes from 0 to 1 at 4.88mV (at a full step). Therefor a ADC value of 0 is for a voltage of 0V to 4.88mV, that is average 2.44mV (half a bit). The full 5.0V can not be measured.

The analog voltage is / 1024 * 5 + half a bit.
This can be useful when averaging.

ReadAnalogVoltage
Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

But the arduino website uses 1023 instead.

*/

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage

There are many mistakes online, even in the Arduino reference. As far as I know, it is really add half-a-bit and divide by 1024.

My example was wrong in my Reply #1, I have removed it. I added half-a-voltage instead of half-a-bit :frowning:

I think this is okay, but I didn’t check my projects yet.

  int n = 10;          // number of samples to average, maximum 30 will fit in 'total'.
  int total = 0;
  for( i=0; i<n; i++)
  {
    total += analogRead( A0);
  }
  float voltage = ( (float) total + (float) n / 2.0 ) / (float) n ) / 1024.0 * 5.0 );

In this example the ‘n’ is 10, that means the total is incremented with 5 before the rest of the calculation is done.

Thanks