cancel
Showing results for 
Search instead for 
Did you mean: 

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

Jnana Panuganti
Associate II
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
8 REPLIES 8
raptorhal2
Lead
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

Posted on February 10, 2017 at 22:12

Thanks! I am using L433.

Jordi Becares
Associate II

Hi,

about calibration, when is necessary recalibrate?

What is "long periods of operation"?

1hour?

1 Day?

1000 conversions?

Thanks

Jordi

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

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

You read AN2834 too fast. Read it again.

The STM32F334 Reference Manual RM0364 13.3.30, 5th paragraph, states that the user application makes the temperature sensor compensation. You could check the HAL source code, but I suspect you won't find anything.

Cheers, Hal

Jordi Becares
Associate II

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

specifically, see AN2834 Rev 4 :

4.2.9 Temperature-effect compensation

One method is be to fully characterize the offset and gain drift and provide a lookup table in memory to correct measurement according to temperature change. This calibration involves additional cost and takes time.

The second method consists in recalibrating the ADC when the temperature change reaches given values, by using the internal temperature sensor and the ADC watchdog.