How to Use ADC using STM32 HAL Library


The Analog-to-Digital Converter (ADC) is used to read analog signals and convert them into digital values.

Step-by-Step Guide:

  1. Enable ADC in STM32CubeMX:

    • Select a pin (e.g., PA0) and set its mode to ADC_IN.

    • Enable ADC1 in the ‘Peripherals’ tab.

  2. Generate Initialization Code.

  3. Write Application Code:

HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
uint32_t adc_value = HAL_ADC_GetValue(&hadc1);

Tips:

  • Configure the sampling time for accurate conversion.

  • Use DMA for continuous conversion without CPU overhead.