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.
Using the internal reference voltage of the ADC
-
- Posts: 633
- Joined: Thu Dec 19, 2019 1:23 am
Re: Using the internal reference voltage of the ADC
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
the codes looks like this
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.
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);
}
Re: Using the internal reference voltage of the ADC
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
Unfortunately that's true yes, now that you wrote it, I realize this is true. Thanks for pointing it out for me...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.