cancel
Showing results for 
Search instead for 
Did you mean: 

Hi. I want to measure the voltage of the battery with the ADC. Currently the voltage of the battery is 3.3 volts. I always see the result "6106" on the serial screen. What could I be doing wrong? Can you help me please?

ZKURT.1
Senior

code:

/* USER CODE BEGIN 2 */

 HAL_UART_Transmit(&hlpuart1, (uint8_t *)"Basladi\r\n", 9, 100);

HAL_ADC_Start(&hadc);

#define VREFINT_CAL_ADDR 0x1FF80078

#define VREFINT_CAL ((uint16_t*) VREFINT_CAL_ADDR)

#define MAX_VOLTAGE 3000.0

uint32_t VREFINTADC;

char buffer[23];

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 VREFINTADC=HAL_ADC_GetValue(&hadc);

 float VDD = MAX_VOLTAGE * (*VREFINT_CAL / (float) VREFINTADC);

 sprintf(buffer, "%f", VDD);

 HAL_UART_Transmit(&hlpuart1, (uint8_t *)buffer, 5, 1000);

 HAL_UART_Transmit( &hlpuart1, (uint8_t *)"\r\n", 2, 100);

 HAL_Delay(2000);

 }

This discussion is locked. Please start a new topic to ask your question.
27 REPLIES 27
Andrew Neil
Super User

0693W000008xsqBQAQ.pngHave you proved that your code actually works using various known input voltages; eg, supplied from a potentiometer?

You need to show the schematic of how you've connected the ADC input to the battery; it's not "behind" a voltage regulator - is it?

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
MM..1
Chief III

You cant start ADC and immediately get value.

 /* USER CODE BEGIN 3 */
//here add pool for value ... or wait for complete flag or how yuo config ADC
 
 VREFINTADC=HAL_ADC_GetValue(&hadc);
 

@MM..1​ "pool (sic) for value"

* poll

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
Peter BENSCH
ST Employee

And please remember that with the STM32L010C6 a measurement is always relative to VDD because VREF is not accessible due to the pin limitation of the package.

Absolute measurements are thus only possible indirectly by performing them with two measurements:

  • one measurement of Vrefint or an external reference voltage and
  • one measurement at the ADC input.

Afterwards, both have to be put in relation to each other to get an absolute value.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Alternatively, if VDD is directly connected to the battery, a measurement of Vrefint (which is constant) relative to VDD (which changes with battery voltage) will give you a measurement of the battery voltage ...

EDIT

If VDD is supplied via a regulator, this might (depending on the system) enable you to detect when the battery has fallen to such a level that the regulator can no longer regulate...

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

Hi. I started adc, I guess it doesn't appear in the code

hello, I didn't quite understand what you mean, but I connected a cable between the + part on the breadboard and the adc pin input. I'm currently running the power from the power supply. The power supply is adjustable, I can increase or decrease it. i will put battery after the code works

hello, I didn't quite understand what you mean, but I connected a cable between the + part on the breadboard and the adc pin input. I'm currently running the power from the power supply. The power supply is adjustable, I can increase or decrease it. After the code works, I will put a battery. What does it mean can't access Vref? What does a meter on the ADC input mean?

I watched videos about ADC, but I could not understand this subject (voltage measurement with ADC-Vref Internal). I need help. Are there any resources you can recommend?