cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F411RE gives wrong digital values when a 5V supply is given to the temperature sensor.

cchan.3
Associate II

I am trying to get LM35 temperature sensor value using STM32F411RE. When supply voltage fed to LM35 is 3.3 V, the correct digital data is produced. But when 5V is fed as supply to LM35, the digital values produced are the same as those when 3.3V is supplied. Do I have to configure the reference as 5V anywhere in the code? or Is there any way to get proper digital values for 5V supply as well? Please help me out.

0693W00000JOxtTQAT.pngCode:

ADC_HandleTypeDef hadc1;

DMA_HandleTypeDef hdma_adc1;

int main(void)

{

 HAL_Init();

 SystemClock_Config();

 MX_GPIO_Init();

 MX_DMA_Init();

 MX_ADC1_Init();

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

   /* USER CODE END WHILE */

HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&ADCres,ADCres_len);

if(conv_handler==1){

conv_handler=0;

   //HAL_ADC_Stop_DMA(&hadc1);

//break;

}

//HAL_ADC_Stop_DMA(&hadc1);

HAL_Delay(100);

   digital=ADCres;

temp=(digital*330)/4096;

 }

}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){

conv_handler=1;

}

static void MX_ADC1_Init(void)

{

 ADC_ChannelConfTypeDef sConfig = {0};

 /* USER CODE BEGIN ADC1_Init 1 */

 /* USER CODE END ADC1_Init 1 */

 /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)

 */

 hadc1.Instance = ADC1;

 hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;

 hadc1.Init.Resolution = ADC_RESOLUTION_12B;

 hadc1.Init.ScanConvMode = DISABLE;

 hadc1.Init.ContinuousConvMode = DISABLE;

 hadc1.Init.DiscontinuousConvMode = DISABLE;

 hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;

 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;

 hadc1.Init.NbrOfConversion = 1;

 hadc1.Init.DMAContinuousRequests = DISABLE;

 hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

 if (HAL_ADC_Init(&hadc1) != HAL_OK)

 {

  Error_Handler();

 }

 /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.

 */

 sConfig.Channel = ADC_CHANNEL_0;

 sConfig.Rank = 1;

 sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;

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

 {

  Error_Handler();

 }

}

1 ACCEPTED SOLUTION

Accepted Solutions
Peter BENSCH
ST Employee

You didn't mention what hardware you are using?

For what reason you want to set VREF to 5V? According to the datasheet the voltage at VREF+ is not allowed to be higher than VDD, which typically is 3.3V.

The LM35 creates a voltage proportional to the temperature, so a voltage range of 0...1.5V would correspond to a temperature of 0...150°C.

VREF+: OK, I was not precise enough. AVDD is not a supply, but needs to be connected to VDD, either directly for low requirements or via a filter network (LC or RC) for precision requirements.

VREF+ then sets the voltage range, so the ADC measures relative to this reference voltage. That's why you divide the ADC value by 4096 (=2^12) and multiply it with 330 (if VREF is connected to 3.3V, divided by 10mV due to the factor of the LM35). And that's the reason why I recommended to measure this voltage to get an actual value instead of the fixed one of 330=3.3V/10mV.

BTW: please keep in mind that with this setup you cannot measure temperatures below 0°C (or to be more precise below 2°C, see the datasheet of LM35). This would result in a negative voltage, which not only requires an additional negative supply, but would also exceed the permissible measuring range.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

7 REPLIES 7
Peter BENSCH
ST Employee

Welcome, @cchan.3​, to the community!

Of course, you will get the same results corresponding to the output voltage of the LM35. This output voltage is essentially independent of Vs of the LM35 and follows the function 10mV*temperature, where temperature is used in degrees Celsius.

So it doesn't matter whether you supply the LM35 with 4V or 30V - it's just not recommended to operate it below 4V, as TI's datasheet says.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Thanks for the reply @Peter BENSCH​ .

But when we make a conversion of digital value (say 343) to Celsius for 3V, the formula used is

temp=(digital*330)/4096------------- I get 27 C which is the correct temperature value.

whereas for the same digital value (343), the formula used is

temp=(digital*500)/4096-------------- I get 41 C which is the wrong value.

Now I understand where your mental knot is, but first for clarification:

  • The LM35 outputs an absolute voltage according to the given formula Vo=10mV*temperature [°C].
  • The ADC must therefore also perform an absolute measurement at this voltage, but can only do so relative to its reference voltage.

Now comes the solution to your error:

In your formula you do not have to use the VDD of the LM35, but the reference voltage of the ADC. The STM32F411RE in the LQFP64 has the VREF+ available via pin 13, so if you have connected it to VDD=3.3V, you clearly have to multiply by 330 (or better the current value of your voltage) in your formula.

Please note:

If VREF+ is connected to VDD, VREF+ usually has a more or less large deviation. It is then advisable to use a reference measurement, e.g. the internal reference or, if the requirements are high, an external reference voltage.

Good luck!

If the problem is resolved, please mark this topic as answered by selecting Select as best. This will help other users find that answer faster.

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

"digital" in your equation is VREF provided to the STM32 ADC. Probably it is 3.3 V and so it is independent from the supply of the LM35

@Peter BENSCH​ ,

you mean to say, whatever might be the supply voltage given to LM35, the VREF will be the voltage supplied to ADC on STM32. Is it so? If yes, how do I make that internal VREF as 5V?

>The STM32F411RE in the LQFP64 has the VREF+ available via pin 13, so if you have connected it to VDD=3.3V

By this, did you mean to say that VDD=3.3V on CN6 should be connected AVDD (pin 13) on CN5? Isn't AVDD itself a 3.3V supply?

Peter BENSCH
ST Employee

You didn't mention what hardware you are using?

For what reason you want to set VREF to 5V? According to the datasheet the voltage at VREF+ is not allowed to be higher than VDD, which typically is 3.3V.

The LM35 creates a voltage proportional to the temperature, so a voltage range of 0...1.5V would correspond to a temperature of 0...150°C.

VREF+: OK, I was not precise enough. AVDD is not a supply, but needs to be connected to VDD, either directly for low requirements or via a filter network (LC or RC) for precision requirements.

VREF+ then sets the voltage range, so the ADC measures relative to this reference voltage. That's why you divide the ADC value by 4096 (=2^12) and multiply it with 330 (if VREF is connected to 3.3V, divided by 10mV due to the factor of the LM35). And that's the reason why I recommended to measure this voltage to get an actual value instead of the fixed one of 330=3.3V/10mV.

BTW: please keep in mind that with this setup you cannot measure temperatures below 0°C (or to be more precise below 2°C, see the datasheet of LM35). This would result in a negative voltage, which not only requires an additional negative supply, but would also exceed the permissible measuring range.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Thanks for the clarity.

I didn't know that VREF could not go beyond VDD.

>For what reason you want to set VREF to 5V?

There is no particular reason, just wanted to operate the ADC at voltages other than 3.3V and check for digital values produced.