STM32F091 ADC resolution 10 bit instead of 12 bit

STM32F103 Nucleo boards e.g. STM Nucleo F103RB
GianniDPC
Posts: 16
Joined: Thu Jun 22, 2017 10:09 am

STM32F091 ADC resolution 10 bit instead of 12 bit

Post by GianniDPC » Sat Jun 24, 2017 4:04 pm

I just tested the ADC on my Nucleo STM32F091 board, and it shows a 10-bit resolution like the original Arduino's but my board supports 12-bit. If I look in to the source code, I see that the resolution is also defined as 12-bit.

Do I need to set something differently to achieve a 12-bit resolution?
Source code: https://github.com/stm32duino/Arduino_ ... /variant.h

Code: Select all

void setup() {
  Serial.begin(9600);
}

void loop() {
  long sensorValue = analogRead(0);
  Serial.println(sensorValue);
}
ADC.png
ADC.png (6.77 KiB) Viewed 662 times

User avatar
Rick Kimball
Posts: 1077
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by Rick Kimball » Sat Jun 24, 2017 4:17 pm

https://github.com/stm32duino/Arduino_C ... alog.c#L32

maybe a call analogReadResolution(12) in setup()
-rick

GianniDPC
Posts: 16
Joined: Thu Jun 22, 2017 10:09 am

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by GianniDPC » Sat Jun 24, 2017 4:31 pm

Rick Kimball wrote:
Sat Jun 24, 2017 4:17 pm
https://github.com/stm32duino/Arduino_C ... alog.c#L32

maybe a call analogReadResolution(12) in setup()
Thanks! that indeed does the trick.

One more question, I defined the ADC on port A0 in my code, and it works fine when I connect a voltage to A0. But if I plug my wire into A1 for example it also shows me that ADC value even when I didn't tell my code to do that. Is this normal behaviour?

User avatar
RogerClark
Posts: 7683
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by RogerClark » Sat Jun 24, 2017 11:15 pm

Rick Kimball wrote:
Sat Jun 24, 2017 4:17 pm
https://github.com/stm32duino/Arduino_C ... alog.c#L32

maybe a call analogReadResolution(12) in setup()
Thats interesting.

In had not realised that Frederics core defaulted to AVR resolution, I wonder what Daniel's defaults to.

BTW. Am am not going to change what libmaple does, as plenty of code has been written for it already and expects 12 bit

stevestrong
Posts: 2061
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by stevestrong » Sun Jun 25, 2017 9:07 am

RogerClark wrote:
Sat Jun 24, 2017 11:15 pm
BTW. Am am not going to change what libmaple does, as plenty of code has been written for it already and expects 12 bit
Indeed, it seems a pure Arduino_Core issue, not a libmaple core one.

danieleff
Posts: 336
Joined: Thu Sep 01, 2016 8:52 pm
Location: Hungary
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by danieleff » Sun Jun 25, 2017 9:36 am

RogerClark wrote:
Sat Jun 24, 2017 11:15 pm
Rick Kimball wrote:
Sat Jun 24, 2017 4:17 pm
https://github.com/stm32duino/Arduino_C ... alog.c#L32

maybe a call analogReadResolution(12) in setup()
Thats interesting.

In had not realised that Frederics core defaulted to AVR resolution, I wonder what Daniel's defaults to.

BTW. Am am not going to change what libmaple does, as plenty of code has been written for it already and expects 12 bit
10 bits. analogReadResolution is the standard Arduino API to change analogread bits from the default 10.

User avatar
ahull
Posts: 1727
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by ahull » Sun Jun 25, 2017 10:25 am

NOTE: Looking at the API documentation, it seems that changing the resolution using analogReadResolution(X) merely changes the resolution of the result.
A 12 bit read will still be performed, in the case of the STM32FXXX ADCs.
In other words, lowering the resolution will not result in faster analog read calls, and in reality, may make them slower, since the call will also include the time needed to scale the result to the required range.
- Andy Hull -

User avatar
Rick Kimball
Posts: 1077
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by Rick Kimball » Sun Jun 25, 2017 12:40 pm

GianniDPC wrote:
Sat Jun 24, 2017 4:31 pm
One more question, I defined the ADC on port A0 in my code, and it works fine when I connect a voltage to A0. But if I plug my wire into A1 for example it also shows me that ADC value even when I didn't tell my code to do that. Is this normal behaviour?
I don't understand the question. It "shows you the ADC value" what does that mean? Can you post the code you are using?
-rick

User avatar
Rick Kimball
Posts: 1077
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by Rick Kimball » Sun Jun 25, 2017 12:48 pm

ahull wrote:
Sun Jun 25, 2017 10:25 am
NOTE: Looking at the API documentation, it seems that changing the resolution using analogReadResolution(X) merely changes the resolution of the result.
A 12 bit read will still be performed, in the case of the STM32FXXX ADCs.
In other words, lowering the resolution will not result in faster analog read calls, and in reality, may make them slower, since the call will also include the time needed to scale the result to the required range.
The mapping is pretty simple when they are different just a shift. The real overhead is the giant HAL library sitting in front of the ADC combined with the Arduino mentality of lazy pin management, which favors run-time code over compile time code. If you are looking to do optimal ADC sampling at a high rate of speed, then you probably want to move away from Arduino code and implement it with code native to the chip you are using.
-rick

GianniDPC
Posts: 16
Joined: Thu Jun 22, 2017 10:09 am

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by GianniDPC » Sun Jun 25, 2017 1:26 pm

Rick Kimball wrote:
Sun Jun 25, 2017 12:40 pm
GianniDPC wrote:
Sat Jun 24, 2017 4:31 pm
One more question, I defined the ADC on port A0 in my code, and it works fine when I connect a voltage to A0. But if I plug my wire into A1 for example it also shows me that ADC value even when I didn't tell my code to do that. Is this normal behaviour?
I don't understand the question. It "shows you the ADC value" what does that mean? Can you post the code you are using?
Well the code is the same as in my first post. But what I mean is that I defined A0 as my analog pin in the code and I get my output from the serial monitor as expected, which is 4095 when using a 12-bit resolution.

But when I disconnect the 5V from A0 and connect it to A1 the serial monitor still shows me the same result so 4095. I don't expect this as I defined A0 as my analog pin in the code and not A1.

Post Reply