STM32F091 ADC resolution 10 bit instead of 12 bit

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

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

void loop() {
long sensorValue = analogRead(0);
Serial.println(sensorValue);
}


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()


GianniDPC
Sat Jun 24, 2017 4:31 pm
[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()

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?


RogerClark
Sat Jun 24, 2017 11:15 pm
[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()

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
Sun Jun 25, 2017 9:07 am
[RogerClark – 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
Sun Jun 25, 2017 9:36 am
[RogerClark – Sat Jun 24, 2017 11:15 pm] –

[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()

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.


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.

Rick Kimball
Sun Jun 25, 2017 12:40 pm
[GianniDPC – 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 Kimball
Sun Jun 25, 2017 12:48 pm
[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.

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.


GianniDPC
Sun Jun 25, 2017 1:26 pm
[Rick Kimball – Sun Jun 25, 2017 12:40 pm] –

[GianniDPC – 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.


Rick Kimball
Sun Jun 25, 2017 1:35 pm
I’m pretty sure you should be using A0 not 0 and A1 not 1, that is how I have been testing. Otherwise it isn’t going to do the right thing:

Read A0:
void setup() {
Serial.begin(9600);
analogReadResolution(12);
}

void loop() {
long sensorValue = analogRead(A0);
Serial.println(sensorValue);
}


Rick Kimball
Sun Jun 25, 2017 2:21 pm
[GianniDPC – Sun Jun 25, 2017 1:26 pm] –
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.

Also, you shouldn’t be connecting 5v to any analog pin. When used as an analog input the max voltage is 3.3v. You are risking frying the analog peripheral.

From the datasheet:
TTa 3.3 V-tolerant I/O directly connected to ADC ..
PA1 is marked TTa


ahull
Sun Jun 25, 2017 2:25 pm
[GianniDPC – Sun Jun 25, 2017 1:26 pm] –

[Rick Kimball – Sun Jun 25, 2017 12:40 pm] –

[GianniDPC – 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.

I’m not sure I follow. If you are looking at pin A1, but your input is on pin A0, then if A1 is not tied high or low, it may well drift up towards the level of the adjacent pin(s).

I would get a couple of 100K linear pits, strap them between 5V and gnd, and point the wipers at the A0 and A1 pins, and slowly rotate the wiper from the gnd end towards the 5V end.

You should see the values on the analog pins follow, up till the reach a maximum when the output of the voltage divider formed by the 100k pot hits around 3.3V .. what happens next, if you move the wiper any further, rather depends on how robust the STM32F103 pins you have chosen are.

If they are 5V tolerant (remember the STM32F103 is a 3.3V device, not a 5V one), then they will stay at the maximum value, and when you rotate the wiper back towards gnd, the value will start to drop again. If they are not 5V tolerant, there will be a lot of heat, perhaps some smoke, and you will earn yourself an honorary membership of our well attended magic smoke club. :twisted:

Better still, check the spec. of pins in the STM32F103 datasheet first to be sure they will suffer such misuse.

EDIT: Thanks Rick, I must admit I was too lazy to check the spec of the analog pins.


Rick Kimball
Sun Jun 25, 2017 2:28 pm
[ahull – Sun Jun 25, 2017 2:25 pm] –
If they are 5V tolerant (remember the STM32F103 is a 3.3V device, not a 5V one), then they will stay at the maximum value, and when you rotate the wiper back towards gnd, the value will start to drop again. If they are not 5V tolerant, there will be a lot of heat, perhaps some smoke, and you will earn yourself an honorary membership of our well attended magic smoke club. :twisted:

Better still, check the spec. of pins in the STM32F103 datasheet first to be sure they will suffer such misuse.

EDIT: Thanks Rick, I must admit I was too lazy to check the spec of the analog pins.

He is using an STM32F091RC, the cortex-m0 stuff with the STM Core.


ahull
Sun Jun 25, 2017 2:38 pm
[Rick Kimball – Sun Jun 25, 2017 2:28 pm] –

[ahull – Sun Jun 25, 2017 2:25 pm] –
If they are 5V tolerant (remember the STM32F103 is a 3.3V device, not a 5V one), then they will stay at the maximum value, and when you rotate the wiper back towards gnd, the value will start to drop again. If they are not 5V tolerant, there will be a lot of heat, perhaps some smoke, and you will earn yourself an honorary membership of our well attended magic smoke club. :twisted:

Better still, check the spec. of pins in the STM32F103 datasheet first to be sure they will suffer such misuse.

EDIT: Thanks Rick, I must admit I was too lazy to check the spec of the analog pins.

He is using an STM32F091RC, the cortex-m0 stuff with the STM Core.

In that case, from my brief trawl through the datasheet (Chapter 6 – Electrical Characteristics) I suspect poking the analog pins with 5V is probably a pretty good way to join the magic smoke club. ;)


GianniDPC
Sun Jun 25, 2017 2:52 pm
[ahull – Sun Jun 25, 2017 2:38 pm] –

[Rick Kimball – Sun Jun 25, 2017 2:28 pm] –

[ahull – Sun Jun 25, 2017 2:25 pm] –
If they are 5V tolerant (remember the STM32F103 is a 3.3V device, not a 5V one), then they will stay at the maximum value, and when you rotate the wiper back towards gnd, the value will start to drop again. If they are not 5V tolerant, there will be a lot of heat, perhaps some smoke, and you will earn yourself an honorary membership of our well attended magic smoke club. :twisted:

Better still, check the spec. of pins in the STM32F103 datasheet first to be sure they will suffer such misuse.

EDIT: Thanks Rick, I must admit I was too lazy to check the spec of the analog pins.

He is using an STM32F091RC, the cortex-m0 stuff with the STM Core.

In that case, from my brief trawl through the datasheet (Chapter 6 – Electrical Characteristics) I suspect poking the analog pins with 5V is probably a pretty good way to join the magic smoke club. ;)

Thanks for pointing that out! I’m really stupid doing things before even reading through the data sheet. At least everything still works and I didn’t see any smoke haha.


Rick Kimball
Sun Jun 25, 2017 3:01 pm
[GianniDPC – Sun Jun 25, 2017 2:52 pm] –
Thanks for pointing that out! I’m really stupid doing things before even reading through the data sheet. At least everything still works and I didn’t see any smoke haha.

That’s what people from the 60’s said about LSD :) Just wait …


ahull
Sun Jun 25, 2017 4:03 pm
Image

What no magic smoke… I’m almost disappointed. ;)


fpiSTM
Tue Jun 27, 2017 12:20 pm
wow there’s lot of smoke here :)

To sum up, stm32 is 3.3V based so provide 5V as input ADC should bring some strange behavior… Mainly for ADC reading.

FYI, on the next release analogRead(0) and analogRead(A0) will be equivalent.
whereas analogWrite(0) is equivalent to analogWrite(D0) .


RogerClark
Tue Jun 27, 2017 12:52 pm
On the subject of magic smoke, I am sure I accidentally powered a F103VET board or several hour from 5V and it survived !

I think on the whole the STM32F103 MCU is quite robust


zmemw16
Tue Jun 27, 2017 3:26 pm
roger,
could you please try it with 7.5v / 9v / 12v and report your findings :D
actually 12v would be redundant as i already know the answer is copious.
stephen

Rick Kimball
Tue Jun 27, 2017 5:39 pm
[fpiSTM – Tue Jun 27, 2017 12:20 pm] –
wow there’s lot of smoke here :)

To sum up, stm32 is 3.3V based so provide 5V as input ADC should bring some strange behavior… Mainly for ADC reading.

FYI, on the next release analogRead(0) and analogRead(A0) will be equivalent.
whereas analogWrite(0) is equivalent to analogWrite(D0) .

I tried the latest github source code version with this change and I can verify that it indeed works with just 1 now in addtion to A1.


RogerClark
Tue Jun 27, 2017 9:32 pm
[zmemw16 – Tue Jun 27, 2017 3:26 pm] –
roger,
could you please try it with 7.5v / 9v / 12v and report your findings :D
actually 12v would be redundant as i already know the answer is copious.
stephen

I may try 5v again, because I have 2 old Red Pill boards, which are partially broken already, as the USB connections keep breaking


Leave a Reply

Your email address will not be published. Required fields are marked *