Skip to main content

Subsection 6.1.2 Quantization: What the Number Means

The top of an ADC’s input range is set by a reference voltage, written \(V_\text{ref}\text{.}\) On the Nucleo board \(V_\text{ref}\) is wired to the 3.3 V supply, so an input of 0 V reads as count 0 and an input at \(V_\text{ref}\) reads at the top of the scale. Every count in this section is a fraction of \(V_\text{ref}\text{,}\) so every conversion we do depends on that one number.
An ADC divides its input range into a finite number of equal steps and reports which step the input falls into. How many steps it has is set by its resolution, expressed in bits. A \(B\)-bit ADC produces \(2^B\) distinct output values, mapping the input range \([0,\, V_\text{ref}]\) onto the integers \(0, 1, 2, \ldots, 2^B - 1\text{.}\)
Figure 6.1.2. The ADC as a system. An analog voltage between 0 and \(V_\text{ref}\) goes in, and a bundle of \(B\) wires carries the digital result out, as an integer between 0 and \(2^B - 1\text{.}\) On the STM32C031C6, \(V_\text{ref}\) is 3.3 V and \(B\) can be 6, 8, 10, or 12. We start with 12, and you will change it later. The relationship written across the figure, \(D_\text{out} = \lfloor 4096 \cdot V_\text{in} / V_\text{ref} \rfloor\text{,}\) is the whole conversion in one line, and it holds everywhere except at the very top of the range, where the result is limited to the largest count the converter has, 4095.
When we choose \(B = 12\) in our ADC this gives us 4096 steps across the 0–3.3 V range. One step (also called LSB, for least significant bit) is therefore worth
\begin{gather*} 1\text{ LSB} = \frac{V_\text{ref}}{2^B} = \frac{3.3\text{ V}}{4096} \approx 0.806\text{ mV} \end{gather*}
and to convert a count back into a voltage you multiply:
\begin{gather*} V = \text{count} \times \frac{V_\text{ref}}{2^B} = \text{count} \times \frac{3.3\text{ V}}{4096} \end{gather*}
Here is an example: A count of 2048 corresponds to \(2048 \times 3.3/4096 = 1.65\text{ V}\text{,}\) exactly half of \(V_\text{ref}\text{,}\) which is what you would hope for the middle count. Going the other way, an input of 1.0 V should produce \(\lfloor 1.0 \times 4096/3.3 \rfloor = 1241\) counts.
Notice the floor. The ADC cannot represent a voltage exactly; it reports the step the input fell into, so the result is always rounded down to a multiple of one LSB. The largest count, 4095, corresponds to \(4095 \times 3.3/4096 \approx 3.299\text{ V}\text{,}\) not 3.3 V: there are 4096 steps, but the highest one is numbered 4095. Dividing by 4095 instead of 4096, or expecting the top count to read exactly \(V_\text{ref}\text{,}\) is an easy slip to make.
The reason the divisor is 4096 is worth being precise about, because it is where that slip comes from. The converter is not placing your voltage on a line between two endpoints; it is chopping the range into 4096 equal bins, each one LSB wide, and reporting which bin the voltage fell into. Bin 0 covers 0 V up to one LSB, bin 1 the next LSB, and so on up to bin 4095, which covers the voltages just below \(V_\text{ref}\text{.}\) So the count is a bin number rather than a proportional position, 4096 is the number of bins rather than the number of gaps between them, and the top count stands for a range of voltages rather than for \(V_\text{ref}\) exactly.
More bits means smaller steps and less rounding error, at the cost of a slightly longer conversion: about two ADC clock cycles, some 167 ns, for every two bits you keep.