Electronic – Conversion formula for adc

adcstm32

If you have a 12 bit adc, as the one in STM32F437, which of the following formulas do you use to calculate the analog value?

V = ADC_sample / 4095 * 3.3

or

V = ADC_sample / 4096 * 3.3

My question is similar to this one, however, I find the answers somewhat conflicting:

https://stackoverflow.com/q/892723/7689257

One answer linked to "The Data Conversion Handbook" and said that it should be the latter equation.

https://stackoverflow.com/a/7895054/7689257

The consensus in the answers dictate that you should refer to the datasheet of the adc, which is where I fail. I have tried to find the answers in the datasheet and reference manual:

https://www.st.com/resource/en/datasheet/DM00077036.pdf

https://www.st.com/content/ccc/resource/technical/document/reference_manual/3d/6d/5a/66/b4/99/40/d4/DM00031020.pdf/files/DM00031020.pdf/jcr:content/translations/en.DM00031020.pdf

I understand that the difference has no practical difference and perhaps not even measurable, but I want to understand the theory. Not only for the stm32f4 but with other adc's as well.


Update:
Thank you for all answers, I am still not quite sure what to make of this.
The comment from Scott Seidman on Ron Beyers anwser says that:

Many ADC's can only go up to Vref-1LSB. If Vref is 3.3, the ADC has no
code for 3.3V — it is out of range."

How can you tell if the ADC goes to Vref or Vref-1LSB?

A post by Adrian S. Nastase, states that an ADC will never go to Vref:

https://masteringelectronicsdesign.com/an-adc-and-dac-least-significant-bit-lsb/

Update 2
After searching some more I found an application note for STM32 microcontroller ADCs (AN2834):

https://www.st.com/content/ccc/resource/technical/document/application_note/group0/3f/4c/a4/82/bd/63/4e/92/CD00211314/files/CD00211314.pdf/jcr:content/translations/en.CD00211314.pdf

Under section 2.2.1 it is explained about reference voltage noise, and the following equation is used to calculate the digital value if the input is 1 volt.

(1/3.3) * 4095 = 0x4D9

And with the same formula instead have a 3.3v input would give:

(3.3/3.3) * 4095 = (1) * 4095 = 4095

And the answer to this question is therefore(?):

V = ADC_sample / 4095 * 3.3

Because this is how the ADC works on STM32 microcontrollers, which is/might be different on other vendors.

Best Answer

Consider a successive approximation ADC. Each step size successively halves the step size at the previous step, so teh first step (for the MSB) compares the input with Vref/2.

Then successive steps are smaller powers of 2, until ... 4095? No, 4096.

Now the ADC can output 4096 codes, from 0 to ... 4095.

Every output code is effectively "rounded down" to the largest step below it (in an ideal model of an ADC), this is a consequence of the comparator step

Therefore it cannot accurately represent an input voltage exactly equal to Vref ... effectively rounding it down to the code immediately below, i.e. 4095.

In practice of course you have to deal with the errors in non-ideal ADCs, as descried in Andy's answer.