Page 1 of 1

[STM32L452RE] analogRead() does not reach max value with VDDA/REF+

Posted: Mon Sep 04, 2023 11:01 am
by brixton
Hello all,

I have noticed something peculiar. I have removed SB57 to allow using VDDA/REF+ for external analog voltage reference. I have tied A0 directly to VDDA (pin 8, CN5), and to the voltage output from a Siglent SPD1305X power supply. Nucleo GND is tied to the GND of power supply.

In this situation, you'd expect the analogRead(A0) to consistently read 1023 (max value) since the reference voltage and the A0 input are the same. However this is not the case.
See sketch and serial monitor output. Any ideas what might be going wrong here? N.B. I am familiar with STM application note AN2834 on ADC accuracy.


EDIT: The result is the same when SB57 is ON (i.e. connected), and I feed the 3.3 V Nucleo output straight to A0. No external power supply attached.

Sketch

Code: Select all

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200); 
  // analogReadResolution(12);
  
}

void loop() {
  // Calculate the voltage of the voltage divider
  Serial.println(analogRead(A0));   
  delay(100); 
}
Serial output

Code: Select all

12:32:16.707 -> 1022
12:32:16.820 -> 1022
12:32:16.908 -> 1022
12:32:17.002 -> 1022
12:32:17.095 -> 1022
12:32:17.221 -> 1022
12:32:17.299 -> 1023
12:32:17.424 -> 1022
12:32:17.516 -> 1022
12:32:17.611 -> 1023
12:32:17.703 -> 1022
12:32:17.797 -> 1022
12:32:17.921 -> 1023
12:32:18.014 -> 1023
12:32:18.107 -> 1022
12:32:18.200 -> 1023
12:32:18.326 -> 1022
12:32:18.418 -> 1023
12:32:18.512 -> 1021
12:32:18.589 -> 1022
12:32:18.713 -> 1022
12:32:18.806 -> 1022
12:32:18.899 -> 1022
12:32:19.016 -> 1022
12:32:19.110 -> 1023
12:32:19.203 -> 1022
12:32:19.295 -> 1022
12:32:19.421 -> 1022
12:32:19.514 -> 1022
12:32:19.606 -> 1022
12:32:19.699 -> 1022
12:32:19.824 -> 1022
12:32:19.917 -> 1023
12:32:20.005 -> 1023
12:32:20.097 -> 1022
12:32:20.223 -> 1022
12:32:20.317 -> 1022
12:32:20.409 -> 1022
12:32:20.504 -> 1022
12:32:20.596 -> 1023
12:32:20.721 -> 1022
12:32:20.815 -> 1023
12:32:20.924 -> 1022
12:32:21.002 -> 1023
12:32:21.125 -> 1023
12:32:21.205 -> 1022
12:32:21.299 -> 1022

Gallery_1693823748008.jpg
Gallery_1693823748008.jpg (13.11 KiB) Viewed 5798 times
Gallery_1693824503816-compressed.jpg
Gallery_1693824503816-compressed.jpg (79.45 KiB) Viewed 5798 times

Re: [STM32L452RE] analogRead() does not reach max value with VDDA/REF+

Posted: Mon Sep 04, 2023 11:19 am
by brixton
Here an image of the setup when I do the same test, with SB57 ON (closed connection) and feeding Nucleo 3.3 V straight to A0.
Gallery_1693826097496-compressed.jpg
Gallery_1693826097496-compressed.jpg (85.11 KiB) Viewed 5791 times
Output:

Code: Select all

13:19:17.570 -> 1022
13:19:17.664 -> 1022
13:19:17.788 -> 1022
13:19:17.881 -> 1022
13:19:17.974 -> 1022
13:19:18.052 -> 1022
13:19:18.178 -> 1022
13:19:18.256 -> 1022
13:19:18.381 -> 1022
13:19:18.474 -> 1022
13:19:18.568 -> 1022
13:19:18.692 -> 1022
13:19:18.785 -> 1022
13:19:18.863 -> 1022
13:19:18.989 -> 1022
13:19:19.067 -> 1022
13:19:19.194 -> 1022
13:19:19.288 -> 1022
13:19:19.382 -> 1022
13:19:19.476 -> 1022
13:19:19.571 -> 1022
13:19:19.665 -> 1023
13:19:19.791 -> 1022
13:19:19.870 -> 1022
13:19:19.947 -> 1022
13:19:20.086 -> 1022
13:19:20.179 -> 1022
13:19:20.273 -> 1022
13:19:20.367 -> 1022
13:19:20.492 -> 1022
13:19:20.587 -> 1022
13:19:20.681 -> 1022
13:19:20.776 -> 1022
13:19:20.870 -> 1022
13:19:20.994 -> 1022
13:19:21.072 -> 1022
13:19:21.182 -> 1022

Re: [STM32L452RE] analogRead() does not reach max value with VDDA/REF+

Posted: Mon Sep 04, 2023 1:30 pm
by fpiSTM
Change the analog resolution. By default the ADC conversion uses the max bits resolution available and then it is converted to map default Arduino resolution (10bits) for compatibility.
https://www.arduino.cc/reference/en/lan ... esolution/

Re: [STM32L452RE] analogRead() does not reach max value with VDDA/REF+

Posted: Mon Sep 04, 2023 2:30 pm
by brixton
Hi @fpiSTM , thanks for the suggestion. When I do that I get values that aren't as close to 4095 as I'd expect. This is with SB57 ON (i.e. connection is closed).

Serial output:

Code: Select all

16:28:11.754 -> 4091
16:28:11.845 -> 4091
16:28:11.970 -> 4091
16:28:12.063 -> 4091
16:28:12.157 -> 4091
16:28:12.267 -> 4091
16:28:12.359 -> 4091
16:28:12.453 -> 4091
16:28:12.563 -> 4091
16:28:12.663 -> 4091
16:28:12.737 -> 4092
16:28:12.863 -> 4094
16:28:12.955 -> 4091
16:28:13.047 -> 4091
16:28:13.171 -> 4092
16:28:13.263 -> 4092
16:28:13.356 -> 4091
16:28:13.433 -> 4091
16:28:13.559 -> 4091
16:28:13.652 -> 4091
16:28:13.745 -> 4091
16:28:13.869 -> 4091
16:28:13.962 -> 4091
16:28:14.054 -> 4092
16:28:14.147 -> 4091
16:28:14.271 -> 4091
16:28:14.350 -> 4091
16:28:14.442 -> 4091
16:28:14.567 -> 4094
16:28:14.659 -> 4091
16:28:14.737 -> 4092
16:28:14.868 -> 4091
16:28:14.946 -> 4091
16:28:15.070 -> 4094
16:28:15.165 -> 4091
16:28:15.257 -> 4091
16:28:15.352 -> 4094
16:28:15.443 -> 4094
16:28:15.567 -> 4091
16:28:15.645 -> 4091
16:28:15.770 -> 4091
EDIT: When I do the same test, but connect A0 to GND then the minimum value is consistently reached (i.e. 0). So that's interesting, that the problem is isolated to only the 3.3 V side.

Code: Select all

16:39:18.941 -> 0
16:39:19.061 -> 0
16:39:19.155 -> 0
16:39:19.278 -> 0
16:39:19.370 -> 0
16:39:19.463 -> 0
16:39:19.557 -> 0
16:39:19.682 -> 0
16:39:19.775 -> 0
16:39:19.868 -> 0
16:39:19.960 -> 0
16:39:20.085 -> 0
16:39:20.177 -> 0
16:39:20.285 -> 0
16:39:20.378 -> 0
16:39:20.470 -> 0
16:39:20.595 -> 0
16:39:20.688 -> 0
16:39:20.781 -> 0
16:39:20.874 -> 0
16:39:20.999 -> 0
16:39:21.077 -> 0
16:39:21.201 -> 0
16:39:21.280 -> 0

Re: [STM32L452RE] analogRead() does not reach max value with VDDA/REF+

Posted: Mon Sep 04, 2023 3:28 pm
by MasterT
Read DS:
https://www.st.com/en/microcontrollers- ... 452re.html
Page 158
Table 79. ADC accuracy - limited test conditions 1
What you observe is normal, offset error. All adc have offset abd full scale (FS) errors, that easy to correct in software

Re: [STM32L452RE] analogRead() does not reach max value with VDDA/REF+

Posted: Mon Sep 04, 2023 6:24 pm
by ag123
Among other things you can try is to increase the sample time to a max possible

But that if that don't work, e.g. that it gives 4091 max, you could perhaps take that as the upper bound.
'electrically', these aren't really surprises as even OpAmps mostly won't reach VDD/VCC (for the output voltage), that is true for even 'rail-to-rail' OpAmps it could be at least micro-volts to milli-volts off VCC/VDD dependent on the loads and specs.

Re: [STM32L452RE] analogRead() does not reach max value with VDDA/REF+

Posted: Tue Sep 05, 2023 10:20 am
by brixton
MasterT wrote: Mon Sep 04, 2023 3:28 pm Read DS:
https://www.st.com/en/microcontrollers- ... 452re.html
Page 158
Table 79. ADC accuracy - limited test conditions 1
What you observe is normal, offset error. All adc have offset abd full scale (FS) errors, that easy to correct in software
Alright thanks! Do you know if there is any sample (Arduino) software that shows how to calibrate for these errors (primarily the offset and gain error)?

I was reading RM0394 Reference manual and page 384 describes the calibration procedure. However I was hoping there would be a sample arduino sketch that would give a bit of guidance... :)
ag123 wrote: Mon Sep 04, 2023 6:24 pm Among other things you can try is to increase the sample time to a max possible
I've set the sampling cycles to 640.5 clock cycles, using file "build_opt.h" in the sketch folder and the line

Code: Select all

 -DADC_SAMPLINGTIME=ADC_SAMPLETIME_640CYCLES_5
So I think there's not more I can do in that respect.