Changing analogRead Voltage reference?

Post here first, or if you can't find a relevant section!
Post Reply
neo_nmik
Posts: 20
Joined: Thu Feb 04, 2021 7:09 pm
Answers: 2

Changing analogRead Voltage reference?

Post by neo_nmik »

Hi guys,

Just wanted to check how to go about changing the analogRead voltage reference? There's some code examples here https://github.com/stm32duino/wiki/wiki/API but I'm not sure what to do with them?

Running a bluepill with the pots on a 3.3v rail, and I'm running the official STM32duino core, stm32duino (STM Cores 1.9.0).

Cheers,

Nick
by neo_nmik » Tue Apr 13, 2021 8:39 am
fredbox wrote: Mon Apr 12, 2021 9:50 pm AnalogRead is arduino compatible, so it is limited to 0-1023 by default. You can change this to 12 bits, 0-4095 by adding a line or two of code to your program.

See https://github.com/stm32duino/wiki/wiki/API#analog
Thanks Fredbox, I tried this, but unfortunately the same thing happens with the 12bit range.

Now I've had time to think it through/check everything, it's almost like the analogRead is expecting less than 3.3v (around 2.3V as far as I can tell) so when it hits that point, the extra voltage isn't doing anything. In my head, I assume that's what would happen if you were using 3.3V logic, and a 5V signal for your pots upper voltage?)

I've also just checked the onboard voltage, and it's reading 2.1V, which would account for the lowered Logic reference I guess. I'm just not sure why that would be? the only thing I'm powering through the bluepill is itself....

In the process of writing this, I've tested the 3.3V line on the bluepill, and it's reading around 2v, so that would make perfect sense. I've tested it with a simple blink, and with a USB power supply at 5V and it it peaks at about 2.7V. So that's my problem. I'm guessing maybe my LDO isn't up to scratch (or I've damaged it at some point :oops: )?

Would I be able to power the board externally by attaching one of my external LDO's to the 3.3V pin (and maybe detaching the 5v supply? :? )

Cheers for the help as always guys!
Go to full post
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Changing analogRead Voltage reference?

Post by ag123 »

you'd need to review the datasheet for your mcu, the internal voltage reference normally refers to a internal voltage source that produces a particular voltage. this may help if for instance vdd in particular for the adc isn't 3.3v. if you can be assured that vdd would after all be exactly 3.3v, (e.g. you have a good LDO etc) and that 0 is gnd for the adc, then an easy assumption is 3.3 = 4095 (12 bits) and 0 being 0v. so you could interpolate values in between. if vdd isn't 3.3v then having a constant voltage ref could be useful

e.g. if vrefint = 1.2v, and you got a reading from the adc as 1490, then you can try to guess the value of vdd e.g. 4095 * 1.2 / 1490 ~ 3.298 v
need to check i'm not too sure if this is after all correct
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Changing analogRead Voltage reference?

Post by GonzoG »

Some STM32 MCUs have ADC Vref pins. F103C8(B) do not have those and Vdd is used as ADC reference voltage.
There is internal reference voltage but it's not used as ADC reference. It can be used to calculate (measure) Vdd using equation ag123 provided n post above:
ag123 wrote: Mon Apr 12, 2021 4:16 pm e.g. if vrefint = 1.2v, and you got a reading from the adc as 1490, then you can try to guess the value of vdd e.g. 4095 * 1.2 / 1490 ~ 3.298 v
neo_nmik
Posts: 20
Joined: Thu Feb 04, 2021 7:09 pm
Answers: 2

Re: Changing analogRead Voltage reference?

Post by neo_nmik »

I might be off on my thinking it was down to a Voltage Ref setting then. :roll:.

I have 3 potentiometers (10k) wired up to pins PA0-PA2 (along with GND and 3.3V provided via a 3.3V LDO that’s one of 2 powered from one of the 5V pins).

When I poll them, I’m getting 0-1023 (10bit) which is fine for my use, but the upper range (roughly the last 1/4) are all responding 1023. I’ve checked the voltages on the pot pins, and everything seems right with the circuit, so I figured it might be down to a configuration setting in the stm32duino environment.

I guess I’ll have another check of the circuits and check somethings not broken!
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Changing analogRead Voltage reference?

Post by GonzoG »

neo_nmik wrote: Mon Apr 12, 2021 6:27 pm When I poll them, I’m getting 0-1023 (10bit) which is fine for my use, but the upper range (roughly the last 1/4) are all responding 1023.
Are those pots linear or logarithmic ??
neo_nmik
Posts: 20
Joined: Thu Feb 04, 2021 7:09 pm
Answers: 2

Re: Changing analogRead Voltage reference?

Post by neo_nmik »

GonzoG wrote: Mon Apr 12, 2021 6:38 pm
neo_nmik wrote: Mon Apr 12, 2021 6:27 pm When I poll them, I’m getting 0-1023 (10bit) which is fine for my use, but the upper range (roughly the last 1/4) are all responding 1023.
Are those pots linear or logarithmic ??
They're linear, when situated in the centre they're reading ~1.65V. and I'm getting a correct reading with the voltmeter on all parts of the scale.
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: Changing analogRead Voltage reference?

Post by fredbox »

AnalogRead is arduino compatible, so it is limited to 0-1023 by default. You can change this to 12 bits, 0-4095 by adding a line or two of code to your program.

See https://github.com/stm32duino/wiki/wiki/API#analog
neo_nmik
Posts: 20
Joined: Thu Feb 04, 2021 7:09 pm
Answers: 2

Re: Changing analogRead Voltage reference?

Post by neo_nmik »

fredbox wrote: Mon Apr 12, 2021 9:50 pm AnalogRead is arduino compatible, so it is limited to 0-1023 by default. You can change this to 12 bits, 0-4095 by adding a line or two of code to your program.

See https://github.com/stm32duino/wiki/wiki/API#analog
Thanks Fredbox, I tried this, but unfortunately the same thing happens with the 12bit range.

Now I've had time to think it through/check everything, it's almost like the analogRead is expecting less than 3.3v (around 2.3V as far as I can tell) so when it hits that point, the extra voltage isn't doing anything. In my head, I assume that's what would happen if you were using 3.3V logic, and a 5V signal for your pots upper voltage?)

I've also just checked the onboard voltage, and it's reading 2.1V, which would account for the lowered Logic reference I guess. I'm just not sure why that would be? the only thing I'm powering through the bluepill is itself....

In the process of writing this, I've tested the 3.3V line on the bluepill, and it's reading around 2v, so that would make perfect sense. I've tested it with a simple blink, and with a USB power supply at 5V and it it peaks at about 2.7V. So that's my problem. I'm guessing maybe my LDO isn't up to scratch (or I've damaged it at some point :oops: )?

Would I be able to power the board externally by attaching one of my external LDO's to the 3.3V pin (and maybe detaching the 5v supply? :? )

Cheers for the help as always guys!
Post Reply

Return to “General discussion”