title: Voltage Measurement on STM32
body: <p>Im new in ANSI C @STM32 but I tried to measure a Voltage (~12V) with a voltage divider and a Analog GPIO. </p>
  
  <p>I tried: </p>
  
  <pre><code>value = HAL_GPIO_ReadPin(VOLTAGE_GPIO_Port, VOLTAGE_Pin);
  </code></pre>
  
  <p>But it always return 0</p>
  
  <p>Then I tried to use the ADC (I dont know that thats required)</p>
  
  <pre><code>value = HAL_ADC_GetValue(&amp;hadc1);
  </code></pre>
  
  <p>But still 0.</p>
  
  <p>On Arduino you can simple use:</p>
  
  <pre><code>value = analogRead(1);
  </code></pre>
  
  <p>And it work. </p>
  
  <p>Here is the Init of the ADC Channel:</p>
  
  <pre><code>    static void MX_ADC1_Init(void)
  {
  
    /* USER CODE BEGIN ADC1_Init 0 */
  
    /* USER CODE END ADC1_Init 0 */
  
    ADC_ChannelConfTypeDef sConfig = {0};
  
    /* USER CODE BEGIN ADC1_Init 1 */
  
    /* USER CODE END ADC1_Init 1 */
    /** Common config 
    */
    hadc1.Instance = ADC1;
    hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
    hadc1.Init.ContinuousConvMode = DISABLE;
    hadc1.Init.DiscontinuousConvMode = DISABLE;
    hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
    hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
    hadc1.Init.NbrOfConversion = 1;
    if (HAL_ADC_Init(&amp;hadc1) != HAL_OK)
    {
      Error_Handler();
    }
    /** Configure Regular Channel 
    */
    sConfig.Channel = ADC_CHANNEL_15;
    sConfig.Rank = ADC_REGULAR_RANK_1;
    sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
    if (HAL_ADC_ConfigChannel(&amp;hadc1, &amp;sConfig) != HAL_OK)
    {
      Error_Handler();
    }
    /* USER CODE BEGIN ADC1_Init 2 */
  
    /* USER CODE END ADC1_Init 2 */
  
  }
  </code></pre>
  
  <p>Do I really need a ADC Channel, is there no "simple" Analog Input like Arduino? </p>
  
  <p>I know that I need to div the incoming value to get the right voltage but at the moment I do not get any data back. </p>
  
  <p>MCU is STM32F107VCT7</p>
  
  <p>EDIT:</p>
  
  <p>Tried the following now:</p>
  
  <pre><code>HAL_ADC_Start(&amp;hadc1);
    if (HAL_ADC_PollForConversion(&amp;hadc1, 1000000) == HAL_OK)
            {
    g_ADCValue = HAL_ADC_GetValue(&amp;hadc1);
  
            }
    HAL_Delay(500);
  </code></pre>
  
  <p>Now g_ADCValue return values but seems like random numbers... The Input voltage is not changed and get various data back. </p>
  
  <p>Found that Code: <a href="https://hackaday.io/project/4277-stm32f030f4p6-breakout-board/log/13897-printing-adc-values-over-uart" rel="nofollow noreferrer">https://hackaday.io/project/4277-stm32f030f4p6-breakout-board/log/13897-printing-adc-values-over-uart</a></p>
  
  <p>Tried 1:1, but it allways pint 0x3E, changing Voltage to not make any effect </p>