Skip to main content
Visitor II
February 8, 2017
Question

How to calibrate the ADCs on STM32 using the HAL library?

  • February 8, 2017
  • 3 replies
  • 22028 views
Posted on February 08, 2017 at 20:19

Hello,

I am using the the HAL library for calibrating the ADC on STM32L in single ended mode

As per the documentation and Cube ADC examples I make a call to :

HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) at system startup.

Do I also need to call HAL_ADCEx_Calibration_GetValue() and and use this value?  or is the calibration factor already applied?

Below is my code to read the ADC value.

uint16_t get_adc_val(

int input_index

) {

   

   uint16_t uhADCxConvertedValue = 0;

   ADC_ChannelConfTypeDef sConfig;

    /*Set the adc channel based on index*/

   if (input_index==0) { 

      sConfig.Channel = ADC_CHANNEL_8;

   } else if (input_index==1) { 

      sConfig.Channel = ADC_CHANNEL_11;

   } else if (input_index==2) { 

      sConfig.Channel = ADC_CHANNEL_12;

   } 

   sConfig.Rank = 1;

   sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;

   sConfig.SingleDiff = ADC_SINGLE_ENDED;

   sConfig.OffsetNumber = ADC_OFFSET_NONE;

   sConfig.Offset = 0;

   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)

   {

      Error_Handler();

   }

   /*Start the conversion process*/

   if (HAL_ADC_Start(&hadc1) != HAL_OK)

   {

      Error_Handler();

   }

   /*Wait for the end of conversion*/

   if (HAL_ADC_PollForConversion(&hadc1, 1000) != HAL_OK)

   {

      Error_Handler();

   }

   else /* ADC conversion completed */

   {

      uhADCxConvertedValue = HAL_ADC_GetValue(&hadc1);

   }

return uhADCxConvertedValue;

}

Thanks!

#adc #hal #calibration
    This topic has been closed for replies.

    3 replies

    Graduate
    February 10, 2017
    Posted on February 10, 2017 at 22:04

    Answering is guesswork until you identify which L device you are referring to.

    For the STM32L4x, the related Reference Manual RM0351 (I have Rev1) Section 16.3.8 covers the topic well. The calibration is internally kept and applied with no action required on your application's part other than to initiate calibration for each ADC power on event and choose the single vs. differential calibration required . ST recommends recalibrating after long periods of operation.

    Cheers, Hal

    Visitor II
    February 10, 2017
    Posted on February 10, 2017 at 22:12

    Thanks! I am using L433.

    Visitor II
    January 3, 2019

    Hi,

    about calibration, when is necessary recalibrate?

    What is "long periods of operation"?

    1hour?

    1 Day?

    1000 conversions?

    Thanks

    Jordi

    Graduate
    January 3, 2019

    As you have noticed, ST's guidance in the reference manual isn't very helpful.

    ST's application note AN2834 on ADC accuracy is more helpful. In Section 3.2.8 it advises to use the internal temperature sensor, and recalibrate when the temperature changes significantly.

    If someone has done an Arctic to Sahara temperature range conversion error testing, perhaps they may share the results here. Otherwise, if your need is critical you may have to do the testing yourself.

    Cheers, Hal

    Visitor II
    January 4, 2019

    Hi @raptorhal2​ ,

    thanks for your feedback. It is very useful and clear.

    I did a fast read of AN2834, but I didn't find this tip. I will apply a recalibration when temperature changes X ºC.

    Another question, about calibrating temperature sensor- I'm using STM32F334, in datasheet at section 6.3.23 is explained that there is in specific FLASH adress calibrated values for 30ºC and 110ºC.

    Is this compensation done by any method in HAL?

    Or, Do you have to made it?

    Thanks

    Visitor II
    January 7, 2019

    Hi,

    thanks for your answer.

    I checked the HAL API, but I didn't find anything.

    HAL API gives support for a lot of things, it is curious that it doesn't give to calibrate temps sensor.

    Whatever, I compensated the temperature by application code.

    Thanks again for your support

    Jordi