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 12135 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: 1898
Joined: Thu Dec 19, 2019 5:30 am
Answers: 30

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: 491
Joined: Wed Jan 15, 2020 11:30 am
Answers: 36
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...
Post Reply

Return to “General discussion”