How to Use DAC using STM32 HAL Library


Beyond simple analog output, the DAC can be used in combination with DMA and timers to generate waveforms such as sine waves or arbitrary signals.

Generating a Sine Wave with DAC + DMA:

  1. Prepare a buffer:
uint32_t sine_wave[] = {2048, 2447, 2831, 3185, 3495, 3750, 3939, 4056, 4095, 4056, 3939, 3750, 3495, 3185, 2831, 2447, 2048};
  1. Configure DAC and DMA in CubeMX.

  2. Use HAL function:

HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, sine_wave, sizeof(sine_wave)/sizeof(uint32_t), DAC_ALIGN_12B_R);

Tips:

  • Use a timer as a trigger source for consistent sampling intervals.

  • Ensure proper buffer size and alignment.