Page 1 of 1

Using the internal reference voltage of the ADC

Posted: Wed Nov 03, 2021 10:11 pm
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.

Re: Using the internal reference voltage of the ADC

Posted: Thu Nov 04, 2021 1:59 am
by mrburnette
STM32L432.JPG
STM32L432.JPG (86.31 KiB) Viewed 12140 times

Re: Using the internal reference voltage of the ADC

Posted: Thu Nov 04, 2021 8:14 am
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().

Re: Using the internal reference voltage of the ADC

Posted: Thu Nov 04, 2021 10:18 am
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.

Re: Using the internal reference voltage of the ADC

Posted: Thu Nov 04, 2021 10:35 pm
by GonzoG
Internal Vref cannot be used as ADC reference. You can use it only as reference to measure (calibrate) ADC Vref.

Re: Using the internal reference voltage of the ADC

Posted: Thu Nov 04, 2021 10:46 pm
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...