cancel
Showing results for 
Search instead for 
Did you mean: 

How to correctly convert an ADC digital value into Volt? By divison the with (2^n) or with (2^n -1)?

Lazac
Associate

STM32L152 has 12-bit ADC. How to convert the ADC's digital value into Volt, if there is a 'Vref' reference voltage, and Vref- is grounded?

I met two common solution for such ADC conversion:

(1) V = CODE / 2^n * Vref

(2) V = CODE / (2^n -1) * Vref

(CODE means the digital value, read from ADC register, n is the ADC's resolution)

CODE's range is 0...2^n -1, so its maximum value is (2^n -1) (4095 in case of 12-bit ADC).

In (1) case, the maximum code value corresponds to one step less voltage than Vref.

In (2) case, the maximum measureble voltage is just Vref.

For some other ADC's I used before (e.g. ATtiny25), the correct form was always (1).

But I am not sure about STM32L152. The formula on Page 287 of RM0038 Reference manual suggests the (2) form, as FULL_SCALE is 4095. But this formula is for the case where Vdda is the reference.

I also read "VREF+ is the highest voltage, represented by the full scale value". This also suggest (2).

There is a related thread, but the responses are contradictory:

https://community.st.com/s/question/0D50X00009XkeSr/stm32f407-adc-measurement-to-voltage

Its mainly a theoretical example, as the difference between the results of (1) and (2) are very small.

1 REPLY 1
Piranha
Chief II

The correct solution is (1).

The reason behind this is the fact that voltage range is divided in 4096 equal parts and these parts are numbered from 0 to 4095. For example, if the reference voltage is 4096 mV, then you want one division to be exactly 1 mV, not 4096 mV / 4095 = 1,0002442... mV. Ideally most likely (it depends on a device) one needs to add 0,5 or something like that to the ADC value, but dividing by 2^N-1 is always wrong.

AN2834 and especially section 3.1.1 answers this.

P.S. To be precise, the full scale voltage range is (VREF+) - (VREF-) or the same with VDDA and VSSA respectively.