Skip to main content
Visitor
July 6, 2026
Question

Battery Voltage Measurement in STM32WB05

  • July 6, 2026
  • 0 replies
  • 6 views

We took cue from the ADC_Downsampling example from MX to write our Vbat measurement code. 

 

We are getting very small values of raw adc data - somewhere always between 0x160 and 0x200. 

 

We are using a coin cell with 3.3V nominal and are naturally expecting codes in the range of 3000 - 4000 (decimal format). 

 

I have attached our code and the MX configuration here. Can you please help us understand where we are going wrong?

 

int32_t Get_Battery_Voltage_mV(void) {
uint32_t adc_val = 0;
uint32_t vbat_mv = 0;

// 1. Start ADC, poll for conversion, and get value

/* Start ADC group regular conversion */
if (HAL_ADC_Start(&hadc1) != HAL_OK)
{
/* ADC conversion start error */
Error_Handler();
}

/* Wait for ADC conversion completed */
if (HAL_ADC_PollForConversion(&hadc1, 10) != HAL_OK)
{
/* End Of Conversion flag not set on time */
Error_Handler();
}

adc_val = HAL_ADC_GetValue(&hadc1);

HAL_ADC_Stop(&hadc1);

// 2. Convert raw ADC value to millivolts
vbat_mv = (uint32_t)(((uint64_t)adc_val * 3600) / 4096);

return adc_val;
}