Using the internal reference voltage of the ADC

Post here first, or if you can't find a relevant section!
Post Reply
niagFT
Posts: 7
Joined: Wed Sep 29, 2021 8:18 pm

Using the internal reference voltage of the ADC

Post by niagFT »

Hi,

I am using an STM32L432KC (on a NUCLEO-32 board) in my project and I need to monitor the battery voltage, which I am doing by hooking up a voltage divider to the ADC. After quite some time spent understanding why the voltage read by the MCU didn't correspond to what I saw with the multimeter, I realized that using analogRead() means by default using the VREF+ voltage.

What I'm looking for is a way to use the internal reference voltage instead.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: Using the internal reference voltage of the ADC

Post by mrburnette »

STM32L432.JPG
STM32L432.JPG (86.31 KiB) Viewed 7013 times
niagFT
Posts: 7
Joined: Wed Sep 29, 2021 8:18 pm

Re: Using the internal reference voltage of the ADC

Post by niagFT »

mrburnette wrote: Thu Nov 04, 2021 1:59 amSTM32L432.JPG
Hm... yes, I have seen this. I know it, but I don't understand what you mean.

Does that simply mean that I can't use VREFINT as the reference for the ADC conversions? I don't want to read / measure this voltage through the ADC, but to use it as the reference for the conversions instead of the external VREF+, which seems to be the default when using analogRead().
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Using the internal reference voltage of the ADC

Post by ag123 »

the codes looks like this

Code: Select all

#include <Arduino.h>

void setup() {

	Serial.begin();
	
	pinMode(ATEMP, INPUT_ANALOG);
	pinMode(AVREF, INPUT_ANALOG);

}

void loop() {
	//ADC1 channel 18 is vrefint 1.23v
	//uint16_t vrefint = adc_read(ADC1, 17);
	uint16_t vrefint = analogRead(AVREF);
	Serial.print("Vref int (1.21v):");
	Serial.print(vrefint);
	Serial.println();

	//ADC1 channel 16 is temperature sensor
	//uint16_t vtemp = adc_read(ADC1, 18);
	uint16_t vtemp = analogRead(ATEMP);
	Serial.print("temp sensor:");
	Serial.print(vtemp);
	Serial.println();

	uint16_t mv = (1210 * vtemp) / vrefint;
	Serial.print("mvolt:");
	Serial.print(mv);
	Serial.println();

	// specs 5.3.22 temp sensor characteristics
	// V 25 deg ~ 0.76v
	// slope 2.5 mv/C
	uint16_t v25 = 760;
	float temp = (mv - v25) * 1.0 / 2.5 + 25.0;
	Serial.print("temp (deg C):");
	Serial.print(temp);
	Serial.println();

	delay(1000);
}
This is done for stm32f401 blackpill board. Review the specs sheet and ref manual for your MCU as some pins, parameters / constants may be different.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Using the internal reference voltage of the ADC

Post by GonzoG »

Internal Vref cannot be used as ADC reference. You can use it only as reference to measure (calibrate) ADC Vref.
niagFT
Posts: 7
Joined: Wed Sep 29, 2021 8:18 pm

Re: Using the internal reference voltage of the ADC

Post by niagFT »

GonzoG wrote: Thu Nov 04, 2021 10:35 pm Internal Vref cannot be used as ADC reference. You can use it only as reference to measure (calibrate) ADC Vref.
Unfortunately that's true yes, now that you wrote it, I realize this is true. Thanks for pointing it out for me...
danialita
Posts: 1
Joined: Wed Apr 05, 2023 5:28 pm

Re: Using the internal reference voltage of the ADC

Post by danialita »

lizee12 wrote: Thu Mar 09, 2023 9:43 am
niagFT wrote: Thu Nov 04, 2021 10:46 pm
GonzoG wrote: Thu Nov 04, 2021 10:35 pm Internal Vref cannot be used as ADC reference. You can use it only as reference to measure (calibrate) ADC Vref.
Unfortunately that's true yes, now that you wrote it, I realize this is true. Thanks for pointing it out for me... positive z words
I see what you mean, thank you for helping me understand the problem i was having for a long time.
Yes, it's that simple. You just can't use vrefint as the reference for the adc. I spent hours searching for the solution on google and youtube and even asked on reddit.
Andreaa23
Posts: 1
Joined: Wed Apr 12, 2023 7:12 am

Re: Using the internal reference voltage of the ADC

Post by Andreaa23 »

The voltage reference is a three-terminal device having connections to the supply rail, ground (common), and precise output voltage. (Figure 1). A reference that is inappropriate for the task or is used incorrectly will be erroneous, jeopardizing the authenticity and reliability of the converter's output.
miltonx
Posts: 1
Joined: Mon Jun 26, 2023 10:33 am

Re: Using the internal reference voltage of the ADC

Post by miltonx »

Thanks for pointing it out for me..
Post Reply

Return to “General discussion”