cancel
Showing results for 
Search instead for 
Did you mean: 

ADC example measuring wrong voltage.

GS2
Senior

ADC example measuring wrong voltage.

Example - Repository\STM32Cube_FW_WB_V1.22.1\Projects\P-NUCLEO-WB55.Nucleo\Examples\ADC\ADC_SingleConversion_TriggerSW_IT

is always measuring 3.298 V even when voltage is 2.6V.

6 REPLIES 6
TDK
Super User

Connect it to VDD on the board. Do you get 3.3 V?

Connect it to GND, do you get 0 V?

 

If those work, you're probably not applying 2.6 V. Perhaps missing ground. Showing your setup would help.

If you feel a post has answered your question, please click "Accept as Solution".
GS2
Senior

Thank you for response.

Yes, when connected to GND I get 0 V. Power supply from Nucleo board directly connected to PC3 is 2.2 V. But ADC_SingleConversion_TriggerSW_IT showing 3.298 V!

I have the follow setup:

static void MX_ADC1_Init(void)
{
  ADC_ChannelConfTypeDef sConfig = {0};

  /** Common config */
  __HAL_RCC_ADC_CLK_ENABLE();

  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
  hadc1.Init.OversamplingMode = DISABLE;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel */
  sConfig.Channel = ADC_CHANNEL_4;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_92CYCLES_5;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */
  /* ADC1 interrupt Init */
  HAL_NVIC_SetPriority(ADC1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(ADC1_IRQn);
  /* USER CODE BEGIN ADC1_MspInit 1 */
  /* USER CODE END ADC1_Init 2 */
}

 

  //  __HAL_RCC_VREF_CLK_ENABLE(); // Enable the VREF clock
    HAL_SYSCFG_EnableVREFBUF();
    HAL_SYSCFG_VREFBUF_HighImpedanceConfig(SYSCFG_VREFBUF_HIGH_IMPEDANCE_DISABLE); // Disable the high impedance mode which is the default one read page 1694 of refman
    HAL_SYSCFG_VREFBUF_VoltageScalingConfig(SYSCFG_VREFBUF_VOLTAGE_SCALE1); // To set the volage to 2.5v
    HAL_SYSCFG_EnableVREFBUF(); // To enable VREFBUF

  /* Run the ADC calibration in single-ended mode */
  if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) != HAL_OK)
  {
    /* Calibration Error */
    Error_Handler();
  }
  ubAdcGrpRegularUnitaryConvStatus = 0;

  /* Init variable containing ADC conversion data */
  uhADCxConvertedData = VAR_CONVERTED_DATA_INIT_VALUE;
  /*## Start ADC conversions ###############################################*/

  /* Start ADC group regular conversion with IT */
  if (HAL_ADC_Start_IT(&hadc1) != HAL_OK)
  {
    /* ADC conversion start error */
    Error_Handler();
  }
  /* Wait till conversion is done */
  while (ubAdcGrpRegularUnitaryConvStatus == 0);
  uint16_t tt = uhADCxConvertedData_Voltage_mVolt;

 

TDK
Super User

> Power supply from Nucleo board directly connected to PC3 is 2.2 V. But ADC_SingleConversion_TriggerSW_IT showing 3.298 V!

Huh? The VDD voltage on that board is 3.3 V. A reading of 3.298 V is consistent with that.

 

You can't use VREFBUF as the voltage to the VREF+ pin is provided externally.

TDK_0-1746492835418.png

 

If you feel a post has answered your question, please click "Accept as Solution".
GS2
Senior

Thank you Guru,

Just to clarify: Should I add separate power supply for VDDA and VREF+ ?

If yes, what will be the best way to do it? This is my custom PCB.

But ADC_SingleConversion_TriggerSW_IT showing 3.298 V when board is powered from batteries 2.2v ?

GS2_0-1746548783342.png

 

 

 

TDK
Super User

If VREF+ is different than 3.3 V as it is on the board in the example, you need to change the VREF #define.

You can also estimate VREF+ by measuring the internal VREFINT channel and back-calculating what VREF+ is.

If you feel a post has answered your question, please click "Accept as Solution".
GS2
Senior

Thank you, how to measure the internal VREFINT channel and back-calculate what VREF+ ?

The battery voltage is changing - so is VREF #define. The goal is to measure battery voltage!