cancel
Showing results for 
Search instead for 
Did you mean: 

Analog read is not returning the maximum value

areify
Associate II

Hi there.

I was making some tests and I have observed that, when I shortcircuit 3V3 into my analog pin (in this case PA0), the reading I'm having is 4022 instead of 4095.

Why this difference? I'm using HAL libraries as follows:

//Function to obtain ADC value
uint32_t adc_get_value(uint8_t channel)
{
  uint32_t value;
  uint32_t adcChannel;
  
  ADC_ChannelConfTypeDef   sConfig;
  GPIO_InitTypeDef    GPIO_InitStruct;
  
  //Returns ADC channel and sets up GPIO port
  adcChannel = adc_get_channel(channel, &GPIO_InitStruct);
  
 /* Configuration of channel on ADCx regular group on sequencer rank 1 */                
  sConfig.Channel      = adcChannel;                  /* Sampled channel number */
  
  
  sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;     /* Minimum sampling time */
  sConfig.Rank         = ADC_REGULAR_RANK_1;          /* Rank of sampled channel number ADCx_CHANNEL */
  
  if (HAL_ADC_ConfigChannel(&adc_handle_, &sConfig) != HAL_OK)
  {
    /* Channel Configuration Error */
   return -1;
  }
  
  if(HAL_ADC_Start(&adc_handle_) != HAL_OK)
  {
    /* Enables ADC Error */
    return -1;
  }
  
  if (HAL_ADC_PollForConversion(&adc_handle_, 10) != HAL_OK)
  {
   /* Poll for conversion Error */
    return -1;
  }
      
  value = HAL_ADC_GetValue(&adc_handle_);
   
  if (HAL_ADC_Stop(&adc_handle_) != HAL_OK)
  {
   /* Stop ADC conversion Error */
    return -1;
  }
  
  return value;
}

Thank you in advance!

Areify ~

9 REPLIES 9

Perhaps scope wrt VREF+

Try a pin you don't have other circuitry hanging from it.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Sorry, but it is not possible to me at this moment to try that solution, due to the current design of the PCB in which it is installed... Do you have any other idea?

Another time, sorry. :(

And thank you for your help.

David SIORPAES
ST Employee

Try increasing 'sConfig.SamplingTime' and check if something is changing.

Hi David, it did not work, the readings did not vary...

Have you run the calibration process beforehand ?

No. It's neccesary with STM32F105VC? I thought it wasn't

Yes, it's better to calibrate ADC with

HAL_ADCEx_Calibration_Start()

in order to have best accuracy.

See section 11.4 of RM0008.

Yes, it worked!

I don't know exactly where, but I read it wasn't neccesary in this uC, so I was sorrounding the solution all time!

Thank you, David!

OK it seems to be not working with all values...

I'm trying to measure 2,1 volts generated with a divisor and I'm obtaining a value around 1400. When I stop the code with a breakpoint just after the calibration function I obtain a different value: around 2000.

This is so strange...