Docs

Micro-controllers, wireless transmission and database

Head

Internal temperature sensor with Blue Pill using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with blue-pill using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=kXg467nVd_A

Wiring Diagram

Diagram

Image

STM32CubeIDE Settings

Set PA1, PA4 and PA7 GPIO_Output

ADC1 - Temperature Sensor Channel (tick)

Parameter Settings --> ADC Settings --> Continuous Conversion Mode (Enabled)

Click Rank → Sampling Time 55.5 Cycles

See page No. 225 & 235 of https://www.st.com/resource/en/reference_manual/cd00171190-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf for sampling time calculation

Additional code on top of STM32CubeIDE generated code

/* USER CODE BEGIN PV */
uint16_t readValue;
float tCelsius;
float tFahrenheit;
/* USER CODE END PV */

  /* USER CODE BEGIN 2 */
  HAL_ADC_Start(&hadc1);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    HAL_ADC_PollForConversion(&hadc1,1000);
    readValue = HAL_ADC_GetValue(&hadc1);
    tCelsius = 357.558 - 0.187364 * readValue;
    tFahrenheit = 675.6 - 0.337255 * readValue;
    if (readValue > 1740)
    {
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 1);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 0);
    }
    else if (readValue > 1720 ) 
    {
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 0);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 0);
    }
    else 
    {
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 0);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 1);
    }
    HAL_Delay(100);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

Formula derival

Page No. 236 of https://www.st.com/resource/en/reference_manual/cd00171190-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf for formula

Page No. 79 of https://www.st.com/resource/en/datasheet/stm32f103c8.pdf for variable values

Temperature (in °C) = {(V25 - VSENSE) / Avg_Slope} + 25

V25 = 1.43

Avg_Slope = 4.3 mV/C = 0.0043 V/C

VSENSE = 3.3/4096 * ADCReading

Temperature (in °C) = {(1.43 - 3.3/4096 * ADCReading) / 0.0043} + 25

Temperature (in °C) = {332.558 - 0.187364 * ADCReading} + 25

Temperature (in °C) = 357.558 - 0.187364 * ADCReading

Temperature (in °F) = Temperature (in °C) * 9/5 + 32

Temperature (in °F) = (357.558 - 0.187364 * ADCReading) * 9/5 + 32

Temperature (in °F) = 643.6 - 0.337255 * ADCReading + 32

Temperature (in °F) = 675.6 - 0.337255 * ADCReading